Display The Months
To be useful and usable we need to show the names of the months that the sales figures represent. To do this we employ the DrawString method in the same way that we have done in previous articles. That is, we "write" the text by drawing it with a brush. It's an odd way of going about the task of writing to the human mind, but perfectly logical to a computer; letters of the alphabet are after all just shapes - drawings that we happen to be able to translate in our heads into something meaningful.
First, we fix the start point for the first letter of the first word. This is 18 pixels in from the left margin. Then we create a Brush to draw with and also set out the details of the Font that we want to be used.
' Set the start point of the first string
Dim TextStartX As Integer = CInt(LeftMargin + 18)
' 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", 10, FontStyle.Regular)
We're all set now and can go ahead and draw the month names. Those names were stored in the Months array right at the start of this project, you may remember:
For i As Integer = 0 To Months.Length - 1
' Draw the name of the month
g.DrawString(Months(i), TextFont, TextBrsh, TextStartX, _
CInt(PBLineChart.Height - (BaseMargin - 4)))
' Move start point for next name along to the right
TextStartX += CInt(LineWidth)
Now that we have finished with the Brush and the Font objects we should dispose of them as this is good housekeeping practice. It's not critical in a tiny project like this one, but is a good habit to get into.
Display The Result
As we did at the end of the previous pages, we can now display the results of our efforts. However, as we are no longer working with a temporary display, we will create a small procedure that
- Displays the finished chart
- Disposes of the Graphics Object
Here is the procedure:
Private Sub FinalDisplay()
PBLineChart.Image = bmap
And finally we can list the procedures we need to call in turn from the button click event:
Private Sub btnDraw_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDraw.Click
All done .... or are we?
The chart above is OK, but not much more than that. How much better it would be if the joins at the line segments were more clearly marked. And if there were grid lines in the body of the chart to make it easier to see the values on the vertical axis. Shall we add these improvements to the chart?