ComponentOne VSView 8.0
Step 2: Drawing Pie Charts

To draw a pie chart, we will start with some random data, and then call a general-purpose pie drawing routine. Double-click on the Pie Chart button and add the following code to the cmdPie_Click event handler:

Example Title
Copy Code
Private Sub cmdPie_Click()

 

  ' set up chart data in variant array:

  ' col 0 has values, col 1 has labels, and

  ' col 2 has colors

  ' negative values create exploded slices

  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"

  DrawPieChart vd, cd, _

       "Expense Report - March 1999" & vbCrLf & _

       "(values in 1,000 US$)"

 

End Sub

The routine sets up a variant array with some values, labels and colors, and then calls the DrawPieChart routine.

DrawPieChart is a generic pie-chart routine for the VSDraw control. It is not trivial code, but it is not very difficult either. You should be able to understand what it does and to customize it without trouble.

Here is the DrawPieChart routine (it is pretty long, so we suggest you copy it to the clipboard and paste into your project rather than typing it):

Example Title
Copy Code
Sub DrawPieChart(vd As VSDraw, _

                 ByRef ChartData(), _

                 tit$)

  Const Pi = 3.1416

  Const Radius = 500

  Dim i%, arc#, start#, tot#, a#, cx#, cy#


  With vd


  '---------------------------------------------

  ' clear control

  .Clear

  '---------------------------------------------

  ' setup scale based on pie radius

  ' use negative ScaleHeight so positive is up

  .ScaleWidth = 2.75 * Radius

  .ScaleLeft = 0

  .ScaleHeight = -.ScaleWidth

  .ScaleTop = .ScaleWidth

  '---------------------------------------------

  ' calculate total value to scale pie slices

  For i = 0 To UBound(ChartData, 2)

    tot = tot + Abs(ChartData(0, i))

  Next

  '---------------------------------------------

  ' set up to draw pie slices

  .PenColor = vbBlack

  .PenWidth = 0

  .PenStyle = psSolid

  .BrushStyle = bsSolid

  '---------------------------------------------

  ' draw shadow slices in gray

  .BrushColor = RGB(90, 90, 90)

  For i = 0 To UBound(ChartData, 2)

    ' calculate arc

    arc = Abs(ChartData(0, i)) / tot * 2 * Pi

    a = start + arc / 2

    ' get slice origin

    cx = .ScaleWidth / 2 + Radius / 20

    cy = .ScaleWidth / 2 + Radius / 20

    ' explode slices with negative amounts

    If ChartData(0, i) < 0 Then

      cx = cx + Radius / 10 * Cos(a)

      cy = cy + Radius / 10 * Sin(a)

    End If

    ' draw slice

    If arc > 0 Then

      .DrawCircle cx, cy, Radius, start, start + arc

    End If

    ' update start

    start = start + arc

  Next

  '---------------------------------------------

  ' draw real slices in given colors

  start = 0

  For i = 0 To UBound(ChartData, 2)

    ' set color

    .BrushColor = ChartData(2, i)

    ' calculate arc

    arc = Abs(ChartData(0, i)) / tot * 2 * Pi

    a = start + arc / 2

    ' get slice origin

    cx = .ScaleWidth / 2

    cy = .ScaleWidth / 2

    ' explode slices with negative amounts

    If ChartData(0, i) < 0 Then

      cx = cx + Radius / 10 * Cos(a)

      cy = cy + Radius / 10 * Sin(a)

    End If

    ' draw slice

    If arc > 0 Then

      .DrawCircle cx, cy, Radius, start, start + arc

    End If

    ' update start

    start = start + arc

  Next

  '---------------------------------------------

  ' set up to draw slice labels

  .TextAlign = tadCenter

  .TextColor = vbWhite

  .FontBold = True

  .FontSize = Radius / 10

  '---------------------------------------------

  ' draw slice labels

  start = 0

  For i = 0 To UBound(ChartData, 2)

    ' calculate arc

    arc = Abs(ChartData(0, i)) / tot * 2 * Pi

    a = start + arc / 2

    ' get slice origin

    cx = .ScaleWidth / 2

    cy = .ScaleWidth / 2

    ' explode slices with negative amounts

    If ChartData(0, i) < 0 Then

      cx = cx + Radius / 10 * Cos(a)

      cy = cy + Radius / 10 * Sin(a)

    End If

    ' draw label

    If arc > 0 Then

      a = start + arc / 2

      .X1 = cx + Radius * 2 / 3 * Cos(a)

      .Y1 = cy + Radius * 2 / 3 * Sin(a) + .FontSize

      .Text = ChartData(1, i) & vbCrLf & _

              Abs(ChartData(0, i))

    End If

    ' update start

    start = start + arc

  Next

  '---------------------------------------------

  ' draw chart title

  .X1 = .ScaleWidth / 2

  .Y1 = .ScaleTop + .ScaleHeight + .FontSize * 2.5

  .TextColor = vbBlack

  .Text = tit

  End With

End Sub

 

 


Copyright (c) GrapeCity, inc. All rights reserved.

Product Support Forum  |  Documentation Feedback