How can I
#1 explode a piece of a pie chart?
#2 Write text inside a piece of a pie chart?
I'm using MSchart and VB6
Thanks.
PS
Is there any source code out there to do
these without MSchart. That is using the Circle
statement.
I can make the pie chart using the circle but need to know how to
explode/write text.

Dani AI

Generated

As explained, “explode” means pulling one wedge a little away from the circle; correctly pointed out that many chart engines provide an “exploded pie” chart type. Two practical routes in VB6: use a chart control that exposes an explode/explosion property (many modern chart libraries and Excel/NET chart examples do), or draw the pie yourself with VB6 graphics (the reliable approach when the control lacks the feature). (stackoverflow.com)

If you draw the pie yourself use the PictureBox (or Form) Circle method: compute each slice’s start/end angles (radians), find the slice mid-angle, and shift that slice’s center by (dx,dy) = (Cos(mid)offset, -Sin(mid)offset) before drawing the wedge. VB6’s Circle expects negative start/end angles to draw radii (and a tiny negative value instead of 0 to get a radius to 0), so handle 0 and >180° slices per the Circle rules. Place the label at a fraction of the radius along the mid-angle and use CurrentX/CurrentY + Print (or TextWidth/TextHeight) to center the text. The Circle method behavior and angle rules are documented and the negative-angle trick is commonly used. (vb-helper.com)

A compact VB6 example (adapt and expand for edge cases):

' DrawPieTo: pic = PictureBox, vals(), labels(), colors() arrays
Private Sub DrawPieTo(pic As PictureBox, vals() As Double, labels() As String, colors() As Long, _
                      Optional ByVal explodeIdx As Long = -1, Optional ByVal explodeOffset As Single = 20)
    Const PI = 3.14159265358979
    Dim i As Long, total As Double, ang As Double
    Dim cx As Single, cy As Single, r As Single
    Dim startA As Double, endA As Double, midA As Double
    Dim ex As Single, ey As Single, sArg As Double, eArg As Double

    pic.AutoRedraw = True
    pic.Cls
    For i = LBound(vals) To UBound(vals): total = total + vals(i): Next i
    cx = pic.ScaleWidth / 2: cy = pic.ScaleHeight / 2
    r = (IIf(pic.ScaleWidth < pic.ScaleHeight, pic.ScaleWidth, pic.ScaleHeight) * 0.4)

    startA = 0
    For i = LBound(vals) To UBound(vals)
        ang = vals(i) / total * 2 * PI
        endA = startA + ang
        midA = (startA + endA) / 2

        If i = explodeIdx Then
            ex = Cos(midA) * explodeOffset
            ey = -Sin(midA) * explodeOffset
        Else
            ex = 0: ey = 0
        End If

        pic.FillStyle = vbFSSolid
        pic.FillColor = colors(i)

        ' prepare Circle args (use negative angles; swap order for >180° slices if needed)
        sArg = -startA: eArg = -endA
        If sArg = 0 Then sArg = -0.000001
        If eArg = 0 Then eArg = -0.000001
        If ang > PI Then Swap sArg, eArg  ' simple rule; wrap cases need extra handling

        pic.Circle (cx + ex, cy + ey), r, vbBlack, sArg, eArg

        ' label centered inside slice
        Dim tx As Single, ty As Single, txt As String
        txt = labels(i)
        tx = cx + ex + Cos(midA) * (r * 0.6)
        ty = cy + ey - Sin(midA) * (r * 0.6)
        pic.CurrentX = tx - pic.TextWidth(txt) / 2
        pic.CurrentY = ty - pic.TextHeight(txt) / 2
        pic.ForeColor = vbBlack
        pic.Print txt

        startA = endA
    Next i
End Sub

Notes and troubleshooting: set AutoRedraw = True to avoid flicker; the small negative-angle hack avoids the “zero angle” problem; slices that span more than 180° or wrap past 2π need special care (swap arguments or split into two arcs) — the Circle method quirks are documented. If you want true 3D exploded shading or interactive explode-distance control, either use a charting library that supports exploded points or draw with the Win32 Pie API / GDI for more control. Example manual/explode implementations and transform-based approaches exist in community samples. (vb-helper.com)

Summary: try to find an MSChart/third-party control that already supports exploded/doughnut slices (fast), but for full control — labels, explosion distance, fonts — draw the pie with Circle or the GDI Pie API and compute offsets/label positions with Cos/Sin as shown above.

Recommended Answers

All 3 Replies

What exactly you mean by explode a piece of a pie chart ?

Imagine you're at the Pizza Store and a fresh pie is taken out of the oven. You say "one piece,please." You watch as one piece is cut (exploded/removed). The piece is not in the pie anymore but just a bit outside.

That type of chart is displayed if the chart type is Exploded pie 3d chart. I am not sure if the chart control inbuilt to Vb 6.0 has that type support.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.