Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hello !

I am working in VB.NET 2010.
I am using MS Chart Control to Draw a Pie Chart.
The pie chart is given the data from a table that contains four records ...
i.e.
ColorName NumberofItems
Green 100
Blue 200
Red 300
Yellow 400

Total Items=1000

My pie chart is showing the results as ....

Green 100
Blue 200
Red 300
Yellow 400

but i want to see the result in percentage like...

Green 100 (1.00%)
Blue 200 (2.00%)
Red 300 (3.00%)
Yellow 400 (40.00%)

i tried my best but i could not find the solution.

Can anyone solve my problem ?

Thanks!

Best Regards:
Muhammad Asim Mughal
Posted

1 solution

Well, the formula to compute a percentage is:
VB
Dim v1 As Double = 100
Dim v2 As Double = 200
Dim v3 As Double = 300
Dim v4 As Double = 400
Dim total As Double = v1 + v2 + v3 + v4
Dim p1 As Double = v1 / total '' p1 = 100 / 1000 = 0.1
Dim p2 As Double = v2 / total '' p2 = 200 / 1000 = 0.2
Dim p3 As Double = v3 / total '' p3 = 300 / 1000 = 0.3
Dim p4 As Double = v4 / total '' p4 = 400 / 1000 = 0.4
Dim p1Display As String = p1.ToString("P") '' p1Display = "10.00%"
Dim p2Display As String = p2.ToString("P") '' p2Display = "20.00%"
Dim p3Display As String = p3.ToString("P") '' p3Display = "30.00%"
Dim p4Display As String = p4.ToString("P") '' p4Display = "40.00%"

Important points here:
- Compute the percent value in its primary, real form. If you have to save it in a repository, keep its real value, not its string representation.
- Display it as a percent only when you are presenting the value to the user. Here I used the ToString("P") method to tell that I want the percent-representation of the underlying real value.
- The concept in two points above is valid for all types of data, and especially datetimes. Always handle and store datetime values, not their string representation.

Finally, as it is your question is more a mathematic question than a development one. Kindly, I advise you to improve your skills in mathematics; as a developper, it will make your every-day life way much easier.

Hope this helps. Good work :)

[Edit] To apply the percentage format to a chart label, you can do it this way:
VB
yourChart.ChartAreas.First().AxisY.LabelStyle.Format = "P"
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900