Quartz2D基础~饼状图

代码:

//
//  ZQDraw.m
//  
//
//  Created by QiZhang on 11/26/15.
//
//

#import "ZQDraw.h"
#import "UIColor+Random.h"

#define kWidth 30
#define kMargin kWidth

@implementation ZQDraw


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self setNeedsDisplay];
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {

    [self testZhu];
}

// 柱状图
- (void)testZhu
{
    NSArray *data = @[@"50",@"50",@"100"];
    // get context
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    for (int i = 0; i < data.count ; i++) {
        // 构建路径
        UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(i * (kWidth + kMargin), 200 - [data[i] intValue], kWidth,[data[i] intValue])];
        // 把路径添加到上下文
        CGContextAddPath(ctx, path.CGPath);
        [[UIColor randomColor] set];
        CGContextFillPath(ctx);
    }

}

// 饼状图
- (void)testPie
{
    NSArray *data = @[@25,@25,@50];
    CGContextRef ctx = UIGraphicsGetCurrentContext();

    // 拼接路径
    CGPoint center = CGPointMake(100, 100);
    CGFloat radius = 100;
    CGFloat startA = 0;
    CGFloat endA = 0;
    CGFloat angle = 0;

    for (NSNumber *number in data) {
        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];
        // 添加到上下文
        [[UIColor randomColor] set];
        CGContextAddPath(ctx, path.CGPath);

        CGContextFillPath(ctx);
    }

}

- (void)test
{
    // 得到上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();

    CGPoint center = CGPointMake(100, 100);
    CGFloat radius = 100;
    CGFloat startA = 0;
    CGFloat endA = 0;
    CGFloat angle = 0;
    // 第一个扇形
    angle = 25/100.0 * M_PI * 2;
    endA = startA + angle;
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:startA endAngle:endA clockwise:YES];
    [path addLineToPoint:center];
    // 添加到上下文
    CGContextAddPath(ctx, path.CGPath);
    [[UIColor redColor] set];
    // 渲染
    CGContextFillPath(ctx);

    // 第二个扇形
    startA = endA;
    angle = 25 / 100.0 * M_PI * 2;
    endA = startA + angle;
    UIBezierPath *path1 = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:startA endAngle:endA clockwise:YES];
    [path1 addLineToPoint:center];
    // 添加到上下文
    CGContextAddPath(ctx, path1.CGPath);
    [[UIColor greenColor] set];
    // 渲染
    CGContextFillPath(ctx);

    // 第三个扇形
    startA = endA;
    angle = 50 / 100.0 * M_PI * 2;
    endA = startA + angle;
    UIBezierPath *path2 = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:startA endAngle:endA clockwise:YES];
    [path2 addLineToPoint:center];
    // 添加到上下文
    CGContextAddPath(ctx, path2.CGPath);
    [[UIColor blueColor] set];
    // 渲染
    CGContextFillPath(ctx);

}


@end

截图:
饼状图

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值