Step 6: All is Revealed!
You may already have inserted this next code line if you elected to “try as you go” to see your chart building up. Just in case you didn’t, though, this is the time and place to do so.
It doesn’t seem much, but it’s actually a key line in the process. This takes the Bitmap object on which you have drawn your chart and finally shows it to the user. It does this by assigning the Bitmap to the Image property of the PictureBox.
' Assign the chart drawing bitmap as the image for the picturebox
PBBarChart.Image = bmap
As we learned in Part 1, you should dispose of any disposable objects once they are finished with. This code deals with that task:
' Dispose of drawing objects
g.Dispose()
LinePen.Dispose()
BarBrush.Dispose()
ValueFont.Dispose()
TextFont.Dispose()
End Sub
Again, if you haven't been testing the code while reading, then you will need to add the code for the button click event:
Private Sub btnDraw_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDraw.Click
' Generate the Data
GetData()
' Then Draw the Chart
DrawChart()
End Sub
And there you have your first, basic but working, bar chart.