如何在iPhone中创建饼图?

如何在iPhone中创建饼图?,iphone,pie-chart,Iphone,Pie Chart,我想按百分比显示我的类别的饼图 如何创建类别百分比的饼图 您必须实现一个重写drawRect:方法的类,然后自己绘制饼图。您将使用类,特别是研究绘制圆的一部分的方法 另请参见和。如果它看起来像其他人的代码,这可能会有帮助,因为我已经解决了如何在网站上使用它,另外还有一个警告,即我在启动iPhone后不久就这样做了。欢迎那些想告诉我哪些比特是无效的或错误的人,我还在学习 static inline float radians(double degrees) { return degrees * M

我想按百分比显示我的类别的饼图


如何创建类别百分比的饼图

您必须实现一个重写drawRect:方法的类,然后自己绘制饼图。您将使用类,特别是研究绘制圆的一部分的方法


另请参见和。

如果它看起来像其他人的代码,这可能会有帮助,因为我已经解决了如何在网站上使用它,另外还有一个警告,即我在启动iPhone后不久就这样做了。欢迎那些想告诉我哪些比特是无效的或错误的人,我还在学习

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

有一个API,你可以做这件事

有效但不方便的图书馆。
除了其他答案中建议的库之外,我还使用了两个非常好的开源饼图类。它们看起来非常漂亮,非常简单,请在GitHub上查看:

您可以参考我的答案,其中只包含一行代码来绘制饼图