Labeling Pie Charts

RTSwimmer

New member
Joined
Nov 7, 2006
Messages
2
Location
Portsmouth, UK
Programming Experience
1-3
Is it possible to find the center of the outside edge on a pie slice?

At the moment i have a static label showing the values in the key but i would like to get the values next to the corisponding slice.
Like this: http://www.chestysoft.com/images/pieexample.gif

Here is my code which creates a pie chart with 3 sections
pie1, pie2 & pie3's values get sent from a main form.
VB.NET:
        formGraphics = Me.CreateGraphics()
        Dim curPos As Integer = -90 '12 O'clock
        Dim curRect As New System.Drawing.Rectangle(20, 20, 200, 200) 'position of circle
        Dim piePen As New System.Drawing.Pen(System.Drawing.Color.Black)
        Dim pieBrush As New System.Drawing.SolidBrush(System.Drawing.Color.Blue)
        Dim thisForm As System.Windows.Forms.Form = frmPieChart.ActiveForm
        Dim pieFill As New System.Drawing.SolidBrush(Color.Magenta) 'first pie
        Dim figValue As New System.Drawing.Point
        Dim total As Double = pie1 + pie2 + pie3 'Total of all values
        Dim pie1size As Double = pie1 / total * 360  
        Dim pie2Size As Double = pie2 / total * 360 
        Dim pie3Size As Double = pie3 / total * 360 
        Dim pie1Perc As Double = pie1 / total * 100
        Dim pie2Perc As Double = pie2 / total * 100
        Dim pie3Perc As Double = pie3 / total * 100

        'slice 1
        lblFig1Val.Text = FormatNumber(pie1Perc, 0) & "%"
        formGraphics.FillPie(pieFill, curRect, curPos, pie1size)
        formGraphics.DrawPie(piePen, curRect, curPos, pie1size)

        'slice 2
        lblFig2Val.Text = FormatNumber(pie2Perc, 0) & "%"
        pieFill.Color = Color.Lime 'reset colour
        formGraphics.FillPie(pieFill, curRect, curPos + pie1size, pie2Size)
        formGraphics.DrawPie(piePen, curRect, curPos + pie1size, pie2Size)

        'slice 3
        lblFig3Val.Text = FormatNumber(pie3Perc, 0) & "%" 'outputs the value of stack 1
        pieFill.Color = Color.Blue  'reset colour
        formGraphics.FillPie(pieFill, curRect, curPos + pie1size + pie2Size, pie3Size)
        formGraphics.DrawPie(piePen, curRect, curPos + pie1size + pie2Size, pie3Size)

        'tidy up
        piePen.Dispose()
        pieFill.Dispose()
Can anyone help?

Cheers RTSwimmer
 
How are your quadratic equations? Finding the center of an arc is not trivial even in complex math terms.

Given: Arc AB, A(x1,y1), B(x2,y2), and radius r, find the center of the circle.

Solution: Draw two circles of radius r, one centered at A and the other centered at B. The points of intersection are the centers of the two circles of radius r that contain points A and B.

For easier calculations, we will make a new coordinate system translated x1 units to the right and y1 units up, and we will let a=x2-x1, b=y2-y1, so now
A(0,0) and B(a,b) are the two points in our new coordinate system. We'll add back (x1,y1) to the result at the end.

The two equations are:

x2+y2=r2
(x-a)2+(y-b)2=r2

Subtracting the second from the first,

2ax-a2+2by-b2=0
2ax=a2+b2-2by
x=(a2+b2-2by)/(2a)

Substituting this in place of x in the first equation,
((a2+b2-2by)/(2a))2+y2=r2
a2/4 + b2/2 + b4/(4a2) - r2 - by - (b3y)/a2 + y2 + (b2y2)/a2 = 0
y2 + (b2y2)/a2 - by - (b3y)/a2 + a2/4 + b2/2 + b4/(4a2) - r2 = 0
y2(1+b2/a2) +y(-b-b3/a2) + a4/(4a2) + 2a2b2/(4a2) + b4/(4a2) - r2 = 0
y2(1+b2/a2) +y(-b-b3/a2) + (a4+2a2b2+b4)/(4a2) - r2 = 0

Now we have a quadratic in y, so (I'll save you the algebraic slogging)
y = b/2±(a/2)sqrt(4r2/(a2+b2)-1)

Now, having solved the equations for y in terms of a, b, and r, we can use the equation for x, above,
x=(a2+b2-2by)/(2a)

and the center of the circle, then, is (x+x1, y+y1)


Frankly, i'd just do a little calculation and give it a best guess approach. I you put your pie in a graphics path object you can use the pathpoints property to go through, well all the points in the path. That maybe of help here. I'd really have to try it to say for sure though.
 
Back
Top