zoukankan      html  css  js  c++  java
  • iphone上如何绘制饼图(使用CGContextAddArc)(原创)



    CGContextAddArc是一个比较强大的函数,建议仔细看一下iphone的开发文档。

    CGContextAddArc(CGContextRef c, CGFloat x, CGFloat y, CGFloat radius, CGFloat startAngle, CGFloat endAngle, intclockwise)

    • CGContextRef: 图形上下文
    • x,y: 开始画的坐标
    • radius: 半径
    • startAngle, endAngle: 开始的弧度,结束的弧度
    • clockwise: 画的方向(顺时针,逆时针)
    GraphView.h文件:
    #import <UIKit/UIKit.h>
    #import <Foundation/Foundation.h>
     
    @interface GraphView : UIView {
     
    }
     
    @end
    
    GraphView.m文件:
    #import "GraphView.h"
    #define PI 3.14159265358979323846
    static inline float radians(double degrees) { return degrees * PI / 180; }
     
    
    @interface GraphView(private)
    //如果有什么私有方法,在这里声明
    @end
    
    @implementation GraphView
    
    - (id)initWithFrame:(CGRect)frame {
        if ((self = [super initWithFrame:frame])) {
            // Initialization code
        }
        return self;
    }
    
    - (void)drawRect:(CGRect)rect {
     
    	CGRect parentViewBounds = self.bounds;
    	CGFloat x = CGRectGetWidth(parentViewBounds)/2;
    	CGFloat y = CGRectGetHeight(parentViewBounds)*0.55;
     
        // Get the graphics context and clear it
        CGContextRef ctx = UIGraphicsGetCurrentContext();
        CGContextClearRect(ctx, rect);
     
    	// define stroke color
    	CGContextSetRGBStrokeColor(ctx, 1, 1, 1, 1.0);
     
    	// define line width
    	CGContextSetLineWidth(ctx, 4.0);
     
     
           // need some values to draw pie charts
     
            double snapshotCapacity =20;
            double rawCapacity = 100;
            double systemCapacity = 1;
     
    	int offset = 5;
    	double pie1_start = 315.0;	
    	double pie1_finish = snapshotCapacity *360.0/rawCapacity; 
    	double system_finish = systemCapacity*360.0/rawCapacity;
     
        CGContextSetFillColor(ctx, CGColorGetComponents( [[UIColor greenColor] CGColor]));
        CGContextMoveToPoint(ctx, x+2*offset, y);     
        CGContextAddArc(ctx, x+2*offset, y, 100,  radians(snapshot_start), radians(snapshot_start+snapshot_finish), 0); 
        CGContextClosePath(ctx); 
        CGContextFillPath(ctx); 
     
    	// system capacity 
    	CGContextSetFillColor(ctx, CGColorGetComponents( [[UIColor colorWithRed:15 green:165/255 blue:0 alpha:1 ] CGColor]));
    	CGContextMoveToPoint(ctx, x+offset,y);     
        CGContextAddArc(ctx, x+offset, y, 100,  radians(snapshot_start+snapshot_finish+offset), radians(snapshot_start+snapshot_finish+system_finish), 0); 
        CGContextClosePath(ctx); 
        CGContextFillPath(ctx); 
     
    	/* data capacity */
    	CGContextSetFillColor(ctx, CGColorGetComponents( [[UIColor colorWithRed:99/255 green:184/255 blue:255/255 alpha:1 ] CGColor]));
    	CGContextMoveToPoint(ctx, x, y);     
        CGContextAddArc(ctx, x, y, 100,  radians(snapshot_start+snapshot_finish+system_finish+offset), radians(snapshot_start), 0); 
        CGContextClosePath(ctx); 
        CGContextFillPath(ctx);	
    }
    
    
    - (void)dealloc {
        [super dealloc];
    }
    @end
    
    


    调用代码如下:
    GraphView* viewTest = [[GraphView alloc] initWithFrame:CGRectMake(x,y,width,height)];
    [self.view addsubView:viewTest];
    [viewTest release];


    效果图如下:


  • 相关阅读:
    ES6你不知道的let关键字及变量的提升
    [ts] Property 'aaa' does not exist on type 'Window' 解决办法
    EXPRESS项目PM2启动NODE_ENV传参数不生效问题解决方法
    react组件开发规范总结
    JavaScript heap out of memory解决方法
    移动端响应式布局--你不知道的CSS3.0媒体查询,解决rem部分情况下无法适配的场景
    在nodeJs的Express框架下用TypeScript编写router路由出现import关键字错误的解决方案
    Zepto的天坑汇总
    mac下CornerstoneSVN出错 Description : The working copy is locked due to a previous error
    bootstrap的popover插件在focus模式时在Safari浏览器无法使用的bug解决方案
  • 原文地址:https://www.cnblogs.com/moshengren/p/1855246.html
Copyright © 2011-2022 走看看