Tutorials > VSDraw Tutorial - Chart > Step 3: Drawing Bar Charts |
Drawing bar charts is very similar to drawing pie charts. We will use exactly the same data structure and organization and add a new routine to draw the bar chart. Double-click on the Bar Chart button and add the following code to the cmdBar_Click event handler:
Example Title |
Copy Code
|
---|---|
Private Sub cmdBar_Click() ' set up chart data in variant array: ' col 0 has values, col 1 has labels, and ' col 2 has colors Dim cd(2, 6) cd(0, 0) = 30: cd(1, 0) = "Phone" cd(0, 1) = 65: cd(1, 1) = "Payroll" cd(0, 2) = 42: cd(1, 2) = "Equipment" cd(0, 3) = 24: cd(1, 3) = "Rent" cd(0, 4) = 52: cd(1, 4) = "Advertising" cd(0, 5) = 12: cd(1, 5) = "Production" cd(0, 6) = 22: cd(1, 6) = "Other" cd(2, 0) = vbRed cd(2, 1) = vbBlue cd(2, 2) = vbGreen cd(2, 3) = vbYellow cd(2, 4) = vbCyan cd(2, 5) = vbMagenta cd(2, 6) = vbBlack ' draw chart vd.FontName = "Tahoma" DrawBarChart vd, cd, _ "Expense Report - March 1999" & vbCrLf & _ "(values in 1,000 US$)" End Sub |
As you can see, this routine is virtually identical to the one we used to draw the pie charts. The only differences are we no longer use negative values, and the drawing routine is DrawBarChart. DrawBarChart is a generic bar-chart routine for the VSDraw control. It is simpler than the pie chart routine, and shows some interesting scaling and text output techniques.
Here is the DrawBarChart routine (again, we suggest you copy it to the clipboard and paste into your project rather than typing it):
Example Title |
Copy Code
|
---|---|
Sub DrawBarChart(vd As VSDraw, _ ByRef ChartData(), _ tit$) Dim i#, max#, barwid#, stp# With vd '--------------------------------------------- ' clear control .Clear '--------------------------------------------- ' setup scale so chart will be scaled to the ' rectangle from (0,0) to (1000,1000) .ScaleWidth = 1300 .ScaleLeft = -200 .ScaleHeight = -1300 .ScaleTop = 1100 .FontSize = 50 .FontBold = True '--------------------------------------------- ' calculate maximum amount for scaling bars For i = 0 To UBound(ChartData, 2) If ChartData(0, i) > max Then max = ChartData(0, i) End If Next '--------------------------------------------- ' draw fat black box around chart .PenColor = vbBlack .PenStyle = psSolid .PenWidth = 5 .BrushColor = BackColor .BrushStyle = bsSolid .DrawRectangle 0, 0, 1000, 1000 '--------------------------------------------- ' draw horizontal grid .FontBold = False .TextAlign = tadRight .PenWidth = 0 .PenStyle = psDot stp = 1 If max > 30 Then stp = 10 If max > 300 Then stp = 100 If max > 3000 Then stp = 1000 If max > 30000 Then stp = 10000 For i = 0 To max Step stp .X1 = -25 .Y1 = 1000 * i / max + .FontSize / 2 .Text = i .Y1 = 1000 * i / max If i > 0 And i < max Then .DrawLine 0, .Y1, 1000, .Y1 End If Next '--------------------------------------------- ' set up to draw bars .PenWidth = 0 .PenStyle = psSolid .BrushStyle = bsSolid barwid = 1000 / 2 / (UBound(ChartData, 2) + 1) '--------------------------------------------- ' draw bars .TextAlign = tadLeft .TextAngle = -900 .TextColor = vbWhite .FontBold = True For i = 0 To UBound(ChartData, 2) ' set color .BrushColor = ChartData(2, i) ' draw bar .X1 = barwid + i / UBound(ChartData, 2) * _ (1000 - 2 * barwid) .DrawRectangle .X1 - barwid / 2, 0, _ .X1 + barwid / 2, _ 1000 * ChartData(0, i) / max ' draw label .X1 = .X1 - .FontSize / 2 .Y1 = barwid / 2 .Text = ChartData(1, i) & _ " (" & ChartData(0, i) & ")" Next '--------------------------------------------- ' draw chart title .TextAlign = tadCenter .TextAngle = 0 .TextColor = vbBlack .FontSize = 50 .X1 = .ScaleLeft + .ScaleWidth / 2 .Y1 = .ScaleTop + .ScaleHeight + 3 * .FontSize .Text = tit End With End Sub |
That's it. The application can already draw pie and bar charts. We're almost done now.