0

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

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

4

4 回答 4

4

您必须实现一个覆盖该drawRect:方法的类并自己绘制饼图。您将使用UIBezierPath类,特别是查看addArcWithCenter:radius:startAngle:endAngle:itarian:方法来绘制圆的一部分。

另见这篇文章这篇文章

于 2011-01-22T07:57:53.683 回答
3

有API,你可以做这件事。

  1. MIMChart
  2. CorePlot有效但不方便的库。
于 2012-02-17T10:28:16.673 回答
1

这可能有助于警告如果它看起来像其他人的代码,那是因为我制定了如何在网站上执行此操作,以及我在启动 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;
}
于 2011-01-22T08:09:35.850 回答
0

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

于 2012-08-20T10:48:40.863 回答