16.04.2012

SSRS Custom Drawing (Code)

Technical Value

SQL Server 2005 - 2012

After my last blog post , I got a lot of requests for the code I used for the graphics. So this post just shows the source code for the graphics of my last post.

Both examples require that you include a reference to System.Drawing to your report as shown below:

image

 

1. The deviation chart element

image

Here is the code used for the last column’s image:

  1. Function PaintAbw(ByVal width As Integer, ByVal height As Integer, ByVal min As Single, ByVal max As Single, ByVal middle As Single, ByVal cur As Single) As System.Drawing.Bitmap      
  2.     ' Draws a vertical deviation chart      
  3.     ' parameters      
  4.     '   width/height   width and height of the resulting image in pixel      
  5.     '   min/max        minimum and maximum value of the data being passed to the chart      
  6.     '   middle         position of the vertical reference line (usually zero or avg of the data)      
  7.     '                  data points below this value are plotted in red, above this value in blue      
  8.     '   cur            actual value to display
  9.     Dim objBitmap As System.Drawing.Bitmap      
  10.     Dim objGraphic As System.Drawing.Graphics      
  11.     Dim myBrush As System.Drawing.Brush      
  12.     Dim x0 As Integer, x As Integer
  13.     ' Initialize the graphic      
  14.     objBitmap = New System.Drawing.Bitmap(width, height)      
  15.     objGraphic = System.Drawing.Graphics.FromImage(objBitmap)      
  16.     objGraphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias      
  17.     objGraphic.FillRectangle(System.Drawing.Brushes.White, 0, 0, width, height)
  18.     ' Draw the vertical line (the "middle" position)      
  19.     x0 = 8 + ((width - 18) * (middle - min)) \ (max - min)      
  20.     objGraphic.DrawLine(System.Drawing.Pens.Gray, x0, 0, x0, height)      
  21.     If cur < min Then cur = min      
  22.     If cur > max Then cur = max
  23.     ' Draw the deviation (line and circle)      
  24.     x = 8 + ((width - 18) * (cur - min)) \ (max - min)      
  25.     objGraphic.DrawLine(System.Drawing.Pens.Gray, x0, height \ 2, x, height \ 2)      
  26.     If cur < middle Then      
  27.         myBrush = New System.Drawing.SolidBrush(System.Drawing.Color.FromArgb(215, 100, 100))      
  28.     Else      
  29.         myBrush = New System.Drawing.SolidBrush(System.Drawing.Color.FromArgb(100, 100, 215))      
  30.     End If      
  31.     objGraphic.FillPie(myBrush, x - 4, height \ 2 - 4, 8, 8, 0, 360)
  32.     ' Return the image as type bitmap      
  33.     Return objBitmap      
  34. End Function
  35. Function PaintAbwBmp(ByVal width As Integer, ByVal height As Integer, ByVal min As Single, ByVal max As Single, ByVal middle As Single, ByVal cur As Single) As Byte()      
  36.     ' Wrapper function for PaintAbw. This function is to be called from reporting services as      
  37.     ' for parameters see PaintAbw
  38.     Dim bmpImage As System.Drawing.Bitmap
  39.     ' Get bitmap from PaintAbw      
  40.     bmpImage = PaintAbw(width, height, min, max, middle, cur)
  41.     ' Convert this bitmap to a byte array of type BMP      
  42.     Dim stream As System.IO.MemoryStream = New IO.MemoryStream      
  43.     Dim bitmapBytes As Byte()      
  44.     bmpImage.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp)      
  45.     bitmapBytes = stream.ToArray      
  46.     stream.Close()      
  47.     bmpImage.Dispose()      
  48.     Return bitmapBytes      
  49. End Function

Examples

 

PaintAbw(100, 20, -100.0, 100.0, 0.0, 50.0) image
PaintAbw(100, 20, 0.0, 100.0, 0.0, 80.0) image
PaintAbw(100, 20, -100.0, 100.0, 0.0, -80.0) image

Usage within the report as the source for a bitmap

image

Sample Expression:

=Code.PaintAbwBmp(100,20,Min(Fields!Value.Value, "DataSet1"),Max(Fields!Value.Value, "DataSet1"), Avg(Fields!Value.Value, "DataSet1"), Fields!Value.Value)

 

2. The KPI slider

image

