简单的饼状图

#import <UIKit/UIKit.h>

@interface WHBKPieChartView : UIView

@property(nonatomic,strong) NSArray *dataResource;


@end
#import "WHBKPieChartView.h"

@implementation WHBKPieChartView

-(void)setDataResource:(NSArray *)dataResource{
    _dataResource = dataResource;
}

-(instancetype)initWithFrame:(CGRect)frame{
    if(self = [super initWithFrame:frame]){
        self.backgroundColor = [UIColor whiteColor];
        self.layer.cornerRadius = self.frame.size.width / 2;
        self.layer.masksToBounds = YES;
        
//        UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.frame.size.width * 0.25 , 38, self.frame.size.width * 0.5, self.frame.size.height * 0.3)];
//        titleLabel.text = @"核心客户分析图";
//        titleLabel.numberOfLines = 2;
//        titleLabel.font = [UIFont systemFontOfSize:15];
//        titleLabel.textAlignment = NSTextAlignmentCenter;
//        titleLabel.textColor = [UIColor colorWithRed:247.0/255.0 green:87.0/255.0 blue:38.0/255.0 alpha:1.0];
//        [self addSubview:titleLabel];
//        
//        UILabel *subTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.frame.size.width * 0.25, CGRectGetMaxY(titleLabel.frame), self.frame.size.width * 0.5, 22)];
//        subTitleLabel.text = @"消费年龄";
//        subTitleLabel.font = [UIFont systemFontOfSize:12];
//        subTitleLabel.textColor = [UIColor lightGrayColor];
//        subTitleLabel.textAlignment = NSTextAlignmentCenter;
//        [self addSubview:subTitleLabel];
    }
    return self;
}

-(void)drawRect:(CGRect)rect{
    CGContextRef ctr = UIGraphicsGetCurrentContext();
    int sum = 0;
    for (int  i = 0 ; i < self.dataResource.count; i++) {
        CGFloat startRadius = 0;
        CGFloat endRadius = 0;
        startRadius = (sum / 100.0) *  M_PI * 2;
        if(i == 3){
            endRadius = 2 * M_PI;
        }else{
            endRadius = ([self.dataResource[i] floatValue] / 100) *  M_PI * 2 + startRadius;
        }
        
        CGContextMoveToPoint(ctr, rect.size.width / 2, rect.size.height / 2);
        CGContextAddArc(ctr, rect.size.width / 2, rect.size.height / 2, self.frame.size.width / 2, startRadius, endRadius, 0);
        [[self colorWithIndex:i]set];
        CGContextFillPath(ctr);
        sum += [self.dataResource[i] floatValue];
    }

    CGContextMoveToPoint(ctr, rect.size.width / 2, rect.size.height / 2);
    CGContextAddArc(ctr, rect.size.width / 2, rect.size.height / 2, self.frame.size.width / 3.0, 0, 2 * M_PI, 0);
    [[UIColor whiteColor]set];
    CGContextFillPath(ctr);
}

-(UIColor *)colorWithIndex:(int)num{
    UIColor *color;
    switch (num) {
        case 0:
//            color = [UIColor colorWithRed:247.0/255.0 green:87.0/255.0 blue:38.0/255.0 alpha:1.0];
            color = [MyController colorWithHexString:@"ffb454"];
            break;
        case 1:
             color = [MyController colorWithHexString:@"e6e6e6"];
//            color = [UIColor colorWithRed:123.0/255.0 green:162.0/255.0 blue:34.0/255.0 alpha:1.0];
            break;
        case 2:
//            color = [UIColor colorWithRed:251.0/255.0 green:137.0/255.0 blue:30.0/255.0 alpha:1.0];
            break;
        case 3:
//            color = [UIColor colorWithRed:64.0/255.0 green:181.0/255.0 blue:211.0/255.0 alpha:1.0];
            break;
        default:
            break;
    }
    return color;
}


@end

 注:引入头文件,像添加控件一样添加就好了!

原文地址:https://www.cnblogs.com/ljcgood66/p/5812645.html