1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
|
' Shows how to call the DrawPieChart method
Public Sub DrawPieChartHelper()
Dim percents() As Integer = {10, 20, 70}
Dim colors() As Color = {Color.Red, Color.CadetBlue, Color.Khaki}
Dim graphics As Graphics = Me.CreateGraphics
Dim location As Point = New Point(10, 10)
Dim size As Size = New Size(150, 150)
DrawPieChart(percents, colors, graphics, location, size)
End Sub
' Draws a pie chart.
Public Sub DrawPieChart(ByVal percents() As Integer, ByVal colors() As Color, _
ByVal surface As Graphics, ByVal location As Point, ByVal pieSize As Size)
' Check if sections add up to 100.
Dim sum As Integer = 0
For Each percent As Integer In percents
sum += percent
Next
If sum <> 100 Then
Throw New ArgumentException("Percentages do not add up to 100.")
End If
If percents.Length <> colors.Length Then
Throw New ArgumentException("There must be the same number of percents and colors.")
End If
Dim percentTotal As Integer = 0
For percent As Integer = 0 To percents.Length() - 1
surface.FillPie( _
New SolidBrush(colors(percent)), _
New Rectangle(location, pieSize), CType(percentTotal * 360 / 100, Single), CType(percents(percent) * 360 / 100, Single))
percentTotal += percents(percent)
Next
Return
End Sub |