饼状统计图

同学叫我帮忙简单的弄一个饼状统计图,咳咳我顺手呢就弄了一个,顺便放上来仅供各位小伙伴的参考,写得不好的地方评论一下有时间我就改改.本文为原创如有雷同敬请谅解,仅仅作为小伙伴们的参考不做其他用途.



多的话不用说下面直接贴代码

ViewController

<<<<<<<<<<<<<<<<<<<<<<<<<<//

//ViewController.m

//DrawRation

//

//Created by Telent丶妖孽 on 2017/10/11.

//Copyright © 2017年 Telent丶妖孽. All rights reserved.

//

#import"ViewController.h"

#import"RationView.h"

#import"RoundModel.h"

@interfaceViewController(){

RationView* rationView;

}

@end

@implementationViewController

- (void)viewDidLoad {

[superviewDidLoad];

self.view.backgroundColor= [UIColorwhiteColor];

rationView= [[RationViewalloc]init];

rationView.frame=CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height);

rationView.backgroundColor= [UIColorwhiteColor];

[self.viewaddSubview:rationView];

rationView.isstart=YES;

rationView.arcModelArray=getDataArray();

rationView.uiModel= [selfcrateRoundModel];

}

-(RoundModel* )crateRoundModel{

RoundModel* model = [[RoundModelalloc]init];

//CGFloat radius:半径

model.radius=60;

//textFontSize:字体大小

model.textFontSize=15.0;

//textColor:字体颜色

model.textColor=[UIColorblackColor];

//content_boundary_width:标注边界->标注线离着屏幕边界的距离

model.content_boundary_width=50.0;

//content_round_width:标注的label距离圆弧边界的距离

model.content_round_width=10.0;

//tagging_length:标注线的长度

model.tagging_length=10;

//label_offset_y:标注的label的偏移量

model.label_offset_y=15.0/2;

returnmodel;

}

staticinlineNSMutableArray* getDataArray(){

//此方法->我 为了模拟数据而生的

NSMutableArray* array = [NSMutableArrayarray];

NSMutableArray* colorArray = [NSMutableArrayarrayWithObjects:[UIColorredColor],[UIColorgreenColor],[UIColorblueColor],[UIColorpurpleColor],nil];

NSMutableArray* titleArray = [NSMutableArrayarrayWithObjects:@"小月饼",@"大月饼",@"小小月饼",@"大大月饼",nil];

NSMutableArray*pecArray= [NSMutableArrayarrayWithObjects:@"20.0",@"30.0",@"20.0",@"30.0",nil];

for(inti =0; i < pecArray.count; i ++) {

RoundPecentAndTitleModel* model = [[RoundPecentAndTitleModelalloc]init];

model.pecent= pecArray[i];

model.title= titleArray[i];

model.color= colorArray[i];

[arrayaddObject:model];

}

returnarray;

}

