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 itCGContextRef 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 chartsdouble 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];
效果图如下:

转载于:https://www.cnblogs.com/moshengren/archive/2010/10/19/1855246.html

iphone上如何绘制饼图(使用CGContextAddArc)(原创)相关推荐

  1. 用g.raphael.js高速绘制饼图、柱状图、点状图、折线图(上)

    首先介绍一下什么是g.raphael.这个又要说到什么是raphael.js.raphael是一个javascript库,可以用来跨浏览器绘制各种图形,只要是你想得到的图形都可以用raphael绘制出 ...

  2. 【带着canvas去流浪】 (3)绘制饼图

    目录 一. 任务说明 二. 重点提示 三. 示例代码 四. hover高亮的实现思路 示例代码托管在:http://www.github.com/dashnowords/blogs 博客园地址:< ...

  3. python绘制饼图-Python使用Plotly绘图工具,绘制饼图

    今天我们来学习一下如何使用Python的Plotly绘图工具,绘制饼图 使用Plotly绘制饼图的方法,我们需要使用graph_objs中的Pie函数 函数中最常用的两个属性values,用于赋值给需 ...

  4. ogre绘制3d图形_R语言统计与绘图:绘制饼图

    饼图也是最常见的图形之一,饼图在商业世界中无所不在,然而多数统计学家,包括相应R文档的编写者却都对它持否定态度. 相对于饼图,更推荐使用条形图或点图,因为相对于面积,人们对长度的判断更精确. 今天来学 ...

  5. 【Python】用 Highcharts 绘制饼图,也很强大

    来源:Python数据之道 作者:Peter 整理:阳哥 用 Highcharts 绘制饼图,也很强大 前不久,分享了读者投稿的文章,较为综合的介绍了可视化库 Highcharts ,这个一个 Jav ...

  6. python绘制饼图双层_Python入门进阶:Python绘制饼图到Microsoft Excel

    原标题:Python入门进阶:Python绘制饼图到Microsoft Excel 来自:Linux迷https://www.linuxmi.com/python-pie-chart-microsof ...

  7. 肝!用 Highcharts 绘制饼图,也很强大

    来源 / Python数据之道 作者/Peter   整理/阳哥 用 Highcharts 绘制饼图,也很强大 前不久,分享了读者投稿的文章,较为综合的介绍了可视化库 Highcharts ,这个一个 ...

  8. IOS开发基础之绘制饼图、柱状图、自定义进度条

    IOS开发基础之绘制饼图.柱状图.自定义进度条 源码在我的主页里 1.绘制饼图 效果 源码 // LJView.m // 34-绘图饼图 // Created by 鲁军 on 2021/2/23. ...

  9. chart.js 饼图显示百分比_实战PyQt5: 135-数据可视化之QChart绘制饼图

    饼图是数据可视图表的基本类型,在QChart中,QPieSeries, QPieSlice处理饼图的绘制. QPieSeries QPieSeries类以饼图形式显示数据.饼图系列由定义为QPieSl ...

最新文章

  1. php 开发百度聚合,php利用百度javascript API实现导航
  2. python基于dlib的face landmarks
  3. Linux中如何使用Htop监控工具?【网络安全】
  4. Direct3D提高篇:HLSL编程实现PhotoShop滤镜效果(1)
  5. C语言实现数组Array(附完整源码)
  6. 【Hibernate】could not instantiate class.. from tuple] with root cause
  7. web前端java script BOM学习笔记2017.8.1
  8. json数组传递到后台controller
  9. Redis的安装与使用
  10. 四张图,读懂 BIO、NIO、AIO、多路复用 IO 的区别
  11. Hotmail的2G邮箱被收回,只剩250M了
  12. java 异常_学习Java,你需要知道这些Java异常
  13. 本草纲目pdf彩图版下载_本草纲目中药图谱大全书PDF下载
  14. jQuery 进度条实现
  15. 学车笔记(科目二——总结)
  16. C#画一个有故事的爱心
  17. _nop_();的由来和作用
  18. 挖财基于大数据的信贷审批系统实践
  19. LibreOffice完美解决中文字体问题(在黑暗中摸索了好久~)
  20. CentOS部署ElasticSearch7.6.1集群

热门文章

  1. 《Python Cookbook 3rd》笔记(3.15):字符串转换为日期
  2. 王道考研 计算机网络18 私有IP地址 网络地址转发NAT 路由器 路由表 路由转发
  3. java日期工具类DateUtil
  4. Linux下安装jdk8步骤详述
  5. 解决报错:java.lang.NoSuchMethodException: com.tangyuan.entity.RicherProduct.<init>()
  6. maven工程建立和SSM(springMVC+spring+mybatis)整合
  7. mysql 数据库函数入门
  8. iOS linker command failed with exit code 1 (use -v to see invocation)多种解决方案汇总
  9. Github Pages建立个人博客
  10. Audit(查看审核/审计信息)