iOS 图表工具charts之BarChartView
关于charts的系列视图介绍传送门:
iOS 图表工具charts介绍
iOS 图表工具charts之LineChartView
iOS 图表工具charts之BarChartView
iOS 图表工具charts之PieChartView
iOS 图表工具charts之CandleStickChartView
iOS 图表工具charts之CombinedChartView
BarChartView在charts中可以用来绘制柱状图,由于charts是基于swift开发的,如果需要和objective-C混编(通过pod的方式不用管),可以参考我的上几篇文章《iOS OC中桥接swift第三方库》,这里主要讲的是LineChartView的一些常用属性和一些基本用法,实际情况以开发为准
chartView的更加细节的属性设置 请查看我的上篇文章《iOS 图表工具charts之LineChartView》,因为BarChartView大部分基础属性都差不多,这里就不详细写注释了
BarChartView的基础设置
-(void)setupUI{
//BarChartView默认纵向展示柱状图, 如果需要横向展示 则创建HorizontalBarChartView即可
BarChartView *chartView = [[BarChartView alloc] init];
//设置偏移
[chartView setExtraOffsetsWithLeft:10 top:10 right:10 bottom:10];
//开启border
chartView.drawBordersEnabled = YES;
chartView.borderLineWidth = .5f;
chartView.borderColor = UIColor.blackColor;
//设置背景
chartView.drawGridBackgroundEnabled = NO;
chartView.gridBackgroundColor = [UIColor grayColor];
//无内容显示
chartView.noDataText = @"";
//关闭描述
chartView.chartDescription.enabled = NO;
chartView.chartDescription.text = @"tiny`s barChart demo";
//关闭图例
chartView.legend.enabled = NO;
//缩放
chartView.scaleXEnabled = NO;
chartView.scaleYEnabled = NO;
chartView.autoScaleMinMaxEnabled = YES;
chartView.highlightPerTapEnabled = NO;
chartView.highlightPerDragEnabled = NO;
chartView.pinchZoomEnabled = NO; //手势捏合
chartView.dragEnabled = YES;
chartView.dragDecelerationFrictionCoef = 0.5; //0 1 惯性
//代理
chartView.delegate = self;
//leftAxis
ChartYAxis *leftAxis = chartView.leftAxis;
leftAxis.enabled = YES;
leftAxis.labelPosition = YAxisLabelPositionOutsideChart;
leftAxis.drawGridLinesEnabled = YES;
leftAxis.gridLineDashLengths = @[@2,@4];
leftAxis.labelTextColor = UIColor.blackColor;
leftAxis.labelFont = [UIFont systemFontOfSize:10];
leftAxis.decimals = 2;
//设置样式
LeftAxisFormatter *leftFormatter = [LeftAxisFormatter new];
leftAxis.valueFormatter = leftFormatter;
//rightAxis
ChartYAxis *rightAxis = chartView.rightAxis;
rightAxis.enabled = NO;
//xAxis
ChartXAxis *xAxis = chartView.xAxis;
xAxis.enabled = YES;
xAxis.labelPosition = XAxisLabelPositionBottom;
//不画线
xAxis.drawGridLinesEnabled = NO;
BarxAxisFormatter *xFormatter = [BarxAxisFormatter new];
xAxis.valueFormatter = xFormatter;
xFormatter.titles = @[@"语文",@"数学",@"外语",@"物理"];
self.charView = chartView;
[self addSubview:self.charView];
[self.charView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.mas_offset(0);
}];
//draw
[self drawData];
//执行动画
[self.charView animateWithYAxisDuration:1.f];
}
-(void)drawData{
NSArray *datas = @[@100,@90,@76,@55,@45,@77,@98,@62];
NSMutableArray *array = [NSMutableArray array];
for (int i = 0; i < datas.count; i++) {
BarChartDataEntry *entry = [[BarChartDataEntry alloc] initWithX:i y:[datas[i] integerValue]];
[array addObject:entry];
}
//set
BarChartDataSet *set = [[BarChartDataSet alloc] initWithEntries:array label:@"Bar DataSet"];
[set setColors:@[UIColor.redColor,UIColor.blueColor,UIColor.blueColor,UIColor.blackColor,UIColor.cyanColor,UIColor.grayColor,UIColor.greenColor,UIColor.cyanColor]];
//显示柱图值并格式化
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
numberFormatter.positiveSuffix = @"分";
ChartDefaultValueFormatter *formatter = [[ChartDefaultValueFormatter alloc] initWithFormatter:numberFormatter];
[set setValueFormatter:formatter];
set.highlightEnabled = NO;
BarChartData *data = [[BarChartData alloc] initWithDataSet:set];
self.charView.data = data;
}
#pragma mark - ChartViewDelegate
#pragma mark 图表中数值被选中
-(void)chartValueSelected:(ChartViewBase *)chartView entry:(ChartDataEntry *)entry highlight:(ChartHighlight *)highlight{
// NSLog(@"图表中数值被选中");
}
#pragma mark 图表中的空白区域被选中
-(void)chartValueNothingSelected:(ChartViewBase *)chartView{
// NSLog(@"空白区域被选中");
}
#pragma mark 图表被缩放
-(void)chartScaled:(ChartViewBase *)chartView scaleX:(CGFloat)scaleX scaleY:(CGFloat)scaleY{
// NSLog(@"图表被缩放");
}
#pragma mark 图表被移动
-(void)chartTranslated:(ChartViewBase *)chartView dX:(CGFloat)dX dY:(CGFloat)dY{
// NSLog(@"图表被移动");
}
一些需要注意的点:
1.如果需要给每个bar显示的数字格式化比如 100分, 100t,100g等等添加前缀或者后缀,则可以设置BarChartDataSet属性
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
numberFormatter.positiveSuffix = @"分";
ChartDefaultValueFormatter *formatter = [[ChartDefaultValueFormatter alloc] initWithFormatter:numberFormatter];
[set setValueFormatter:formatter];
2.柱状图动画效果
//执行动画
[self.charView animateWithYAxisDuration:1.f];
3.颜色可以没每个柱子设置颜色
[set setColors:@[UIColor.redColor,UIColor.blueColor,UIColor.blueColor,UIColor.blackColor,UIColor.cyanColor,UIColor.grayColor,UIColor.greenColor,UIColor.cyanColor]];
4.每个点的数值要显示出来
set.drawValuesEnabled = YES;
5.柱状图横向显示,只需要将BarChartView换成HorizontalBarChartView即可,其他不变 效果图如下:
6.BarChartData可以添加多个柱子
BarChartData *data = [[BarChartData alloc] init]
[data addDataSet:set]
转载请标注来源:https://www.cnblogs.com/qqcc1388/
iOS 图表工具charts之BarChartView的更多相关文章
- iOS 图表工具charts之CombinedChartView
关于charts的系列视图介绍传送门: iOS 图表工具charts介绍 iOS 图表工具charts之LineChartView iOS 图表工具charts之BarChartView iOS 图表 ...
- iOS 图表工具charts之CandleStickChartView(K线)
关于charts的系列视图介绍传送门: iOS 图表工具charts介绍 iOS 图表工具charts之LineChartView iOS 图表工具charts之BarChartView iOS 图表 ...
- iOS 图表工具charts之PieChartView
关于charts的系列视图介绍传送门: iOS 图表工具charts介绍 iOS 图表工具charts之LineChartView iOS 图表工具charts之BarChartView iOS 图表 ...
- iOS 图表工具charts之LineChartView
关于charts的系列视图介绍传送门: iOS 图表工具charts介绍 iOS 图表工具charts之LineChartView iOS 图表工具charts之BarChartView iOS 图表 ...
- iOS 图表工具charts介绍
charts是一个很好的绘图工具,功能非常强大,可以用来绘制折线,柱状图,饼状图,k线图,k线分时图,雷达图,气泡图等等,charts是一款仿照安卓 MPAndroidChart而来的一个基于swif ...
- JHChart 1.1.0 iOS图表工具库中文ReadMe
JHChart(最新版本1.1.0) 好吧,的确当前的github上已经存有不少的iOS图表工具库,然而,当公司的项目需要图表时,几乎没有哪个第三方能够完全满足我的项目需求.无奈之下,本人不得不花费一 ...
- JHChart iOS图表工具库1.0.3新版本详解
前言. 从2016年4月14日开始,本人着手开发了JHChart图表工具库.经过断断续续的开发,截止到现在,已经实现了折线图.柱状图.饼状图.环形图和表格样式的图表功能.为了方便使用,我已经将一个简单 ...
- iOS图表库Charts集成与使用
Charts是一个很优秀的图表库,它支持Android.iOS.tvOS和macOS,这样使用起来,可以节省学习成本,可以从GitHub上了解更多信息.本文记录在iOS项目上的集成与使用. Chart ...
- .net图表工具汇总
概述:图形图表的可视化数据表现形式已成为一种趋势,本文推荐了10款非常好用的.NET图表控件,希望对广大.NET图表开发者能有所帮助. 读图时代,图形图表的可视化数据表现形式已成为一种趋势.因为图表能 ...
随机推荐
- linux上安装rz和sz
简介 lrzsz 官网入口:http://freecode.com/projects/lrzsz/ lrzsz是一个unix通信套件提供的X,Y,和ZModem文件传输协议 windows 需要向ce ...
- 20199319《Linux内核原理与分析》第十一周作业
ShellShock攻击实验 什么是ShellShock Shellshock,又称Bashdoor,是在Unix中广泛使用的Bash shell中的一个安全漏洞,首次于2014年9月24日公开.许多 ...
- Python time、datetime、os、random、sys、hashlib、json、shutil、logging、paramiko、subprocess、ConfigParser、xml、shelve模块的使用
文章目录: 1. time & datetime模块 2. os模块 3. random模块 4. sys模块 5. hashlib模块 6. json模块 7. shutil模块 8. lo ...
- Python基础:深浅拷贝
对于数字.字符串深浅拷贝: import copy num = 0 copy_num = copy.copy(num) print("These are normal copy") ...
- Linux tcpdump命令详解与Wireshark
简介 用简单的话来定义tcpdump,就是:dump the traffic on a network,根据使用者的定义对网络上的数据包进行截获的包分析工具. tcpdump可以将网络中传送的数据包的 ...
- 【BZOJ2752】【Luogu P2221】 [HAOI2012]高速公路
不是很难的一个题目.正确思路是统计每一条边被经过的次数,但我最初由于习惯直接先上了一个前缀和再推的式子,导致极其麻烦难以写对而且会爆\(longlong\). 推导过程请看这里. #include & ...
- django初步--+urls解析
1.静态文件配置: 你在浏览器中输入网址能够有响应的资源返回给你 是因为后端已经提前给你开设该资源的接口,也就意味着你所能 访问到的资源 都是人家事先定义好的. 2.django如何给用户开设资源接口 ...
- 01-01 Web应用
一 Web应用的组成 接下来我们学习的目的是为了开发一个Web应用程序,而Web应用程序是基于B/S架构的,其中B指的是浏览器,负责向S端发送请求信息,而S端会根据接收到的请求信息返回相应的数据给浏览 ...
- 【JZOJ5439】【NOIP2017提高A组集训10.31】Calculate
题目 分析 对于\[\sum_{i=1}^{n}\lfloor\dfrac{T-B_i}{A_i}\rfloor\] 我们考虑拆开处理,得到 \[\sum_{i=1}^{n}(\lfloor\dfra ...
- JZOJ5358【NOIP2017提高A组模拟9.12】BBQ
题目 分析 发现,\(C_{ai+aj+bi+bj}^{ai+aj}\),其实就等于从(0,0)走最短路到(ai+aj,bi+bj). 我们可以想办法将i.j分开,从(0,0)走最短路到(ai+aj, ...