- (void)didReceiveMemoryWarning {

[superdidReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

@end>>>>>>>>>>>>>>>>>>>

点h没啥东西


接着View


点H文件 :<<<<<<<<<<<<<<<<<<//

//RationView.h

//DrawRation

//

//Created by Telent丶妖孽 on 2017/10/11.

//Copyright © 2017年 Telent丶妖孽. All rights reserved.

//

#import

#import"RoundModel.h"

#import"RoundPecentAndTitleModel.h"

@interfaceRationView :UIView

@property(nonatomic,assign)BOOLisstart;

/**

定制限定UI的model

*/

@property(nonatomic,retain)RoundModel* uiModel;

/**

定义度数以及我们绘制需要的数据model

*/

@property(nonatomic,retain)NSMutableArray* arcModelArray;

@end>>>>>>>>>>>>>>>>>>>>>


点M文件:<<<<<<<<<<<<<<<<//

//RationView.m

//DrawRation

//

//Created by Telent丶妖孽 on 2017/10/11.

//Copyright © 2017年 Telent丶妖孽. All rights reserved.

//

#import"RationView.h"

#define ANGLE_PI (3.14/180.0)

@implementationRationView

/**

绘制圆弧区块

注:内联函数

@param context 上下文

@param point 中心点

@param start_angle 起点角度

@param end_angle 终点角度

@param color 画笔颜色

@param radius 半径

*/

staticinlinevoidstartDrawArc(CGContextRefcontext,CGPointpoint,floatstart_angle,floatend_angle,UIColor* color,floatradius) {

CGContextMoveToPoint(context, point.x,point.y);

CGContextSetFillColor(context,CGColorGetComponents( [colorCGColor]));

CGContextAddArc(context, point.x,point.y, radius,start_angle*ANGLE_PI, end_angle *ANGLE_PI,0);

CGContextFillPath(context);

}

/**

根据给予的百分比计算角度

@param angleStr 给予的百分比字符串

@return 回调一个角度

*/

staticinlineCGFloatgetAngle(NSString* angleStr){

CGFloatangle = [angleStrfloatValue]/100*360.0;

returnangle;

}

staticinlineNSString* getName(NSString* pecent,NSString* title){

return[NSStringstringWithFormat:@"%@:%@%%",title,pecent];

}

- (void)drawRect:(CGRect)rect {

if(self.isstart==YES) {

CGFloatradius =self.uiModel.radius;

CGFloatstart_angle =0.0;

CGFloatend_angle =0.0;

CGContextRefcontext =UIGraphicsGetCurrentContext();

for(inti =0; i

RoundPecentAndTitleModel* pecentModel =self.arcModelArray[i];

end_angle =getAngle(pecentModel.pecent);

startDrawArc(context,self.center, start_angle, start_angle+end_angle, pecentModel.color, radius);

//圆弧中心点

CGPointpoint_center = [selfgetPointWithContentTextCenterX:self.center.xContentTextCenterY:self.center.yradius:radiuscirAngle:(start_angle+ end_angle /2)];

//标注延伸线

CGPointpoint_turning= [selfgetPointWithContentTextCenterX:self.center.xContentTextCenterY:self.center.yradius:radius+self.uiModel.tagging_lengthcirAngle:(start_angle+ end_angle /2)];

//字符串长度

NSString*label_Title =getName(pecentModel.pecent, pecentModel.title);

NSDictionary*dict =@{NSFontAttributeName:[UIFontsystemFontOfSize:self.uiModel.textFontSize]};

CGSizesize = [label_TitleboundingRectWithSize:CGSizeMake(MAXFLOAT,self.uiModel.textFontSize)options:NSStringDrawingUsesLineFragmentOriginattributes:dictcontext:nil].size;

if(size.width>CGRectGetWidth(self.frame) /2- radius) {

size.width=CGRectGetWidth(self.frame) /2- radius -10;

}

//折线

CGFloatturning_line_x;

CGFloatcontent_label_x;

if(start_angle+ end_angle /2<90|| start_angle+ end_angle /2>270) {

//右边

if(point_turning.x+ size.width>CGRectGetWidth(self.frame)) {

point_turning.x=CGRectGetWidth(self.frame) - size.width-self.uiModel.content_boundary_width;

}

turning_line_x = point_turning.x+ size.width+self.uiModel.content_round_width;

content_label_x= turning_line_x - size.width/2;

}else{

//左边

if(point_turning.x< size.width) {

point_turning.x= size.width+50;

}

turning_line_x = point_turning.x-size.width-self.uiModel.content_round_width;

content_label_x =turning_line_x +size.width/2;

}

//标注的内容

UILabel*content_label = [[UILabelalloc]initWithFrame:CGRectMake(0,0, size.width,self.uiModel.textFontSize)];

content_label.text= label_Title;

content_label.font= [UIFontsystemFontOfSize:self.uiModel.textFontSize];

content_label.textColor=self.uiModel.textColor;

[selfaddSubview:content_label];

content_label.center=CGPointMake(content_label_x, point_turning.y-self.uiModel.label_offset_y);

CGContextMoveToPoint(context, point_center.x, point_center.y);CGContextAddLineToPoint(context, point_turning.x, point_turning.y);

CGContextAddLineToPoint(context, point_turning.x, point_turning.y);CGContextAddLineToPoint(context, turning_line_x, point_turning.y);

CGContextStrokePath(context);

start_angle+=end_angle;

}

}

}

/**

计算每一块区域圆弧的中心点

@param center_x 画板中心点的x

@param center_y 画板中心点的y

@param radiu 半径

@param cirAngle 圆弧中心点距离原始起点的弧度

@return 返回一个圆弧中心点

*/

- (CGPoint)getPointWithContentTextCenterX:(CGFloat)center_x ContentTextCenterY:(CGFloat)center_y radius:(CGFloat)radiu cirAngle:(CGFloat)cirAngle

{

CGFloatpoint_x =0.0;

CGFloatpoint_y =0.0;

CGFloatarcAngle =M_PI* cirAngle /180.0;

if(cirAngle <90) {

point_x = center_x +cos(arcAngle) * radiu;

point_y = center_y +sin(arcAngle) * radiu;

}elseif(cirAngle ==90) {

point_x = center_x;

point_y = center_y + radiu;

}elseif(cirAngle >90&& cirAngle <180) {

arcAngle =M_PI* (180- cirAngle) /180.0;

point_x = center_x -cos(arcAngle) * radiu;

point_y = center_y +sin(arcAngle) * radiu;

}elseif(cirAngle ==180) {

point_x = center_x - radiu;

point_y = center_y;

}elseif(cirAngle >180&& cirAngle <270) {

arcAngle =M_PI* (cirAngle -180) /180.0;

point_x = center_x -cos(arcAngle) * radiu;

point_y = center_y -sin(arcAngle) * radiu;

}elseif(cirAngle ==270) {

point_x = center_x;

point_y = center_y - radiu;

}else{

arcAngle =M_PI* (360- cirAngle) /180.0;

point_x = center_x +cos(arcAngle) * radiu;

point_y = center_y -sin(arcAngle) * radiu;

}

returnCGPointMake(point_x, point_y);

}

@end>>>>>>>>>>>>>>>>>>


接着Model

ModelOne

点H:<<<<<<<<<<<<<<<<<<//

//RoundModel.h

//DrawRation

//

//Created by Telent丶妖孽 on 2017/10/13.

//Copyright © 2017年 Telent丶妖孽. All rights reserved.

//

#import

@interfaceRoundModel :NSObject

/**

半径

*/

@property(nonatomic,assign)CGFloatradius;

/**

textFontSize:字体大小

*/

@property(nonatomic,assign)CGFloattextFontSize;

/**

textColor:字体颜色

*/

@property(nonatomic,retain)UIColor* textColor;

/**

label_offset_y:标注的label的偏移量

content_round_width:标注的label距离圆弧边界的距离

content_boundary_width:标注边界->标注线离着屏幕边界的距离

tagging_length:标注线的长度

*/

@property(nonatomic,assign)CGFloatlabel_offset_y,content_round_width,content_boundary_width,tagging_length;

@end>>>>>>>>>>>>


ModelTwo

点H:<<<<<<<<<<<<<<//

//RoundPecentAndTitleModel.h

//DrawRation

//

//Created by Telent丶妖孽 on 2017/10/13.

//Copyright © 2017年 Telent丶妖孽. All rights reserved.

//

#import

@interfaceRoundPecentAndTitleModel :NSObject

/**

百分比

*/

@property(nonatomic,retain)NSString* pecent;

/**

所统计的区块名字

*/

@property(nonatomic,retain)NSString* title;

/**

画笔的颜色

*/

@property(nonatomic,retain)UIColor* color;

@end>>>>>>>>>>>>>


好了ok了.简单的就这样完成了.小伙伴们可以直接把对应的文件创建好了把这些代码帖进去就可以看到效果了.当然了这个就是为了帮助一下遇到这方面不会写的小伙伴们一下.至于会写的大神呢要是能帮得到那我当然很开心了,帮不到的话勿喷请多多指点.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,835评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,598评论 1 295
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,569评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,159评论 0 213
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,533评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,710评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,923评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,674评论 0 203
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,421评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,622评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,115评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,428评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,114评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,097评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,875评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,753评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,649评论 2 271

推荐阅读更多精彩内容