iphone上如何绘制饼图(使用CGContextAddArc)(原创)
来源:互联网 发布:马尔可夫链的算法 编辑:程序博客网 时间:2024/06/06 14:59
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.14159265358979323846static 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 colorCGContextSetRGBStrokeColor(ctx, 1, 1, 1, 1.0); // define line widthCGContextSetLineWidth(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];效果图如下:
- iphone上如何绘制饼图(使用CGContextAddArc)(原创)
- iphone上如何绘制柱状图(转载,整理)
- 如何在iPhone上绘制出一个笑脸
- CGContextAddArc的使用
- CGContextAddArc的使用
- CGContextAddArc
- CGContextAddArc
- 如何在iphone上使用opencv
- iphone学习(原创+搜集)
- 如何将控件controls 画(绘制)到 图(bitmap)上
- 如何使用双缓冲在窗体上绘制
- 如何在Android上使用OpenGL ES 2.0绘制点。
- 教你如何使用iPhone上MSN,聊Skype
- 如何使用xcode将应用安装到IPhone上
- CGContextAddArcToPoint和CGContextAddArc的绘图使用
- BaiduMap---百度地图官方Demo之OpenGL绘制功能(介绍如何使用OpenGL绘制在地图中进行绘制)
- Iphone上使用UIPopoverPresentationController
- IPhone 网络开发之如何使用NSURLConnection(一)
- scrollViewDidScroll 不调用
- TFT屏(OCM320240T350)调试总结
- rails工程如何发布
- iOS 条形码技术
- ubuntu如何安装 tar,gz tar.gz2
- iphone上如何绘制饼图(使用CGContextAddArc)(原创)
- Android 百度地图API 定位 导航 代码
- java内存分配研究
- Java程序执行过程
- Drupal新建内容类型后无法添加该类型的内容
- objective-c的属性声明问题“ivar used by '@synthesize' declaration must be an existing iva”
- 第二周项目二—读取数据到结构体数组
- 求日期
- Matlab编程笔记:续行符