Article Options
Premium Sponsor
Premium Sponsor

 »  Home  »  .NET Framework  »  Chart Success : GDI+ Graphics At Work Part 4  »   Draw Vertical Axis
 »  Home  »  Windows Development  »  Graphics  »  Chart Success : GDI+ Graphics At Work Part 4  »   Draw Vertical Axis
Chart Success : GDI+ Graphics At Work Part 4
by Ged Mead | Published  05/10/2005 | .NET Framework Graphics | Rating:
Draw Vertical Axis

Graphics Object and Bitmap
Next, we set up Graphics and Bitmap objects.   Put this initialising code in a separate procedure named GetGraphics:


Private Function GetGraphics() As Graphics
        '  Make bmap the same size and resolution as the PictureBox
        bmap = New Bitmap(PBBarChart.Width, PBBarChart.Height, PBBarChart.CreateGraphics)
        '   Assign the Bitmap object to the Graphics object
        '   and return it
        Return Graphics.FromImage(bmap)
    End Function


Vertical Axis
  If you have read the previous articles, you may notice that the various stages are now being broken up into smaller code chunks and put into separate procedures.

   The code for the vertical axis is now placed in its own procedure and remains largely unchanged from previous versions.  The first part looks like this:


Private Sub DrawVerticalAxis(ByVal g As Graphics)
'   Draw a line for the Vertical Axis.
        Dim StartPoint As New Point(LeftMargin, PBBarChart.Height - BaseMargin)
        Dim EndPoint As New Point(LeftMargin, TopMargin)
        '  Basic Pen
        Dim LinePen As New Pen(Color.Black, 2)
        '  Draw the vertical line (without tick marks)
        g.DrawLine(LinePen, StartPoint, EndPoint)

        '  Draw the Tickmarks and Display Numbers
        '   Calculate length of the vertical axis
        VertLineLength = PBBarChart.Height - (BaseMargin + TopMargin)

'  Identify the highest sales figure
        For Each gd As GraphData In SalesData
            If gd.Sales > HighSale Then HighSale = gd.Sales
        Next
' :  Scaled Tick Marks code follows


Scaled Tick Marks
   Here’s a change from the earlier article.    In Part 2,  we fixed the maximum number of sales at a figure of 1000.  Not very realistic, but helped to keep the code less complicated.    This time round we will adjust the maximum sales figure (and therefore the number and the spacing of tick marks) in accordance with whatever value is held in the HighSale variable above.

  This means that you can change the sales data to  allow a total sales value of more than 1000 at any time in the future and your graph won’t become distorted.

   To do this, we identify the highest sales figure, then round up until we get to the next round value of 100.  For example,  if the HighSale figure is 1675 then the maximum value on the scale will be set to 1700.

  Here is the code that does this:

' DrawVerticalAxis procedure continued:
     ' Round up to next hundred above highest sales figure
        Dim NextCent As Integer = CInt(HighSale)
        Do While NextCent Mod 100 <> 0
            NextCent += 1
        Loop

   '  Identify how many TickMarks required (one per hundred):
        Dim TotalTicks As Integer = CInt(NextCent / 100)

  Now we know how many hundreds we have to allow for, we can divide the vertical axis proportionately into 100s, draw the tick marks and the values as text, just as we did in Part 2:

' Calc gaps between vertical tick marks
        Dim YPos As Integer = CInt(VertLineLength / TotalTicks)
        '  Variables for Start and End Points of Tick Marks
        Dim TickSP As New Point(LeftMargin - 5, StartPoint.Y - YPos)
        Dim TickEP As New Point(LeftMargin, StartPoint.Y - YPos)
        '  Font for values  - declared here for readability
        Dim ValueFont As New Font("Arial", 8, FontStyle.Regular)

        For i As Integer = 1 To TotalTicks
            g.DrawLine(New Pen(Color.Black), TickSP, TickEP)   '  Tick mark
            '  Tick Values as text :
            g.DrawString(CStr(i * 100), ValueFont, Brushes.Black, 2, TickSP.Y - 5)
            '  Resetx, y positions, proportionately up vertical line
            TickSP.Y = CInt(TickSP.Y - (VertLineLength / TotalTicks))
            TickEP.Y -= CInt(VertLineLength / TotalTicks)
        Next
End Sub

 

Sponsored Links