简单的饼图和环形图

1.饼状图

效果如下

方法 (void)pie

Paste_Image.png

2.环形图

方法 方法 (void)circle

Paste_Image.png
//
//  pieView.m
//  04-饼图
//
//  Created by 李亮 on 2016/12/1.
//  Copyright © 2016年 www.thelast.com. All rights reserved.
//

#import "pieView.h"

@interface pieView()

@property (nonatomic, strong) NSArray * dataArray;

@end

@implementation pieView

- (NSArray *)dataArray{
    if (_dataArray == nil) {
        _dataArray = @[@25,@25,@50];
    }
    return _dataArray;
}

- (void)drawRect:(CGRect)rect {
//    [self pie];
    [self circle];
    
}
- (void)pie{
    CGPoint startPoint = CGPointMake(self.bounds.size.width * 0.5, self.bounds.size.height * 0.5);
    CGFloat radius = self.bounds.size.width * 0.5 - 30;
    CGFloat startAngle = 0;
    CGFloat endAngle = 0;
    
    for (int index = 0; index < self.dataArray.count; index++) {
        startAngle = endAngle;
        NSNumber * num = self.dataArray[index];
        CGFloat angle = num.integerValue / 100.0 * M_PI * 2;
        endAngle = startAngle + angle;
        
        UIBezierPath * path = [UIBezierPath bezierPathWithArcCenter:startPoint radius:radius startAngle:startAngle endAngle:endAngle clockwise:YES];
        [[self randomColor] set];
        [path addLineToPoint:startPoint];
        
        [path fill];
        
    }
}
- (void)circle{
    CGPoint startPoint = CGPointMake(self.bounds.size.width * 0.5, self.bounds.size.height * 0.5);
    CGFloat radius = self.bounds.size.width * 0.5 - 30;
    CGFloat startAngle = 0;
    CGFloat endAngle = 0;
    
    for (int index = 0; index < self.dataArray.count; index++) {
        
        CAShapeLayer * shapeLayer = [CAShapeLayer layer];
        
        
        startAngle = endAngle;
        NSNumber * num = self.dataArray[index];
        CGFloat angle = num.integerValue / 100.0 * M_PI * 2;
        endAngle = startAngle + angle;
        
        UIBezierPath * path = [UIBezierPath bezierPathWithArcCenter:startPoint radius:radius startAngle:startAngle endAngle:endAngle clockwise:YES];
        
        //        [path addLineToPoint:startPoint];
        
        shapeLayer.path = path.CGPath;
        shapeLayer.lineWidth = 30;
        shapeLayer.fillColor = [UIColor clearColor].CGColor;
        shapeLayer.strokeColor = [self randomColor].CGColor;
        [self.layer addSublayer:shapeLayer];
        //        [path fill];
        
    }
}
- (UIColor *)randomColor{
    
    CGFloat r = arc4random_uniform(255) / 255.0;
    CGFloat g = arc4random_uniform(255) / 255.0;
    CGFloat b = arc4random_uniform(255) / 255.0;
    
    return [UIColor colorWithRed:r green:g blue:b alpha:1.0];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [self setNeedsDisplay];
}
//- (void)test{
//    CGPoint startPoint = CGPointMake(rect.size.width * 0.5, rect.size.height * 0.5);
//    CGFloat radius = rect.size.width * 0.5 - 30;
//    CGFloat startAngle = 0;
//    NSNumber * num = self.dataArray[0];
//    CGFloat endAngle = num.integerValue * M_PI * 2;
//    UIBezierPath * path = [UIBezierPath bezierPathWithArcCenter:startPoint radius:radius startAngle:startAngle endAngle:endAngle clockwise:YES];
//
//    [[self randomColor] set];
//    [path stroke];
//}

@end
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,204评论 4 61
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,322评论 25 709
  • 炒做自己 世上最近的距离 是与生活的触手可及 却及不得 世上最远的距离 是与生活的咫尺天涯 但涯有期 生活就是自己...
    姬二水阅读 1,150评论 2 5
  • 6月7日《人生的84000种可能》 【day87盈盈】 翻开首页序言,一下子就被俞敏洪老师讲的几句话给震惊到,他这...
    苏小盈阅读 923评论 0 0