简单的饼状图

Posted 甘林梦的开发之路

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了简单的饼状图相关的知识,希望对你有一定的参考价值。

#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

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

以上是关于简单的饼状图的主要内容,如果未能解决你的问题,请参考以下文章

教你用MATLAB制作一款最有中秋特色的饼状图——月饼状图

教你用MATLAB制作一款最有中秋特色的饼状图——月饼状图

用canvas绘制的饼状图,6等分,颜色随机

Canvas---绘制饼状图

饼图绘制

Android图表库MPAndroidChart—饼状图可以再简单一点