Using VB.Net and Visual Studios 2005
Ok this app. wont have Dundas charts shaking in their boots (except with laughter), but it should give you a good starter into the Graphics class and it’s FillPie function. (I dont know why but everytime I think FillPie, I smile… ?)
Anyway, using the code supplied you can choose several colors, choose their percentages, and then (once you have reached 100%) finally have the FillPie (smile) function fire and see your glorious pie (ok, smile again…very sorry) drawn in front of your eyes.
To me this was a good starter into graphics and some simple graphics functionality, I hope it will help you as well.
The final app looks like this:
‘ Draws a pie chart.
Public Sub DrawPieChart()
‘Grab the surface to draw the pie upon (here we are drawing it on our panel for contrast)
Dim surface As Graphics = Panel1.CreateGraphics
‘Where in the panel?
Dim location As Point = New Point(80, 60)
‘How big of a pie?
Dim size As Size = New Size(420, 420)
‘used for our draw pie function
Dim percentTotal As Integer = 0
‘Iterating through our list of structs
For percent As Integer = 0 To colorList.Count – 1
‘Grab and name the method for the brush – this wasnt necessary but it makes the brush look better
Dim myColor As Color = colorList(percent).colorName
‘Okay, within the Graphics class we have a method called FillPie that takes a Brush, Rectangle (this is where we tell location and size),
‘The starting point for the angle (our first point is 0 ( 0 * 360 / 100 )), and our ending point for our angle (how much percent we assigned to each color)
‘and we are done!
surface.FillPie( _
New SolidBrush(myColor), _
New Rectangle(location, size), CType(percentTotal * 360 / 100, Single), CType(colorList(percent).colorPercent * 360 / 100, Single))
percentTotal += colorList(percent).colorPercent
Next
Return
End Sub
=========FULL SOURCE CODE HERE==========
