Article Options
Premium Sponsor
Premium Sponsor

 »  Home  »  .NET Newbie  »  Chart Success: GDI+ Graphics at work. Part 1  »  Calculating
 »  Home  »  Windows Development  »  Graphics  »  Chart Success: GDI+ Graphics at work. Part 1  »  Calculating
Chart Success: GDI+ Graphics at work. Part 1
by Ged Mead | Published  03/06/2005 | .NET Newbie Graphics | Rating:
Calculating

Calculate the Total
    Let’s pause here and check what it is we want to do.   We want to display each company’s “share” of the pie chart so that each is allocated the correct proportion of chart real estate.  

     So, if for example the grand total of the four Amounts was 1000, and the first company had an individual Amount value of 250, then of course you would expect that company’s segment of the pie chart to take up exactly 25%, one quarter, of the available space.        In real life, figures are rarely that user-friendly, but we can use some fairly basic math to get .Net to do the grunt work for us.
 
   Calculating the grand total is easy enough.  Enumerate through each of the individual amounts that we have stored in the arraylist, adding each Amount in turn to the total:

  Dim TotalCount As Single
  For Each gd As GraphData In Companies
      TotalCount += gd.Amount
  Next

 This TotalCount value will be used very shortly in a formula that allocates the correct pie portion to each of the companies. 

Calculate the Shares and Draw the Chart
     We are going to write code which calculates those shares that represent each company in the chart and then draws the Colored segments accordingly.

  A crucial piece of information that we have to pass to the drawing code is to tell it :
1. Where to begin drawing the next segment and
2. How big that segment should be

   To do this we need to understand two concepts that sometimes cause difficulty for those new to  GDI+.     These are StartAngle and SweepAngle.  

  a.   StartAngle .  The PieChart is an ellipse, which as you know has 360 degrees.   Although you might expect that the point at the very top of the ellipse is 0 degrees, this is not actually the case with GDI+.    In fact, the 0 degree point is at the far right hand point of the ellipse, effectively on a horizontal line drawn exactly through the centre of the ellipse.   

   That sounds more complicated than it really is and to prove that a picture is really worth a thousand words, here is one:

         

 

    The value of the StartAngle increases as you move in a clockwise rotation round the ellipse from the 0 degree start point.   You can see this in the above graphic.

    The StartAngle is simply the point in degrees on the ellipse where any particular arc begins.     So, in terms of the Pie Chart we are creating here, we will be interested in the StartAngles (or starting point in degrees) of each of the companies’ segments.

    b.   SweepAngle.   The SweepAngle is probably less confusing.   An arc is measured in degrees.   The SweepAngle is the number of degrees that any particular arc encompasses.  In the example above, the highlighted segment has a SweepAngle of about 45 degrees (and a StartAngle of 200).

   Again, dealing specifically with our Companies pie chart, the SweepAngle is the number of degrees in the pie segment that we allocate to each individual company .  That is, the proportion of the 360 degrees each company should be allotted.   


 

Comments    Submit Comment

Comment #1  (Posted by an unknown user on 03/13/2005)
Rating
The subject you have picked is one of the most interesting ones in programming. But sure enough, that may just be my opinion. When an interesting subject comes with explanations that are detailed but still very readable, then this means that the author has a great talent for writing and it shows immense responsibility and patience with beginning programmers. And that’s exactly what your article has achieved.
 
Comment #2  (Posted by an unknown user on 03/16/2005)
Rating
Very nice, Ged.

A small suggestion for part II: when would you use GDI+ vs. DirectX?
 
Comment #3  (Posted by Ged Mead on 03/22/2005)
Rating
Thanks for the comments. Part 2 is out now .... but I think it'll be a while before I get round to DirectX!
 
Comment #4  (Posted by an unknown user on 06/12/2005)
Rating
Hi Ged,
Nice and the way u explained is excellent .
The explanation is in steps and easy to understand and for me it the best methord to understand .
Good work i dont have word to appriciate u but excellent job.
From
Mohd Sufian
IT Specialist
Eastern Polymer Group
Thailand
 
Comment #5  (Posted by Dave on 10/03/2005)
Rating
Explained quite well! However I have a persistence problem that you mention in your article. I am drawing inside a picture box. My drawing code is in its own function which I call from the Paint() method. When the form first opens my drawing flashes up quite briefly and then disappears. It reappears and remains whenever I resize the window. How do I get it to come up without telling it to refresh which puts it in an endless loop?
 
Comment #6  (Posted by an unknown user on 04/05/2006)
Rating
Great explanation.I would like to know how to integrate database data with GDI.
 
Comment #7  (Posted by Ronan on 04/13/2006)
Rating
Hi i have read these tutorials and am looking your help.
I am trying to develop a pie chart interface in Visual Studio 2003 for a PDA (Smart Device Application) and tried your code. however it is not compatible as it does not recognise the drawPie and FillPie commands, etc. Do you have an example i could use to help me for this?
Thank you, Ronan
 
Comment #8  (Posted by an unknown user on 08/09/2006)
Rating
Excellent article. I have learnt a lot of GDI+ Stuff from it.
 
Comment #9  (Posted by an unknown user on 10/19/2006)
Rating
Very Useful article. Prakash Bajaj
 
Comment #10  (Posted by an unknown user on 12/22/2006)
Rating
really useful
 
Comment #11  (Posted by an unknown user on 12/29/2006)
Rating
no corners cut, all is clear
 
Comment #12  (Posted by an unknown user on 07/09/2007)
Rating
thank you
Very simple
detailed explanation
 
Comment #13  (Posted by an unknown user on 07/22/2007)
Rating
best for beginner's. language is just perfect
 
Comment #14  (Posted by neelabh on 07/22/2007)
Rating
Language is clear nd decription is best for beginner
 
Comment #15  (Posted by an unknown user on 10/01/2007)
Rating
Easy to follow tutorial. Picked up the main points straight away.
 
Comment #16  (Posted by an unknown user on 12/28/2007)
Rating
I have been searching for a good graphics article to get me started...with this one I found what I was looking for...GREAT JOB. Now I need to found out how to do vertical text.
 
Comment #17  (Posted by an unknown user on 02/05/2008)
Rating
Free, good, patient advice. A rare commodity and precious to beginners.
 
Comment #18  (Posted by an unknown user on 02/06/2008)
Rating
thanks for giving such a good article to start GDI+ from scratch.
 
Comment #19  (Posted by an unknown user on 02/20/2008)
Rating
good one
 
Sponsored Links