カテゴリの円グラフをパーセンテージで表示したいと考えています。
カテゴリのパーセンテージの円グラフを作成するにはどうすればよいですか?
drawRect:
メソッドをオーバーライドしてパイを自分で描画するクラスを実装する必要があります。UIBezierPathクラスを使用します。具体的にはaddArcWithCenter:radius:startAngle:endAngle:clockwise:メソッドを調べて、円の一部を描画します。
これは、他人のコードのように見える場合、それは私が Web サイトでそれを行う方法を見つけたからであるという警告と、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;
}
他の回答で提案されているライブラリに加えて、私が使用した 2 つの非常に優れたオープン ソースの円グラフ クラスがあります。それらは非常に見栄えがよく、非常にシンプルです。GitHub でそれらを見てください。