zoukankan      html  css  js  c++  java
  • iPhone 利用CG API画一个饼图(Pie chart)

     
    核心函数是:CGContextAddArc(CGContextRef c, CGFloat x, CGFloat y, CGFloat radius, CGFloat startAngle, CGFloat endAngle, int clockwise)

        * CGContextRef: 图形上下文
        * x,y: 开始画的坐标
        * radius: 半径
        * startAngle, endAngle: 开始的弧度,结束的弧度
        * clockwise: 画的方向(顺时针,逆时针)
     
     
    有了这个函数可以画出任意扇形,所以饼图也不再话下.

    #define PI 3.14159265358979323846

    #define radius 100

    static inline float radians(double degrees) { 

            return degrees * PI / 180; 

    }

    static inline void drawArc(CGContextRef ctx, CGPoint point, float angle_start, float angle_end, UIColor* color) {

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

            CGContextSetFillColor(ctx, CGColorGetComponents( [color CGColor]));    

        CGContextAddArc(ctx, point.x, point.y, radius,  angle_start, angle_end, 0);

        //CGContextClosePath(ctx); 

        CGContextFillPath(ctx); 

    }

    - (void)drawRect:(CGRect)rect {

        CGContextRef ctx = UIGraphicsGetCurrentContext();

        CGContextClearRect(ctx, rect);

            float angle_start = radians(0.0);

            float angle_end = radians(121.0);        

            drawArc(ctx, self.center, angle_start, angle_end, [UIColor yellowColor]);

            angle_start = angle_end;

            angle_end = radians(228.0);        

            drawArc(ctx, self.center, angle_start, angle_end, [UIColor greenColor]);

            angle_start = angle_end;

            angle_end = radians(260);

            drawArc(ctx, self.center, angle_start, angle_end, [UIColor orangeColor]);

            angle_start = angle_end;

            angle_end = radians(360);

            drawArc(ctx, self.center, angle_start, angle_end, [UIColor purpleColor]);

    }

  • 相关阅读:
    MessageFormat理解,MessageFormat.format(Object obj)方法
    正则表达式
    数字处理类
    包装类
    遍历Map的4种方法(来自网络)
    集合类
    数组
    字符串
    语言基础
    Linux下使用openssl加解密
  • 原文地址:https://www.cnblogs.com/daguo/p/2619426.html
Copyright © 2011-2022 走看看