How to create pie chart in iPhone? [closed]

不打扰是莪最后的温柔 提交于 2019-11-27 17:19:25

问题


I want to show pie chart of my category by percentage.

How can I create pie chart of category percentage ?


回答1:


There is API by that you can do this thing.

  1. MIMChart
  2. CorePlot Effective but not handy library.



回答2:


You have to implement a class which overrides the drawRect: method and draw the pie yourself. You'd use UIBezierPath class, specifically look into the addArcWithCenter:radius:startAngle:endAngle:clockwise: method to draw a part of a circle.

See also this article and this article.




回答3:


This might be helpful with the warning that if it looks like someone else's code, it's because I worked out how to do it with web sites, and the additional warning that I did this not long after starting iPhone. People who want to tell me which bits are inefficient or wrong are welcome, I'm still learning.

static inline float radians(double degrees) { return degrees * M_PI / 180; }

// making a simple pac man shape
- (UIImage*)getImage {

    UIImage* image;
    if(self.completion == 100.0f) {
        image = [UIImage imageNamed:@"completedTaskIcon.png"];
    } else {
        UIGraphicsBeginImageContext(CGSizeMake(SIDELENGTH, SIDELENGTH));

        CGContextRef context = UIGraphicsGetCurrentContext();

        // the image should have a clear background
        [[UIColor clearColor] set];
        CGRect myRect = CGRectMake(0.0f, 0.0f, SIDELENGTH, SIDELENGTH);
        UIRectFill(myRect);

        // color was hopefully set before this method called
        [self.color set];

        // centre point is required
        CGFloat midx = SIDELENGTH/2;
        CGFloat midy = SIDELENGTH/2;
        // radius of the arc
        CGFloat radius = (SIDELENGTH/2) * 0.60f;

        // pie background
        CGContextSetFillColor(context, CGColorGetComponents([[UIColor orangeColor] CGColor]));
        CGContextBeginPath(context);
        CGContextMoveToPoint(context, midx + radius, midy);
        CGContextAddArc(context, midx, midy, radius,  radians(0), radians(360), 0); 
        CGContextFillPath(context); 

        // pie segment
        CGContextSetFillColor(context, CGColorGetComponents([[UIColor blueColor] CGColor]));
        CGContextBeginPath(context);
        CGContextMoveToPoint(context, midx, midy);
        CGContextAddLineToPoint(context, midx + radius, midy);
        CGContextAddArc(context, midx, midy, radius,  radians(0), radians(360 * (self.completion / 100.0f)), 0); 
        CGContextFillPath(context); 
        image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    }

    return image;
}



回答4:


Besides the proposed library in the other answers, there are two really good open source pie chart classes that I used. They look very nice, are extremely simple, have a look at them at GitHub:

  • XYPieChart

  • NBPieChart



来源:https://stackoverflow.com/questions/4766766/how-to-create-pie-chart-in-iphone

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