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];//开启borderchartView.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;//leftAxisChartYAxis *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;//rightAxisChartYAxis *rightAxis = chartView.rightAxis;rightAxis.enabled = NO;//xAxisChartXAxis *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];}//setBarChartDataSet *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/
转载于:https://www.cnblogs.com/qqcc1388/p/11171939.html
iOS 图表工具charts之BarChartView相关推荐
- iOS 图表工具charts之LineChartView
关于charts的系列视图介绍传送门: iOS 图表工具charts介绍 iOS 图表工具charts之LineChartView iOS 图表工具charts之BarChartView iOS 图表 ...
- JHChart 1.1.0 iOS图表工具库中文ReadMe
代码地址如下: http://www.demodashi.com/demo/11701.html 好吧,的确当前的[github]上已经存有不少的iOS图表工具库,然而,当公司的项目需要图表时,几乎没 ...
- cahrt框架 ios_iOS - Charts(一) - BarChartView
platform:ios,'10.0' inhibit_all_warnings! use_frameworks! target 'iOS_Charts' do pod 'Charts' #pod ' ...
- 如何在Swift中创建漂亮的iOS图表
通过图形和图表呈现数据是当今移动应用程序最显着的特征之一.iOS图表使应用程序看起来更漂亮,更有吸引力. 在本教程中,我们将向您展示如何使用代码示例在Swift中实现我们的iOS图表.我们将看一下Sw ...
- Android中MPAndroidChart图表工具的常用方法(汇总)
本篇主要是MPAndroidChart图表工具常用方法的汇总,其他不清楚的可以去GitHub上的文档查询. 强大的图表绘制工具,支持折线图.面积图.散点图.时间图.柱状图.条图.饼图.气泡图.圆环图. ...
- Superset【实践 01】数据可视化图表工具 Superset(Greenplum数据库连接举例+MySQL数据集+图表添加配置+仪表盘添加与设置)一文学会使用Superset
Superset 的使用跟 tableau.ganglia.grafana.永洪.帆软等的可视化图表工具类似,基本步骤都是配置数据源.配置SQL.配置图表参数等.下面针对Greenplum数据库进行一 ...
- R语言单变量分析实战:汇总统计(Summary Statistics)、频率表(Frequency Table)、图表(charts: boxplot、histogram、density)
R语言单变量分析实战:汇总统计(Summary Statistics).频率表(Frequency Table).图表(charts: boxplot.histogram.density) 目录
- Visio替代图表工具 - 为什么Visual Paradigm Online?
如果您曾尝试使用MSVisio®创建流程图,您知道这并不容易.Visual Paradigm Online(VP Online)更加用户友好且直观,更不用说它的成本更低且平台中立. 让我们来看看为什么 ...
- iOS开发工具——网络封包分析工具Charles
iOS开发工具--网络封包分析工具Charles 简介 Charles是在Mac下常用的截取网络封包的工具,在做iOS开发时,我们为了调试与服务器端的网络通讯协议,常常需要截取网络封包来分析.Char ...
最新文章
- easy-x库graphics.h图形库安装
- 如何创建共享文件夹?2009.6.10 三 紧张后 高兴
- 开发中遇到的问题,以及笔记
- 数据库mysql_row_MYSQL数据库mysql found_row()使用详解
- mysql 8小时问题_Mysql经典的“8小时问题”
- CSS实现div悬浮框的代码(兼容IE6)
- 【Linux网络编程学习】预备知识(网络字节序、IP地址转换函数、sockaddr数据结构)
- 【Java】基本语法训练题
- Windows mysql-64位 数据库安装
- Power BI Embedded 开发国内版-21V-版本实测
- MSB与LSB的含义
- iconv-----linux gbk 转 UTF-8
- win10打开蓝牙_win10动态锁,只要你离电脑远一点,电脑就自动锁定
- Windows 提权
- Google的“神秘链接”
- MATLAB必看书籍推荐
- 《The Elder Scrolls V: Skyrim》百般冷门却强力职业
- Java数据库连接池
- 多变量微积分笔记(1)——向量和矩阵
- 什么叫做「数据驱动方法」