Code:
Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
If propertiesDictionary("Total Income") <> 0 Then
Dim totalHeight As Integer = CInt(propertiesDictionary("Total Income"))
Dim topHeight As Integer = CInt(totalHeight * 0.05)
' Flip vertically and scale to fit.
Dim scalex As Single = 20.0F
Dim scaley As Single = CSng(-PictureBox1.Height / (totalHeight * 1.5))
e.Graphics.ScaleTransform(scalex, scaley, MatrixOrder.Append)
' Translate so (0, MAX_VALUE) maps to the origin.
e.Graphics.TranslateTransform(0, PictureBox1.Height, MatrixOrder.Append)
Dim front() As PointF = { _
New PointF(0, 0), _
New PointF(0, totalHeight), _
New PointF(2, totalHeight), _
New PointF(2, 0), _
New PointF(0, 0) _
}
Dim top() As PointF = { _
New PointF(0, totalHeight), _
New PointF(1, totalHeight + topHeight), _
New PointF(3, totalHeight + topHeight), _
New PointF(2, totalHeight), _
New PointF(0, totalHeight) _
}
Dim right() As PointF = { _
New PointF(2, totalHeight), _
New PointF(3, totalHeight + topHeight), _
New PointF(3, topHeight), _
New PointF(2, 0), _
New PointF(2, totalHeight) _
}
' Draw the histogram.
For x As Integer = 0 To propertiesDictionary.Count - 1
rgns(x) = New GraphicsPath
Dim value As Integer = CInt(propertiesDictionary.ElementAt(x).Value)
If value > 0 Then
Dim offsetX As Single = CSng(x * 2.5)
Dim barFront() As PointF = Array.ConvertAll(front, Function(pf) New PointF(pf.X + offsetX, pf.Y))
barFront(1).Y = value
barFront(2).Y = value
e.Graphics.FillPolygon(New SolidBrush(barColors(x)), barFront)
e.Graphics.DrawPolygon(New Pen(Color.Black, 0), barFront)
rgns(x).AddPolygon(Array.ConvertAll(barFront, Function(pf) New PointF(pf.X * scalex, pf.Y * scaley)))
Dim barTop() As PointF = Array.ConvertAll(top, Function(pf) New PointF(pf.X + offsetX, value))
barTop(1).Y += topHeight
barTop(2).Y += topHeight
e.Graphics.FillPolygon(New SolidBrush(barColors(x)), barTop)
e.Graphics.DrawPolygon(New Pen(Color.Black, 0), barTop)
rgns(x).AddPolygon(Array.ConvertAll(barTop, Function(pf) New PointF(pf.X * scalex, pf.Y * scaley)))
Dim barRight() As PointF = Array.ConvertAll(right, Function(pf) New PointF(pf.X + offsetX, pf.Y))
barRight(0).Y = value
barRight(1).Y = value + topHeight
barRight(4).Y = value
e.Graphics.FillPolygon(New SolidBrush(barColors(x)), barRight)
e.Graphics.DrawPolygon(New Pen(Color.Black, 0), barRight)
rgns(x).AddPolygon(Array.ConvertAll(barRight, Function(pf) New PointF(pf.X * scalex, pf.Y * scaley)))
End If
Next
e.Graphics.ResetTransform()
Else
Array.Clear(rgns, 0, 10)
End If
End Sub