.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal

Home >> Code Snippets >> Windows Form Controls >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Draw Pie Chart

Posted By:Ghanashyam Nayak       Posted Date: December 16, 2010    Points: 15    Category: Windows Form Controls    URL: http://www.dotnetspark.com  

Code for draw pie chart in windows application using vb.net.

Public Sub DrawPieChartOnForm()
	' Pie chart values...
	Dim PiePercent = {1, 2, 3, 4, 5}

	' Pie chart colors...
	Dim PieColors = {Color.Red, Color.Lavender, Color.Blue, Color.Green, Color.Gold}

	'draw a pie chart at particular location & at specified size...
	Using PieGraphic = Me.CreateGraphics()
		Dim PieLocation As New Point(10, 10)
		Dim PieSize As New Size(150, 150)
		DrawPieChart(PiePercent, PieColors, PieGraphic, PieLocation, PieSize)
	End Using
End Sub

' Draws a pie chart.
Public Sub DrawPieChart(ByVal PiePercents() As Integer, ByVal PieColors() As Color, ByVal PieGraphic As Graphics, ByVal PieLocation As Point, ByVal PieSize As Size)
	' Check if sections add up to 100.
	Dim sum = 0

	' check the total which must be 100...
	For Each percent In PiePercents
		sum += percent

	If sum <> 100 Then
		Throw New ArgumentException("Percentages do not add up to 100.")
	End If

	'check here pie chart values & its colors length...
	If PiePercents.Length <> PieColors.Length Then
		Throw New ArgumentException("There must be the same number of percents and colors.")
	End If

	Dim PiePercentTotal = 0

	' Draw particluar pie depend on the values of pie & fill its with color...
	For PiePercent = 0 To PiePercents.Length() - 1
		Using brush As New SolidBrush(PieColors(PiePercent))
			PieGraphic.FillPie(brush, New Rectangle(Location, PieSize), CSng(PiePercentTotal * 360 / 100), CSng(PiePercents(PiePercent) * 360 / 100))
		End Using
		PiePercentTotal += PiePercents(PiePercent)
End Sub


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend