美文网首页
饼状统计图

饼状统计图

作者: JarodWang | 来源:发表于2017-10-14 10:59 被阅读88次
    同学叫我帮忙简单的弄一个饼状统计图,咳咳我顺手呢就弄了一个,顺便放上来仅供各位小伙伴的参考,写得不好的地方评论一下有时间我就改改.本文为原创如有雷同敬请谅解,仅仅作为小伙伴们的参考不做其他用途.

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

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

    相关文章

      网友评论

          本文标题:饼状统计图

          本文链接:https://www.haomeiwen.com/subject/usjxuxtx.html