BLOG

SSRS Custom Drawing (Code)

16.04.2012 Hilmar Buchta

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.     Dim objBitmap As System.Drawing.Bitmap
  10.     Dim objGraphic As System.Drawing.Graphics
  11.     ‚ Initialize the graphic      
  12.     objBitmap = New System.Drawing.Bitmap(width, height)
  13.     objGraphic = System.Drawing.Graphics.FromImage(objBitmap)
  14.     objGraphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias
  15.     objGraphic.FillRectangle(System.Drawing.Brushes.White, 0, 0, width, height)
  16.     ‚ Create color gradient for the background      
  17.     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)
  18.     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)
  19.     objGraphic.FillRectangle(BrushRedYellow, 8, 3, (width 16) \ 2, height 10)
  20.     objGraphic.FillRectangle(BrushYellowGreen, 8 + (width 16) \ 2, 3, (width 16) \ 2, height 10)
  21.     ‚ Scale level properly      
  22.     If level > 1 Then level = 1
  23.     If level < 0 Then level = 0
  24.     ‚ Draw the arrow      
  25.     Dim Arrow(2) As System.Drawing.PointF
  26.     Arrow(0) = New System.Drawing.PointF(8 + (width 16) * level, height 10)
  27.     Arrow(1) = New System.Drawing.PointF(16 + (width 16) * level, height 2)
  28.     Arrow(2) = New System.Drawing.PointF(0 + (width 16) * level, height 2)
  29.     objGraphic.FillPolygon(System.Drawing.Brushes.Black, Arrow)
  30.     ‚ Return the image as type bitmap      
  31.     Return objBitmap
  32. End Function
  33. Function PaintBoxBmp(ByVal width As Integer, ByVal height As Integer, ByVal level As Single) As Byte()
  34.     ‚ Wrapper function for PaintBox. This function is to be called from reporting services as      
  35.     ‚ for parameters see PaintBox      
  36.     Dim bmpImage As System.Drawing.Bitmap
  37.     ‚ Get bitmap from PaintBox      
  38.     bmpImage = PaintBox(width, height, level)
  39.     ‚ Convert this bitmap to a byte array of type BMP      
  40.     Dim stream As System.IO.MemoryStream = New IO.MemoryStream
  41.     Dim bitmapBytes As Byte()
  42.     bmpImage.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp)
  43.     bitmapBytes = stream.ToArray
  44.     stream.Close()
  45.     bmpImage.Dispose()
  46.     Return bitmapBytes
  47. 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)

 

jetzt Teilen auf

Your email address will not be published.

Join #teamoraylispeople

Gestalte mit uns
die Welt der Daten