Article Options
Premium Sponsor
Premium Sponsor

 »  Home  »  .NET Newbie  »  Chart Success: GDI+ Graphics At Work. Part 2  »  Draw The Chart: Horizontals
Chart Success: GDI+ Graphics At Work. Part 2
by Ged Mead | Published  03/16/2005 | .NET Newbie | Rating:
Draw The Chart: Horizontals

Step 5:  Horizontal Axis and the Bars

5a.  Base Line
  The logic for the Horizontal (X) Axis is similar to that used to draw and measure the Vertical Axis:

  '  First draw the X (horizontal) axis line
  g.DrawLine(LinePen, LeftMargin, PBBarChart.Height - BaseMargin, _
     PBBarChart.Width - RightMargin, PBBarChart.Height - BaseMargin)
  '  Calculate length of baseline drawn by the code above
  Dim BaseLineLength As Integer = _
     PBBarChart.Width - (LeftMargin + RightMargin)

 5b.  The Bars
     Calculating the width of each bar again is simple math: Divide the length of the baseline by the number of bars (which is the same as the number of elements in the SalesData arraylist – one per country).  Subtract the width you decided earlier to insert between bars.  

   Dim BarWidth As Double = (BaseLineLength / SalesData.Count) - BarGap

    Each bar is of course rectangular, so we will create a Rectangle object for this purpose.   To locate the  X-position of the first bar we start at the intersection of the x and y axis and then move to the right a distance equal to the width we decided to insert between bars.      The Y-position is set one pixel above the baseline:

  Dim BarRect As Rectangle
  Dim BarStartX As Integer = LeftMargin + BarGap
  Dim BarStartY As Integer = PBBarChart.Height - (BaseMargin + 1)

  Next, create a brush with which to draw and fill the bar rectangles:

   Dim BarBrush As New SolidBrush(Color.BurlyWood)

  So much for the width.  Now we need to consider the heights of the bars.  Obviously the height of each bar will represent the sales figure for a country.   We have already configured the vertical axis so that it is marked out to a maximum of 1000 units of sales values. 

   The vertical axis also has another value – the number of pixels that make up its length.      In order to ensure that each bar’s height is drawn proportionately to the total pixels available we must calculate the scale, i.e. how many Sales are represented by a single pixel up that vertical line.    This measurement will be used to ensure that the bars are drawn proportionately within the overall height available for them.

   This may sound complicated.  It really isn’t and the calculation itself is very simple:

    Dim VertScale As Double
   VertScale = VertLineLength / 1000
  

   By enumerating through the information stored in the arraylist, we can pull out the sales figures one after the other.   We use this figure to calculate the height of the bar .
  
  There are several ways of approaching the task of drawing the bars in the correct positions, some easier than others.    The approach I have taken for this article is as follows.  For each bar:-

  • Calculate the Bar’s height.
  • Create a rectangle to represent the Bar.
  • “Pull” the Y-position upwards until it reaches to place where the top left of the rectangular bar should be.   Why do we have to do this?  Because the rectangle is drawn “outwards and downwards”, not “outwards and upwards”.  If we don’t make this change, the bars will be drawn below the baseline, not above it.
  • Draw and Fill the Rectangle to create the bar
  • Move the X-Position by the calculated amount to the right.

   And here is the code which does exactly that:

  For Each gd As GraphData In SalesData
    '  Calculate Bar Height
    Dim BarHeight As Integer = CInt(gd.Sales * VertScale)
    '  Create a rectangle for the Bar
    BarRect = New Rectangle(BarStartX, BarStartY, CInt(BarWidth), _
          BarHeight)
    '  Pull the Y point upwards so that the bar (rectangle) will
    '  stretch back down to the baseline when drawn

    BarRect.Offset(0, -BarHeight)
    '  Fill the Bar
    g.FillRectangle(BarBrush, BarRect)
    '  Optionally draw a line round the bar
    g.DrawRectangle(LinePen, BarRect)
    '  Increase the X value by bar width plus gap
    '  ready for next bar to be drawn.
    BarStartX += CInt(BarWidth + BarGap)
 Next

