开发者

How to display labels on a Pie Chart

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 开发者_如何学Carea.

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.


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);


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