zoukankan      html  css  js  c++  java
  • iOS-饼图

    //
    //  UIColor+Random.h
    //  饼图
    //
    //  Created by YaguangZhu on 15/9/9.
    //  Copyright (c) 2015年 YaguangZhu. All rights reserved.
    //
    
    #import <UIKit/UIKit.h>
    
    @interface UIColor (Random)
    + (UIColor *)randomColor;
    @end
    
    
    
    //
    //  UIColor+Random.m
    //  饼图
    //
    //  Created by YaguangZhu on 15/9/9.
    //  Copyright (c) 2015年 YaguangZhu. All rights reserved.
    //
    
    #import "UIColor+Random.h"
    
    @implementation UIColor (Random)
    + (UIColor *)randomColor
    {
        CGFloat r = arc4random_uniform(256)/255.0;
        CGFloat g = arc4random_uniform(256)/255.0;
        CGFloat b = arc4random_uniform(256)/255.0;
    
        return [UIColor colorWithRed:r green:g blue:b alpha:1];
    }
    @end
    //
    //  HMPieView.h
    //  饼图
    //
    //  Created by YaguangZhu on 15/9/9.
    //  Copyright (c) 2015年 YaguangZhu. All rights reserved.
    //
    
    #import <UIKit/UIKit.h>
    
    @interface HMPieView : UIView
    
    @end
    
    
    
    //
    //  HMPieView.m
    //  饼图
    //
    //  Created by YaguangZhu on 15/9/9.
    //  Copyright (c) 2015年 YaguangZhu. All rights reserved.
    //
    
    #import "HMPieView.h"
    #import "UIColor+Random.h"
    
    @implementation HMPieView
    
    
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    - (void)drawRect:(CGRect)rect {
        // Drawing code
        NSArray *data = @[@25,@25,@50];
        CGContextRef ctx = UIGraphicsGetCurrentContext();
        
        CGPoint center = CGPointMake(125, 125);
        CGFloat radius = 120;
        CGFloat startA = 0;
        CGFloat angle = 0;
        CGFloat endA = 0;
        for (NSNumber *number in data) {
            startA = endA;
            angle = number.intValue /100.0*M_PI*2;
            endA = startA +angle;
            UIBezierPath *path1 = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:startA endAngle:endA clockwise:YES];
            [path1 addLineToPoint:center];
            CGContextAddPath(ctx, path1.CGPath);
            [[UIColor randomColor]set];
            CGContextFillPath(ctx);
        };
        
        
       
        
    }
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        [self setNeedsDisplay];
    }
    
    - (void)drawPie
    {
        CGContextRef ctx = UIGraphicsGetCurrentContext();
        
        CGPoint center = CGPointMake(125, 125);
        CGFloat radius = 120;
        CGFloat startA = 0;
        CGFloat angle = 0;
        CGFloat endA = 0;
        
        angle = 25 /100.0*M_PI*2;
        endA = startA +angle;
        UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:startA endAngle:endA clockwise:YES];
        [path addLineToPoint:center];
        CGContextAddPath(ctx, path.CGPath);
        [[UIColor redColor]set];
        CGContextFillPath(ctx);
        
        
        startA = endA;
        angle = 25 /100.0*M_PI*2;
        endA = startA +angle;
        UIBezierPath *path1 = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:startA endAngle:endA clockwise:YES];
        [path1 addLineToPoint:center];
        CGContextAddPath(ctx, path1.CGPath);
        [[UIColor greenColor]set];
        
        CGContextFillPath(ctx);
        
        startA = endA;
        angle = 50 /100.0*M_PI*2;
        endA = startA +angle;
        UIBezierPath *path2 = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:startA endAngle:endA clockwise:YES];
        [path2 addLineToPoint:center];
        CGContextAddPath(ctx, path2.CGPath);
        [[UIColor blueColor]set];
        
        CGContextFillPath(ctx);
    }
    @end
  • 相关阅读:
    JavaScript 位运算总结&拾遗
    leetcode
    leetcode
    【位运算经典应用】 寻找那个唯一的数
    归并排序 JavaScript 实现
    【位运算经典应用】 求二进制逆序
    Odoo仪表盘详解
    Odoo启动运行参数(script运行参数,不是运行配置文件)
    Odoo中的self详解
    【Odoo 8开发教程】第二章:Odoo生产环境部署设置
  • 原文地址:https://www.cnblogs.com/zhuyaguang/p/4795630.html
Copyright © 2011-2022 走看看