Reputation: 1
I am developing an application that has a Pie Chart in it. I want to display the values (used for making the pie chart) to be displayed in the center corresponding area.
I am using this code to draw the PieChart:
CGContextSetRGBFillColor(ctx, 0.52, 0.63, 0.31, 1.0);
CGContextMoveToPoint(ctx, posX, posY);
CGContextAddArc(ctx, posX, posY, r,(startDeg)*M_PI/180.0, (endDeg)*M_PI/180.0, 0);
CGContextClosePath(ctx);
CGContextFillPath(ctx);
I can add the labels on top of pie chart but I am not able to position the label in the center of the area.
Upvotes: 0
Views: 980
Reputation: 29
Displaying label for pie chart requires a little bit math. HUChart is to draw half pie chart but I think you will find some useful information at there.
Upvotes: 0
Reputation: 2146
The "center" of the area is sort of a poorly defined concept when you're talking about a pie slice. Here's what I would do:
Take an angle halfway between startDeg
and endDeg
along with a radius about 1/2 to 2/3 of r
and plop the center of your label there. A polar to rect coordinate conversion is necessary, which if you have forgotten is:
x = r * cos(angle);
y = r * sin(angle);
Upvotes: 1