第三方Charts绘制图表四种形式:饼状图,雷达图,柱状图,直线图
对于第三方框架Charts(Swift版本,在OC项目中需要添加桥接头文件),首先要解决在项目中集成的问题,集成步骤:
一、下载Charts框架
下载地址:https://github.com/danielgindi/Charts.
解压后的文件夹里面的内容是这个样子的,如下图:
下载完成后,仔细看一下所需环境,很重要!如下图:
二、新建工程,导入Charts.xcodeproj
工程
1.新建工程
新建工程,取名为ChartsDemo
.
2.复制Charts
整个文件夹到项目ChartsDemo
文件夹中
如下图:
3.导入Charts.xcodeproj
工程
将Charts
文件夹中的Charts.xcodeproj
工程文件导入到ChartsDemo
工程中,注意导入的是Charts.xcodeproj
工程,而不是Charts
文件夹,如下图:
4.添加Charts.framework
点击创建的工程文件-->General->找到Embedded Binaries行,点击+号添加Charts.framework
如下图:
三、建立OC和Swift的桥接文件
在ChartsDemo
工程中新建一个Swift文件,名字随便取,这时候会提示是否建立桥接文件,直接选Create Bridging Header
选项,如下图:

新建完成后,会自动生成一个桥接文件,如下图:
四、设置编译选项及引入Charts
1.设置编译选项
点击工程文件-->Build Settings -> Embedded Content Contains Swift Code 选项,设置为Yes,如下图:

