UIGraphics 绘制饼图

相关设置没有写成宏定义,需要到里面改。自用版。

UIGraphics 绘制饼图_第1张图片

//

//  YJPieModel.h

//  绘制饼图

//

//  Created by pilgrim on 15/3/3.

//  Copyright (c) 2015 pilgrim. All rights reserved.

//


#import <Foundation/Foundation.h>


@interface YJPieModel : NSObject


//饼图数据

@property (nonatomic, strong) NSNumber * number;


//饼图颜色

@property (nonatomic, strong) UIColor * color;


//饼图标注

@property (nonatomic, copy) NSString * title;


//量词

@property (nonatomic, copy) NSString * quantifier;


//工厂方法

- (instancetype)initWithNumber:(NSNumber *)number color:(UIColor *)color title:(NSString *)title quantifier:(NSString *)quantifier;

+ (instancetype)YJPieModelWithNumber:(NSNumber *)number color:(UIColor *)color title:(NSString *)title quantifier:(NSString *)quantifier;


@end


//

//  YJPieModel.m

//  绘制饼图

//

//  Created by pilgrim on 15/3/3.

//  Copyright (c) 2015 pilgrim. All rights reserved.

//


#import "YJPieModel.h"


@implementation YJPieModel


- (instancetype)initWithNumber:(NSNumber *)number color:(UIColor *)color title:(NSString *)title quantifier:(NSString *)quantifier

{

    if (self = [super init]) {

        _number = number;

        _color = color;

        _title = title;

        _quantifier = quantifier;

    }

    return self;

}


+ (instancetype)YJPieModelWithNumber:(NSNumber *)number color:(UIColor *)color title:(NSString *)title quantifier:(NSString *)quantifier

{

    return [[self alloc] initWithNumber:number color:color title:title quantifier:quantifier];

}


@end


//

//  YJPieView.h

//  绘制饼图

//

//  Created by pilgrim on 14-7-13.

//  Copyright (c) 2014 pilgrim. All rights reserved.

//


#import <UIKit/UIKit.h>

#import "YJPieModel.h"


@interface YJPieView : UIView


//饼图数据模型数组

@property (nonatomic, strong) NSArray * arrModel;



@end


//

//  YJPieView.m

//  绘制饼图

//

//  Created by pilgrim on 14-7-13.

//  Copyright (c) 2014 pilgrim. All rights reserved.

//


#import "YJPieView.h"

#import <math.h>

#import "NSString+YJString.h"


@implementation YJPieView


- (void)drawRect:(CGRect)rect

{

    //判断是否有数据,如果没有,不再继续进行

    if (self.arrModel.count == 0)

    {

        NSLog(@"饼图数据缺失。");

        return;

    }

        

    //计算数据总量

    CGFloat totalNumber = 0.0;

    for (YJPieModel * model in self.arrModel)

    {

        totalNumber = totalNumber + model.number.integerValue;

    }

    

    //开启上下文

    CGContextRef ctx = UIGraphicsGetCurrentContext();

    

    //计算原点,半径,开始角度

    CGPoint center = CGPointMake(rect.size.width / 2, rect.size.height / 2);

    CGFloat r = 65;

    CGFloat startAngle = 0;

    

    int i = 0;

    

    for (YJPieModel * model in self.arrModel)

    {

        if (model.number.integerValue <= 0) {

            continue;

        }

        //计算结束角度

        CGFloat endAngle = startAngle + model.number.integerValue / totalNumber * 2 * M_PI;

        

        //贝塞尔路径画圆弧

        UIBezierPath * path = [UIBezierPath bezierPathWithArcCenter:center radius:r startAngle:startAngle endAngle:endAngle clockwise:YES];

        

        //添加到原点的线

        [path addLineToPoint:center];

        

        //封闭路径

        [path closePath];

        

        //将路径添加到上下文中

        CGContextAddPath(ctx, path.CGPath);

        

        //设置颜色

        UIColor * color;

        if (model.number.integerValue == 0)

        {

            color = [UIColor clearColor];

        }else

        {

            color = model.color;

        }

        [color set];

        

        //画图,填充模式

        CGContextDrawPath(ctx, kCGPathFill);

        

        CGContextSetLineWidth(ctx, 1.0);

        CGContextSetStrokeColorWithColor(ctx, model.color.CGColor);

        CGContextMoveToPoint(ctx, center.x, center.y);

        CGFloat x = center.x + (10 + r) * cos(endAngle - (endAngle - startAngle) / 2);

        CGFloat y = center.y + (10 + r) * sin(endAngle - (endAngle - startAngle) / 2);

        CGContextAddLineToPoint(ctx, x, y);

        

        CGFloat landLineX;

        if (endAngle - (endAngle - startAngle) / 2 > M_PI_2 && endAngle - (endAngle - startAngle) / 2 < 3 * M_PI_2) {

            landLineX = x - 5;

        }else

        {

            landLineX = x + 5;

        }

        CGContextAddLineToPoint(ctx, landLineX, y);

        

        CGContextStrokePath(ctx);

        

        NSString * titleStr = [NSString stringWithFormat:@"%@%@%@", model.title, model.number, model.quantifier];

        if (endAngle - (endAngle - startAngle) / 2 > M_PI_2 && endAngle - (endAngle - startAngle) / 2 < 3 * M_PI_2) {

            CGSize titleStrSize = [NSString sizeWithText:titleStr font:[UIFont systemFontOfSize:12.0] maxSize:CGSizeMake(MAXFLOAT, 14)];

            [titleStr drawAtPoint:CGPointMake(landLineX - titleStrSize.width, y - 7) withAttributes : @{NSFontAttributeName: [UIFont systemFontOfSize:12.0], NSForegroundColorAttributeName:model.color}];

        }else

        {

            [titleStr drawAtPoint:CGPointMake(landLineX, y - 7) withAttributes : @{NSFontAttributeName: [UIFont systemFontOfSize:12.0], NSForegroundColorAttributeName:model.color}];

        }

        

        startAngle = endAngle;

        

        //画文字

        

//        CGPoint textPoint;

//        if (startAngle < M_PI_2 || startAngle > 3 * M_PI_2) {

//            textPoint = CGPointMake(<#CGFloat x#>, <#CGFloat y#>)

//        }

//        model.number.stringValue drawAtPoint:CGPointMake(<#CGFloat x#>, <#CGFloat y#>) withAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:12.0], NSForegroundColorAttributeName:[UIColor whiteColor]}

        

        i ++;

        

//        //添加标注

//        UILabel * lblTitle = [[UILabel alloc] initWithFrame:CGRectMake(100, rect.size.width + 30 * i, 100, 30)];

//        lblTitle.textColor = [UIColor blackColor];

//        lblTitle.font = [UIFont systemFontOfSize:15];

//        lblTitle.backgroundColor = [UIColor whiteColor];

//        lblTitle.text = [NSString stringWithFormat:@"%@%@", model.title, model.number];

//        [self addSubview:lblTitle];

//        

//        //添加标注色块

//        UIView * vColor = [[UIView alloc] initWithFrame:CGRectMake(65, rect.size.width + 30 * i + 5, 20, 20)];

//        vColor.backgroundColor = model.color;

//        [self addSubview:vColor];

    }

}


#pragma mark - 懒加载

- (void)setArrModel:(NSArray *)arrModel

{

    _arrModel = arrModel;

    [self setNeedsDisplay];

}



@end





你可能感兴趣的:(UIGraphics,绘制饼图)