Article Options
Premium Sponsor
Premium Sponsor

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

  Just to finish it off, the names of the countries are written below each bar.

  This is very similar to what we did in Part 2, with the addition of a code line that adjusts the font size according to the width available.

   Private Sub WriteTheNames()
        '  X position for start of country name(s).  It is placed
        '  under the left edge of the bar, plus 5 pixels for better look
        Dim TextStartX As Integer = LeftMargin + BarGap + 5

        '  Create a Brush to draw the text
        Dim TextBrsh As Brush = New SolidBrush(Color.Black)
        '  Create a Font object instance for text display
        '  dynamically adjusted font size would be useful:
        Dim fntSize As Integer = CInt(BarWidth / 7)
        Dim TextFont As New Font("Verdana", fntSize, FontStyle.Bold)
        '   Write them:
        For Each gd As GraphData In SalesData
            g.DrawString(gd.Country, TextFont, TextBrsh, TextStartX, CInt(PBBarChart.Height - (BaseMargin - 4)))
            TextStartX += CInt(BarWidth + BarGap)
    End Sub

Again, you will need to add a call to this procedure in the Draw Button’s click event.  The complete code for that event would therefore look like this:

  Private Sub btnDraw_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDraw.Click
        '  The various actions are now split into separate procedures to make
        '  the project more modular

        '  1.  Get the data
        '  2.  Get a Graphics object
        g = GetGraphics()
        '  3.   Draw the Vertical Axis
        '  4.  Draw bars
        '  5.  Write the titles
        ' All Done!   Assign the bitmap with the vertical axis drawn to the picturebox
        pbBarChart.Image = bmap
    End Sub

Sponsored Links