2.在桥接文件中引入Charts
3.在对应的ViewController.m
中引入相关头文件,如图:
引入完成之后,编译一下,如果有错,Clean一下再次编译,编译没有错误说明导入成功.
五、测试
在ViewController.m
中进行测试,代码如下:
BarChartView *chatView = [[BarChartView alloc] initWithFrame:CGRectMake(10, 100, 300, 300)];
[self.view addSubview:chatView];
Charts
完毕!接下来就是绘制各类图表的代码(简单的创建视图,具体代码方法看Demo):
饼状图:
//创建饼状图
self.pieChartView = [[PieChartView alloc] initWithFrame:CGRectMake(20, 80, f_Device_w-40, 300)];
[self.view addSubview:self.pieChartView];
//基本样式
[self.pieChartView setExtraOffsetsWithLeft:30 top:0 right:30 bottom:0];//饼状图距离边缘的间隙
self.pieChartView.usePercentValuesEnabled = YES;//是否根据所提供的数据, 将显示数据转换为百分比格式
self.pieChartView.dragDecelerationEnabled = YES;//拖拽饼状图后是否有惯性效果
self.pieChartView.drawSliceTextEnabled = YES;//是否显示区块文本
//空心饼状图样式
self.pieChartView.drawHoleEnabled = YES;//饼状图是否是空心
self.pieChartView.holeRadiusPercent = 0.5;//空心半径占比
self.pieChartView.holeColor = [UIColor clearColor];//空心颜色
self.pieChartView.transparentCircleRadiusPercent = 0.52;//半透明空心半径占比
self.pieChartView.transparentCircleColor = [UIColor colorWithRed:210/255.0 green:145/255.0 blue:165/255.0 alpha:0.3];//半透明空心的颜色
//实心饼状图样式
// self.pieChartView.drawHoleEnabled = NO;
//饼状图中间描述
if (self.pieChartView.isDrawHoleEnabled == YES) {
self.pieChartView.drawCenterTextEnabled = YES;//是否显示中间文字
//普通文本
// self.pieChartView.centerText = @"饼状图";//中间文字
//富文本
NSMutableAttributedString *centerText = [[NSMutableAttributedString alloc] initWithString:@"饼状图"];
[centerText setAttributes:@{NSFontAttributeName: [UIFont boldSystemFontOfSize:16],NSForegroundColorAttributeName: [UIColor orangeColor]} range:NSMakeRange(0, centerText.length)];
self.pieChartView.centerAttributedText = centerText;
}
//饼状图描述
self.pieChartView.descriptionText = @"饼状图示例";
self.pieChartView.descriptionFont = [UIFont systemFontOfSize:10];
self.pieChartView.descriptionTextColor = [UIColor grayColor];
//饼状图图例
self.pieChartView.legend.maxSizePercent = 1;//图例在饼状图中的大小占比, 这会影响图例的宽高
self.pieChartView.legend.formToTextSpace = 5;//文本间隔
self.pieChartView.legend.font = [UIFont systemFontOfSize:10];//字体大小
self.pieChartView.legend.textColor = [UIColor grayColor];//字体颜色
self.pieChartView.legend.position = ChartLegendPositionBelowChartCenter;//图例在饼状图中的位置
self.pieChartView.legend.form = ChartLegendFormCircle;//图示样式: 方形、线条、圆形
self.pieChartView.legend.formSize = 12;//图示大小
//为饼状图提供数据
self.pieCData = [self setData];
self.pieChartView.data = self.pieCData;
//设置动画效果
[self.pieChartView animateWithXAxisDuration:1.0f easingOption:ChartEasingOptionEaseOutExpo];
雷达图:
//创建雷达图对象
self.radarChartView = [[RadarChartView alloc] initWithFrame:CGRectMake(20, 80, f_Device_w-40, 300)];
[self.view addSubview:self.radarChartView];
self.radarChartView.delegate = self;
self.radarChartView.descriptionText = @"";//描述
self.radarChartView.rotationEnabled = YES;//是否允许转动
self.radarChartView.highlightPerTapEnabled = YES;//是否能被选中
//雷达图线条样式
self.radarChartView.webLineWidth = 0.5;//主干线线宽
self.radarChartView.webColor = [self colorWithHexString:@"#c2ccd0"];//主干线线宽
self.radarChartView.innerWebLineWidth = 0.375;//边线宽度
self.radarChartView.innerWebColor = [self colorWithHexString:@"#c2ccd0"];//边线颜色
self.radarChartView.webAlpha = 1;//透明度
//设置 xAxi
ChartXAxis *xAxis = self.radarChartView.xAxis;
xAxis.labelFont = [UIFont systemFontOfSize:15];//字体
xAxis.labelTextColor = [self colorWithHexString:@"#057748"];//颜色
//设置 yAxis
ChartYAxis *yAxis = self.radarChartView.yAxis;
yAxis.axisMinValue = 0.0;//最小值
yAxis.axisMaxValue = 150.0;//最大值
yAxis.drawLabelsEnabled = NO;//是否显示 label
yAxis.labelCount = 6;// label 个数
yAxis.labelFont = [UIFont systemFontOfSize:9];// label 字体
yAxis.labelTextColor = [UIColor lightGrayColor];// label 颜色
//为雷达图提供数据
self.radarCData = [self setData];
self.radarChartView.data = self.radarCData;
[self.radarChartView renderer];
//设置动画
[self.radarChartView animateWithYAxisDuration:0.1f];
柱状图:
//添加barChartView
self.barChartView = [[BarChartView alloc] initWithFrame:CGRectMake(20, 80, f_Device_w-40, 300)];
self.barChartView.delegate = self;//设置代理
[self.view addSubview:self.barChartView];
//基本样式
self.barChartView.backgroundColor = [UIColor colorWithRed:230/255.0f green:253/255.0f blue:253/255.0f alpha:1];
self.barChartView.noDataText = @"暂无数据";//没有数据时的文字提示
self.barChartView.drawValueAboveBarEnabled = YES;//数值显示在柱形的上面还是下面
self.barChartView.drawHighlightArrowEnabled = NO;//点击柱形图是否显示箭头
self.barChartView.drawBarShadowEnabled = NO;//是否绘制柱形的阴影背景
//交互设置
self.barChartView.scaleYEnabled = NO;//取消Y轴缩放
self.barChartView.doubleTapToZoomEnabled = NO;//取消双击缩放
self.barChartView.dragEnabled = YES;//启用拖拽图表
self.barChartView.dragDecelerationEnabled = YES;//拖拽后是否有惯性效果
self.barChartView.dragDecelerationFrictionCoef = 0.9;//拖拽后惯性效果的摩擦系数(0~1),数值越小,惯性越不明显
//X轴样式
ChartXAxis *xAxis = self.barChartView.xAxis;
xAxis.axisLineWidth = 1;//设置X轴线宽
xAxis.labelPosition = XAxisLabelPositionBottom;//X轴的显示位置,默认是显示在上面的
xAxis.drawGridLinesEnabled = NO;//不绘制网格线
xAxis.spaceBetweenLabels = 4;//设置label间隔,若设置为1,则如果能全部显示,则每个柱形下面都会显示label
xAxis.labelTextColor = [UIColor brownColor];//label文字颜色
//右边Y轴样式
self.barChartView.rightAxis.enabled = NO;//不绘制右边轴
//左边Y轴样式
ChartYAxis *leftAxis = self.barChartView.leftAxis;//获取左边Y轴
leftAxis.labelCount = 5;//Y轴label数量,数值不一定,如果forceLabelsEnabled等于YES, 则强制绘制制定数量的label, 但是可能不平均
leftAxis.forceLabelsEnabled = NO;//不强制绘制制定数量的label
leftAxis.showOnlyMinMaxEnabled = NO;//是否只显示最大值和最小值
leftAxis.axisMinValue = 0;//设置Y轴的最小值
leftAxis.startAtZeroEnabled = YES;//从0开始绘制
leftAxis.axisMaxValue = 105;//设置Y轴的最大值
leftAxis.inverted = NO;//是否将Y轴进行上下翻转
leftAxis.axisLineWidth = 0.5;//Y轴线宽
leftAxis.axisLineColor = [UIColor blackColor];//Y轴颜色
leftAxis.valueFormatter = [[NSNumberFormatter alloc] init];//自定义格式
leftAxis.valueFormatter.positiveSuffix = @" $";//数字后缀单位
leftAxis.labelPosition = YAxisLabelPositionOutsideChart;//label位置
leftAxis.labelTextColor = [UIColor brownColor];//文字颜色
leftAxis.labelFont = [UIFont systemFontOfSize:10.0f];//文字字体
//网格线样式
leftAxis.gridLineDashLengths = @[@3.0f, @3.0f];//设置虚线样式的网格线
leftAxis.gridColor = [UIColor colorWithRed:200/255.0f green:200/255.0f blue:200/255.0f alpha:1];//网格线颜色
leftAxis.gridAntialiasEnabled = YES;//开启抗锯齿
//添加限制线
ChartLimitLine *limitLine = [[ChartLimitLine alloc] initWithLimit:80 label:@"限制线"];
limitLine.lineWidth = 2;
limitLine.lineColor = [UIColor greenColor];
limitLine.lineDashLengths = @[@5.0f, @5.0f];//虚线样式
limitLine.labelPosition = ChartLimitLabelPositionRightTop;//位置
[leftAxis addLimitLine:limitLine];//添加到Y轴上
leftAxis.drawLimitLinesBehindDataEnabled = YES;//设置限制线绘制在柱形图的后面
//图例说明样式
self.barChartView.legend.enabled = NO;//不显示图例说明
// self.barChartView.legend.position = ChartLegendPositionBelowChartLeft;//位置
//右下角的description文字样式
self.barChartView.descriptionText = @"";//不显示,就设为空字符串即可
// self.barChartView.descriptionText = @"柱形图";
self.barCData = [self setData];
//为柱形图提供数据
self.barChartView.data = self.barCData;
//设置动画效果,可以设置X轴和Y轴的动画效果
[self.barChartView animateWithYAxisDuration:1.0f];
折线图:
//添加折线视图LineChartView
self.LineChartView = [[LineChartView alloc] initWithFrame:CGRectMake(20, 80, f_Device_w-40, 300)];
self.LineChartView.delegate = self;//设置代理
[self.view addSubview:self.LineChartView];
//基本样式
self.LineChartView.backgroundColor = [UIColor colorWithRed:230/255.0f green:253/255.0f blue:253/255.0f alpha:1];
self.LineChartView.noDataText = @"暂无数据";
//交互样式
self.LineChartView.scaleYEnabled = NO;//取消Y轴缩放
self.LineChartView.doubleTapToZoomEnabled = NO;//取消双击缩放
self.LineChartView.dragEnabled = YES;//启用拖拽图标
self.LineChartView.dragDecelerationEnabled = YES;//拖拽后是否有惯性效果
self.LineChartView.dragDecelerationFrictionCoef = 0.9;//拖拽后惯性效果的摩擦系数(0~1),数值越小,惯性越不明显
//X轴样式
ChartXAxis *xAxis = self.LineChartView.xAxis;
xAxis.axisLineWidth = 1.0/[UIScreen mainScreen].scale;//设置X轴线宽
xAxis.labelPosition = XAxisLabelPositionBottom;//X轴的显示位置,默认是显示在上面的
xAxis.drawGridLinesEnabled = NO;//不绘制网格线
xAxis.spaceBetweenLabels = 4;//设置label间隔
xAxis.labelTextColor = [self colorWithHexString:@"#057748"];//label文字颜色
//Y轴样式
self.LineChartView.rightAxis.enabled = NO;//不绘制右边轴
ChartYAxis *leftAxis = self.LineChartView.leftAxis;//获取左边Y轴
leftAxis.labelCount = 5;//Y轴label数量,数值不一定,如果forceLabelsEnabled等于YES, 则强制绘制制定数量的label, 但是可能不平均
leftAxis.forceLabelsEnabled = NO;//不强制绘制指定数量的label
leftAxis.showOnlyMinMaxEnabled = NO;//是否只显示最大值和最小值
leftAxis.axisMinValue = 0;//设置Y轴的最小值
leftAxis.startAtZeroEnabled = YES;//从0开始绘制
leftAxis.axisMaxValue = 105;//设置Y轴的最大值
leftAxis.inverted = NO;//是否将Y轴进行上下翻转
leftAxis.axisLineWidth = 1.0/[UIScreen mainScreen].scale;//Y轴线宽
leftAxis.axisLineColor = [UIColor blackColor];//Y轴颜色
leftAxis.valueFormatter = [[NSNumberFormatter alloc] init];//自定义格式
leftAxis.valueFormatter.positiveSuffix = @" $";//数字后缀单位
leftAxis.labelPosition = YAxisLabelPositionOutsideChart;//label位置
leftAxis.labelTextColor = [self colorWithHexString:@"#057748"];//文字颜色
leftAxis.labelFont = [UIFont systemFontOfSize:10.0f];//文字字体
//网格线样式
leftAxis.gridLineDashLengths = @[@3.0f, @3.0f];//设置虚线样式的网格线
leftAxis.gridColor = [UIColor colorWithRed:200/255.0f green:200/255.0f blue:200/255.0f alpha:1];//网格线颜色
leftAxis.gridAntialiasEnabled = YES;//开启抗锯齿
//添加限制线
ChartLimitLine *limitLine = [[ChartLimitLine alloc] initWithLimit:80 label:@"限制线"];
limitLine.lineWidth = 2;
limitLine.lineColor = [UIColor greenColor];
limitLine.lineDashLengths = @[@5.0f, @5.0f];//虚线样式
limitLine.labelPosition = ChartLimitLabelPositionRightTop;//位置
limitLine.valueTextColor = [self colorWithHexString:@"#057748"];//label文字颜色
limitLine.valueFont = [UIFont systemFontOfSize:12];//label字体
[leftAxis addLimitLine:limitLine];//添加到Y轴上
leftAxis.drawLimitLinesBehindDataEnabled = YES;//设置限制线绘制在折线图的后面
//描述及图例样式
[self.LineChartView setDescriptionText:@"折线图"];
[self.LineChartView setDescriptionTextColor:[UIColor darkGrayColor]];
self.LineChartView.legend.form = ChartLegendFormLine;
self.LineChartView.legend.formSize = 30;
self.LineChartView.legend.textColor = [UIColor darkGrayColor];
self.lineCData = [self setData];
self.LineChartView.data = self.lineCData;
[self.LineChartView animateWithXAxisDuration:1.0f];
界面效果:
Demo下载:http://download.csdn.net/detail/hbblzjy/9587821
第三方Charts绘制图表四种形式:饼状图,雷达图,柱状图,直线图的更多相关文章
- 使用D3绘制图表(7)--饼状图
这次是绘制饼状图,也是这一次使用D3绘制图表的最后一篇,大家可以从其他地方深入学习D3绘制图表,也可以直接查看D3的API进行学习,本次绘制饼状图的数据跟之前的卸载数组里面的不一样,这一次是使用d3的 ...
- Node.js-提供了四种形式的定时器
Node.js提供了四种形式的定时器 global.setTimeout(); //一次性定时器 global.setInterval(); //周期性定时器 global.nextTick(); / ...
- es6 Object.assign ECMAScript 6 笔记(六) ECMAScript 6 笔记(一) react入门——慕课网笔记 jquery中动态新增的元素节点无法触发事件解决办法 响应式图像 弹窗细节 微信浏览器——返回操作 Float 的那些事 Flex布局 HTML5 data-* 自定义属性 参数传递的四种形式
es6 Object.assign 目录 一.基本用法 二.用途 1. 为对象添加属性 2. 为对象添加方法 3. 克隆对象 4. 合并多个对象 5. 为属性指定默认值 三.浏览器支持 ES6 O ...
- Python中函数传递参数有四种形式
Python中函数传递参数有四种形式 fun1(a,b,c) fun2(a=1,b=2,c=3) fun3(*args) fun4(**kargs) 四种中最常见是前两种,基本上一般点的教程都会涉及, ...
- 模块的四种形式、 import和from...import、 循环导入问题、模块的搜索路径、 python文件的两种用途
目录 模块的四种形式 模块 模块的四种形式 import和from...import 循环导入问题 模拟问题的发生: 解决方案 模块的搜索路径 Python文件的两种用途 模块的四种形式 Nike推荐 ...
- Android图表库MPAndroidChart(八)——饼状图的扩展:折线饼状图
Android图表库MPAndroidChart(八)--饼状图的扩展:折线饼状图 我们接着上文,饼状图的扩展,增加折现的说明,来看下我们要实现的效果 因为之前对MPAndroidChart的熟悉,所 ...
- Android图表库MPAndroidChart(七)—饼状图可以再简单一点
Android图表库MPAndroidChart(七)-饼状图可以再简单一点 接上文,今天实现的是用的很多的,作用在统计上的饼状图,我们看下今天的效果 这个效果,我们实现,和之前一样的套路,我先来说下 ...
- iOS - Quartz 2D 第三方框架 Charts 绘制图表
1.Charts 简介 使用第三方框架 Charts 绘制 iOS 图表.GitHub 源码 Charts Charts 是一款用于绘制图表的框架,可以绘制柱状图.折线图.K线图.饼状图等.Chart ...
- 参数传递的四种形式----- URL,超链接,js,form表单
什么时候用GET, 查,删, 什么时候用POST,增,改 (特列:登陆用Post,因为不能让用户名和密码显示在URL上) 4种get传参方式 <html xmlns="http:/ ...
随机推荐
- 无忧代理免费ip爬取(端口js加密)
起因 为了训练爬虫技能(其实主要还是js技能-),翻了可能有反爬的网站挨个摧残,现在轮到这个网站了:http://www.data5u.com/free/index.shtml 解密过程 打开网站,在 ...
- java中的final和volatile详解
相比synchronized,final和volatile也是经常使用的关键字,下面聊一聊这两个关键字的使用和实现 1.使用 final使用: 修饰类表示该类为终态类,无法被继承 修饰方法表示该方法无 ...
- Java第7次实验提纲(多线程)
PTA与参考资料 题集:多线程 多线程实验参考文件 ThreadReading 实验-基础部分 1.1 基础题目MyThread类.自行完成题集合的:PrintTask 1.2 Runnable与匿名 ...
- 利用github webhook 结合openresty自动更新静态博客
使用hexo在github pages上弄了一个静态博客,后来觉得访问有点慢,于是放到自己vps上. 对于静态博客的部署非常简单,本来就是html,js,css等静态文件,只要nginx上配置下目录就 ...
- android addCategory()等说明
一.隐式意图介绍 显式意图我们前面已经提到,形如: Intent intent = new Intent(); intent.setClass(this,Other.class);//此句表示显式意图 ...
- 20160223.CCPP体系详解(0033天)
程序片段(01):MyArray.h+MyArray.c+main.c 内容概要:数组库 ///MyArray.h #pragma once #define DT int//类型通用 typedef ...
- Unity角色残影特效
残影特效在网上有很多例子,比如这个,我参考着自己整合了一下,算是整合了一个比较完整且特别简单易用的出来,只需要一个脚本挂上去无需任何设定就能用. 这里只针对SkinnedMeshRenderer的网格 ...
- Swift如何取得View所属的ViewController
从VC取得View很容易,但有些情况下我们需要从View反向获取VC. 不过在一些特殊的场合,Cocoa库帮我们想的很周到,比如在自定义View过渡动画的时候: func animateTransit ...
- linux真正使用shell脚本做定时任务 关键的Nohup
网上有很多的文章教大家使用定时任务,所以别的废话我就不多说了 我这里直接有SH来做定时,只是有一点大家不知道,一定要用Nohup,否则用户退出终端以后,SH任务会被自动终止掉 假设有一 tash.sh ...
- JSP自定义简单标签入门之带有属性
上面写的那个简单标签来控制页面中标签内容的重复次数是固定的,这就是权限"写死了",这是非常不好的行为,因为其灵活性太差.所以下面就介绍一种使用属性来控制标签内容输出次数的方法. 准 ...