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:

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

 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.   


Sponsored Links