Here is the code used for the last column’s image:

  1. Function PaintBox(ByVal width As Integer, ByVal height As Integer, ByVal level As Single) As System.Drawing.Bitmap      
  2.     ' Draws a range chart (blending from red to green)      
  3.     ' parameters      
  4.     '   width/height   width and height of the resulting image in pixel      
  5.     '   min/max        minimum and maximum value of the data being passed to the chart      
  6.     '   level          position of slide      
  7.     '                  0: left, 1: right, 0.5 middle      
  8.     '                  make sure you scale this value to your needs
  9.  
  10.  
  11.     Dim objBitmap As System.Drawing.Bitmap      
  12.     Dim objGraphic As System.Drawing.Graphics
  13.  
  14.  
  15.     ' Initialize the graphic      
  16.     objBitmap = New System.Drawing.Bitmap(width, height)      
  17.     objGraphic = System.Drawing.Graphics.FromImage(objBitmap)      
  18.     objGraphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias      
  19.     objGraphic.FillRectangle(System.Drawing.Brushes.White, 0, 0, width, height)
  20.  
  21.  
  22.     ' Create color gradient for the background      
  23.     Dim BrushRedYellow As New System.Drawing.Drawing2D.LinearGradientBrush(New System.Drawing.Rectangle(8, 0, (width - 16) \ 2, height), System.Drawing.Color.Red, System.Drawing.Color.Yellow, System.Drawing.Drawing2D.LinearGradientMode.Horizontal)      
  24.     Dim BrushYellowGreen As New System.Drawing.Drawing2D.LinearGradientBrush(New System.Drawing.Rectangle(7 + (width - 16) \ 2, 0, (width - 16) \ 2, height), System.Drawing.Color.Yellow, System.Drawing.Color.Green, System.Drawing.Drawing2D.LinearGradientMode.Horizontal)      
  25.     objGraphic.FillRectangle(BrushRedYellow, 8, 3, (width - 16) \ 2, height - 10)      
  26.     objGraphic.FillRectangle(BrushYellowGreen, 8 + (width - 16) \ 2, 3, (width - 16) \ 2, height - 10)
  27.  
  28.  
  29.     ' Scale level properly      
  30.     If level > 1 Then level = 1      
  31.     If level < 0 Then level = 0
  32.  
  33.  
  34.     ' Draw the arrow      
  35.     Dim Arrow(2) As System.Drawing.PointF      
  36.     Arrow(0) = New System.Drawing.PointF(8 + (width - 16) * level, height - 10)      
  37.     Arrow(1) = New System.Drawing.PointF(16 + (width - 16) * level, height - 2)      
  38.     Arrow(2) = New System.Drawing.PointF(0 + (width - 16) * level, height - 2)      
  39.     objGraphic.FillPolygon(System.Drawing.Brushes.Black, Arrow)
  40.  
  41.  
  42.     ' Return the image as type bitmap      
  43.     Return objBitmap      
  44. End Function
  45.  
  46.  
  47. Function PaintBoxBmp(ByVal width As Integer, ByVal height As Integer, ByVal level As Single) As Byte()      
  48.     ' Wrapper function for PaintBox. This function is to be called from reporting services as      
  49.     ' for parameters see PaintBox      
  50.     Dim bmpImage As System.Drawing.Bitmap
  51.  
  52.  
  53.     ' Get bitmap from PaintBox      
  54.     bmpImage = PaintBox(width, height, level)
  55.  
  56.  
  57.     ' Convert this bitmap to a byte array of type BMP      
  58.     Dim stream As System.IO.MemoryStream = New IO.MemoryStream      
  59.     Dim bitmapBytes As Byte()      
  60.     bmpImage.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp)      
  61.     bitmapBytes = stream.ToArray      
  62.     stream.Close()      
  63.     bmpImage.Dispose()      
  64.     Return bitmapBytes      
  65. End Function      

Examples

PaintBox(100, 20, 0.0) image
PaintBox(100, 20, 1.0) image
PaintBox(100, 20, 0.5) image

 

  1. =Code.PaintBoxBmp(100,20,Fields!Sales_Amount.Value/Fields!Sales_Amount_Quota.Value)

Sample Expression:

  1. =Code.PaintAbwBmp(100,20,Min(Fields!Value.Value, "DataSet1"),Max(Fields!Value.Value, "DataSet1"), Avg(Fields!Value.Value, "DataSet1"), Fields!Value.Value)

 

Teilen auf

Newsletter Anmeldung

Abonnieren Sie unseren Newsletter!
Lassen Sie sich regelmäßig über alle Neuigkeiten rundum ORAYLIS und die BI- & Big-Data-Branche informieren.

Jetzt anmelden