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)
Next
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
GetData()
' 2. Get a Graphics object
g = GetGraphics()
' 3. Draw the Vertical Axis
DrawVerticalAxis(g)
' 4. Draw bars
Draw3DBars()
' 5. Write the titles
WriteTheNames()
' All Done! Assign the bitmap with the vertical axis drawn to the picturebox
pbBarChart.Image = bmap
g.Dispose()
End Sub