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: 画的方向(顺时针,逆时针)
1 <pre class="brush:objc">GraphView.h文件:</pre>

 

1 #import <UIKit/UIKit.h>
2 #import <Foundation/Foundation.h>
3   
4 @interface GraphView : UIView {
5   
6 }
7   
8 @end

 

 

 

01 <pre class="brush:objc">GraphView.m文件:</pre>
02 <pre class="brush:objc"><div class="cnblogs_Highlighter"><pre class="brush:objc">#import "GraphView.h"
03 #define PI 3.14159265358979323846
04 static inline float radians(double degrees) { return degrees * PI / 180; }
05   
06  
07 @interface GraphView(private)
08 //如果有什么私有方法,在这里声明
09 @end
10  
11 @implementation GraphView
12  
13 - (id)initWithFrame:(CGRect)frame {
14     if ((self = [super initWithFrame:frame])) {
15         // Initialization code
16     }
17     return self;
18 }
19  
20 - (void)drawRect:(CGRect)rect {
21   
22     CGRect parentViewBounds = self.bounds;
23     CGFloat x = CGRectGetWidth(parentViewBounds)/2;
24     CGFloat y = CGRectGetHeight(parentViewBounds)*0.55;
25   
26     // Get the graphics context and clear it
27     CGContextRef ctx = UIGraphicsGetCurrentContext();
28     CGContextClearRect(ctx, rect);
29   
30     // define stroke color
31     CGContextSetRGBStrokeColor(ctx, 1, 1, 1, 1.0);
32   
33     // define line width
34     CGContextSetLineWidth(ctx, 4.0);
35   
36   
37        // need some values to draw pie charts
38   
39         double snapshotCapacity =20;
40         double rawCapacity = 100;
41         double systemCapacity = 1;
42   
43     int offset = 5;
44     double pie1_start = 315.0; 
45     double pie1_finish = snapshotCapacity *360.0/rawCapacity;
46     double system_finish = systemCapacity*360.0/rawCapacity;
47   
48     CGContextSetFillColor(ctx, CGColorGetComponents( [[UIColor greenColor] CGColor]));
49     CGContextMoveToPoint(ctx, x+2*offset, y);    
50     CGContextAddArc(ctx, x+2*offset, y, 100,  radians(snapshot_start), radians(snapshot_start+snapshot_finish), 0);
51     CGContextClosePath(ctx);
52     CGContextFillPath(ctx);
53   
54     // system capacity
55     CGContextSetFillColor(ctx, CGColorGetComponents( [[UIColor colorWithRed:15 green:165/255 blue:0 alpha:1 ] CGColor]));
56     CGContextMoveToPoint(ctx, x+offset,y);    
57     CGContextAddArc(ctx, x+offset, y, 100,  radians(snapshot_start+snapshot_finish+offset), radians(snapshot_start+snapshot_finish+system_finish), 0);
58     CGContextClosePath(ctx);
59     CGContextFillPath(ctx);
60   
61     /* data capacity */
62     CGContextSetFillColor(ctx, CGColorGetComponents( [[UIColor colorWithRed:99/255 green:184/255 blue:255/255 alpha:1 ] CGColor]));
63     CGContextMoveToPoint(ctx, x, y);    
64     CGContextAddArc(ctx, x, y, 100,  radians(snapshot_start+snapshot_finish+system_finish+offset), radians(snapshot_start), 0);
65     CGContextClosePath(ctx);
66     CGContextFillPath(ctx);
67 }
68  
69  
70 - (void)dealloc {
71     [super dealloc];
72 }
73 @end
74  
75 </pre>
76 </div>
77 <br></pre>
78 <pre class="brush:objc"><br></pre>
79 <pre class="brush:objc">调用代码如下:</pre>
80 <pre class="brush:objc"><pre class="brush:objc">GraphView* viewTest = [[GraphView alloc] initWithFrame:CGRectMake(x,y,width,height)];</pre>
81 <pre class="brush:objc">[self.view addsubView:viewTest];</pre>
82 <pre class="brush:objc">[viewTest release];</pre>
83 <pre class="brush:objc"><br></pre>
84 <pre class="brush:objc"><br></pre>
85 <pre class="brush:objc">效果图如下:</pre>
86 <pre class="brush:objc"><img src="http://pic002.cnblogs.com/images/2010/169783/2010101900291074.png" alt=""><br></pre>

http://www.cnblogs.com/moshengren/archive/2010/10/19/1855246.html

posted @ 2011-10-19 21:09  周宏伟  阅读(3814)  评论(0编辑  收藏  举报