饼图 方法一:
-(void)drawRect:(CGRect)rect
{
[super drawRect:rect];
CGContextRef ctx = UIGraphicsGetCurrentContext();//获取上下文
//可设可不设
// CGContextClearRect(ctx, rect);
// [[UIColor whiteColor] setFill];
// UIRectFill(rect);
CGFloat centerX = self.width/2;//圆心x
CGFloat centerY = self.height/2;//圆心y
CGFloat radius = self.width/3;//半径
CGFloat angle_start = 0; // 开始角
CGFloat angle_end = -M_PI/2; // 结束角
CGFloat angle = 0;//增加的角度
CGFloat red = 0.0;
CGFloat green = 0.0;
CGFloat blue = 0.0;
CGFloat alpha = 0.0;
//数组中保存字典,字典包括比例,和颜色
for (int i =0 ; i<_dataArray.count; i++) {
angle_start = angle_end;
angle = [_dataArray[i][@"data"] floatValue] * M_PI * 2;
angle_end = angle_start + angle;
red = [_dataArray[i][@"color"][@"red"] floatValue] / 255.0;
green = [_dataArray[i][@"color"][@"green"] floatValue] / 255.0;
blue = [_dataArray[i][@"color"][@"blue"] floatValue] / 255.0;
alpha = [_dataArray[i][@"color"][@"alpha"] floatValue];
CGContextMoveToPoint(ctx, centerX, centerY);//设置饼图中心点
CGContextSetRGBFillColor (ctx, red, green, blue, alpha);//设置饼图颜色
CGContextAddArc(ctx, centerX, centerY, radius, angle_start, angle_end, 0);//绘画饼图
CGContextFillPath(ctx);//渲染显示
}
//话覆盖的圆
[[UIColor whiteColor] setFill];
CGContextAddArc(ctx, centerX, centerY, radius-50, 0, 2*M_PI, 0);
CGContextDrawPath(ctx, kCGPathFill);
}
饼图方法二:
// 1.获取上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
// 2.拼接路径
CGPoint center = CGPointMake(self.width/2, self.height/2);//圆心设置
CGFloat radius = self.height/2 -35;//半径
CGFloat startA = 0;//开始角度
CGFloat angle = 0;//变化角度
CGFloat endA = -M_PI/2;//结束角度
for (int i = 0 ; i<_dataArray.count ;i++) {
NSNumber *number = _dataArray[i];
// 2.拼接路径
startA = endA;
angle = number.intValue / 100.0 * M_PI * 2;
endA = startA + angle;
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:startA endAngle:endA clockwise:YES];//绘制饼图
[path addLineToPoint:center];//设置饼图中心
//设置颜色
if (i == 0) {
[COLOR(44, 190, 190, 1) set];
}else if (i == 1){
[COLOR(166, 143, 215, 1) set];
}else if (i == 2){
[COLOR(76, 161, 235, 1) set];
}else if (i == 3){
[COLOR(250, 171, 111, 1) set];
}else if (i == 4){
[COLOR(205, 99, 111, 1) set];
}
// 把路径添加上下文
CGContextAddPath(ctx, path.CGPath);
// 渲染
CGContextFillPath(ctx);
}
空心园:
CGContextSetRGBStrokeColor(ctx, 208 / 255.0, 208 / 255.0, 208 / 255.0, 1);//设置颜色
CGContextSetLineWidth(ctx, 2);//设置边框厚度
CGContextAddArc(ctx, center.x, center.y, radius + 10, 0, 2 * M_PI, 0);
CGContextDrawPath(ctx, kCGPathStroke);
画线
CGPoint points[3];//定于折线的3个点
//设置3个点的坐标
points[0] = CGPointMake(center.x, center.y);
points[1] = CGPointMake(center.x +(radius +20)*cosf(startA2-angle/2), center.y - (radius +20)*sinf(startA2-angle/2));
points[2] = CGPointMake(lineX, points[1].y);
//画线
CGContextSetLineWidth(ctx, 2);
CGContextAddLines(ctx, points, 3);
CGContextDrawPath(ctx, kCGPathStroke);
写字
NSString *text = [NSString stringWithFormat:@”%@%d”,@”区域”,i];
[text drawInRect:CGRectMake(textPoint.x, textPoint.y, 40, 30) withAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12], NSForegroundColorAttributeName:COLOR(44, 190, 190, 1)}];