5c.   Country Names

  The names of the countries are displayed at the bottom of each bar.   In a later article we will investigate more challenging and pictorial ways of doing this but for now simple text using DrawString will suffice.

  As the process is similar to those we have used already in this project, the commented code will, I hope, make sense to you at this stage:
 
   '  Set the start point of the first string
   Dim TextStartX As Integer = LeftMargin + BarGap + 4

   '  Create a Brush to draw the text
   Dim TextBrsh As Brush = New SolidBrush(Color.Black)
   '  Create a Font object instance for text display
   Dim TextFont As New Font("Arial", 11, FontStyle.Bold)

   For Each gd As GraphData In SalesData
       '  Draw the name of the country
       g.DrawString(gd.Country, TextFont, TextBrsh, TextStartX, _
          CInt(PBBarChart.Height - (BaseMargin - 4)))  
        '  Move start point along to the right
        TextStartX += CInt(BarWidth + BarGap)
   Next
 

Comments    Submit Comment

Comment #1  (Posted by an unknown user on 03/14/2005)
Rating
Part one is very good.

Where is part two?
 
Comment #2  (Posted by Ged Mead on 03/16/2005)
Rating
Slight technical hitch there :-}
Part 2 now published. Hope you find it useful.
 
Comment #3  (Posted by an unknown user on 04/18/2005)
Rating
Great Article!
 
Comment #4  (Posted by an unknown user on 05/01/2005)
Rating
Keep up the great work.. I'm at Virginia Tech (Go Hokies).. doing my montrous VB.net final project and this has helped me a bit. Thank you so much. You might wanna add a small blurb for newbies to know how to change the scale. Other than that...A++

 
Comment #5  (Posted by Ged Mead on 05/01/2005)
Rating
Thanks - Scaling will be included in the upcoming Part 4 of the series (Back to the Bar). Also 3D bars and multiple colors.
 
Comment #6  (Posted by an unknown user on 06/15/2005)
Rating
This walk-through is absolutely perfect. I've been looking to draw a simple chart on my project for weeks now, and this was the ONLY comprehensive explaination I've found! Thank You!!!
 
Comment #7  (Posted by an unknown user on 06/15/2005)
Rating
This walk-through is absolutely perfect. I've been looking to draw a simple chart on my project for weeks now, and this was the ONLY comprehensive explaination I've found! Thank You!!!
 
Comment #8  (Posted by an unknown user on 07/26/2005)
Rating
Ged, I can't thank you enough for taking the time to make something like this available to the on-line community. I am in the process of teaching my self vb.net and your tutorial has taught me far more then I ever expected to learn. Once again many thanks!
 
Comment #9  (Posted by an unknown user on 12/08/2005)
Rating
Clearly explained.
 
Comment #10  (Posted by an unknown user on 01/18/2006)
Rating
This is really very nice article explained in a detailed way..

Thanks for providing such a great article.
 
Comment #11  (Posted by an unknown user on 01/30/2006)
Rating
excellent learning tutorial on working with graphics. Explanations for each steps great.
 
Comment #12  (Posted by an unknown user on 02/07/2006)
Rating
Superb article !! helped me a lot in my project
 
Comment #13  (Posted by Samer on 07/01/2006)
Rating
Thanks for ALL the articles. They helped me alot. Can you please write an article about drawing objects (Lines, Rectangles, Circles, ...etc) and then using the mouse to select the object and change their attributes (color, size, position, delete...etc)
 
Comment #14  (Posted by Ankur Adarsh on 07/31/2006)
Rating
this is good but i do not understand how to use picture box in web programming, because i can not see picture box control in web form.
 
Comment #15  (Posted by an unknown user on 10/19/2006)
Rating
Very good article. Prakash Bajaj
 
Comment #16  (Posted by Roy Oliver on 12/04/2006)
Rating
Thanks for the lessons Ged. Knowing how to manipulate graphics was the only thing missing from my skill set.
 
Comment #17  (Posted by an unknown user on 01/20/2007)
Rating
hey dude i love this and i'm going to pass my visual basic test with flying colours..love you keep going dude
 
Comment #18  (Posted by an unknown user on 08/24/2007)
Rating
very simple
 
Comment #19  (Posted by an unknown user on 01/03/2008)
Rating
thank you so much. i need it very much.
 
Comment #20  (Posted by shohreh on 01/03/2008)
Rating
it was excellent.
 
Sponsored Links