How to display Text on a PieChart?

丶灬走出姿态 提交于 2019-12-06 13:41:05

You can draw a string with NSString drawInRect during the drawRect method. For example:

[percentageString drawInRect:CGRectMake(100, 100, 30, 20) 
                    withFont:font 
               lineBreakMode:UILineBreakModeClip 
                   alignment:UITextAlignmentLeft];

Update

Here is how to find the x, y for placing text:

CGFloat startDegree = 180;                             // start at 180
CGFloat endDegree = degree;                            // end at some value

CGFloat mid = (endDegree + startDegree) / 2;           // find midpoint
CGFloat rad = DEGREES_TO_RADIANS(mid);                 // convert to radians


// center is center of pie chart
// radius is how big the pie chart is
// 30 is extra to draw text outside circle

CGFloat x =  center.x + ((radius + 30) * cos(rad));    
CGFloat y = center.y + ((radius + 30) * sin(rad));

NSLog(@"X Y: %f %f %f", x, y, degree);

NSString *text = @"Test";   
[text drawInRect:CGRectMake(x, y, 50, 44) withFont:[UIFont systemFontOfSize:14.0f]];

I had a sample pie chart so I used that. It has a slider with degrees. In the example, I can draw a segment of the pie chart from 180 degrees to 360 degrees.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!