首页 > 代码库 > 简单的饼状图
简单的饼状图
#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
注:引入头文件,像添加控件一样添加就好了!
简单的饼状图
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。