关于charts的系列视图介绍传送门:

iOS 图表工具charts介绍

iOS 图表工具charts之LineChartView

iOS 图表工具charts之BarChartView

iOS 图表工具charts之PieChartView

iOS 图表工具charts之CandleStickChartView

iOS 图表工具charts之CombinedChartView

CandleStickChartView在charts中可以用来绘制K线图,由于charts是基于swift开发的,如果需要和objective-C混编(通过pod的方式不用管),可以参考我的上几篇文章iOS OC中桥接swift第三方库》,这里主要讲的是CandleStickChartView的一些常用属性和一些基本用法,实际情况以开发为准

CandleStickChartView的基本属性介绍

    CandleStickChartView *chartView = [[CandleStickChartView 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 kLineChart demo";
//关闭图例
chartView.legend.enabled = NO;
//缩放
chartView.scaleXEnabled = YES;
chartView.scaleYEnabled = NO;
chartView.autoScaleMinMaxEnabled = YES;
chartView.highlightPerTapEnabled = YES;
chartView.highlightPerDragEnabled = NO;
chartView.pinchZoomEnabled = NO; //手势捏合
chartView.dragEnabled = YES; //开启拖拽
chartView.dragDecelerationFrictionCoef = 0.5; //0 1 惯性
chartView.doubleTapToZoomEnabled = NO;
//代理
chartView.delegate = self; //leftAxis
ChartYAxis *rightAxis = chartView.rightAxis;
rightAxis.enabled = YES;
rightAxis.labelPosition = YAxisLabelPositionInsideChart;
rightAxis.drawGridLinesEnabled = YES;
rightAxis.gridLineDashLengths = @[@2,@4];
rightAxis.labelTextColor = UIColor.blackColor;
rightAxis.labelFont = [UIFont systemFontOfSize:10];
rightAxis.decimals = 2;
rightAxis.labelCount = 6; //设置显示最大点数
rightAxis.forceLabelsEnabled = YES; //强制label个数
// leftAxis.inverted = YES;
//设置样式
LeftAxisFormatter *leftFormatter = [LeftAxisFormatter new];
leftFormatter.digital = 2;
rightAxis.valueFormatter = leftFormatter; //rightAxis
ChartYAxis *leftAxis = chartView.leftAxis;
leftAxis.enabled = NO; //xAxis
ChartXAxis *xAxis = chartView.xAxis;
xAxis.enabled = YES;
xAxis.labelPosition = XAxisLabelPositionBottom;
xAxis.labelCount = 2; //设置显示点数
xAxis.forceLabelsEnabled = YES; //强制label个数
xAxis.avoidFirstLastClippingEnabled = YES; //避免文字显示不全 这个属性很重要
xAxis.granularityEnabled = YES; //设置重复不显示
//不画线
xAxis.drawGridLinesEnabled = NO;
xAxis.spaceMin = 0.5;
xAxis.spaceMax = 0.5;

CandleChartDataEntry代码每个柱子图形

    NSMutableArray *array = [NSMutableArray array];
for (int i = 0; i < datas.count; i++) {
NSDictionary *dict = datas[i];
double high = [dict[@"dbHighPrice"] doubleValue];
double low = [dict[@"dbLowPrice"] doubleValue];
double open = [dict[@"dbOpenPrice"] doubleValue];
double close = [dict[@"dbClosePrice"] doubleValue];
CandleChartDataEntry *entry = [[CandleChartDataEntry alloc] initWithX:(i) shadowH:high shadowL:low open:open close:close];
[array addObject:entry];
}

CandleChartDataSet代码所有的柱子的集合

    CandleChartDataSet *set = [[CandleChartDataSet alloc] initWithEntries:array label:@"kLine DataSet"];
//下降颜色 是否填充填充
set.decreasingColor = UIColorHex(0x32BE89);
set.decreasingFilled = YES;
//上升颜色 是否填充填充
set.increasingColor = UIColorHex(0xFD4C60);
set.increasingFilled = YES;
//显示美国线
//set.showCandleBar = NO;
//阴影线的宽度 颜色跟随
set.shadowWidth = 0.7;
set.shadowColorSameAsCandle = YES;
//k线柱间隙
set.barSpace = 0.15;
//是否现在十字标识
set.drawHorizontalHighlightIndicatorEnabled = NO;
set.drawVerticalHighlightIndicatorEnabled = NO;
//轴线方向
set.axisDependency = AxisDependencyRight;
//不显数字
set.drawValuesEnabled = NO;

CandleChartData

    //创建数据
CandleChartData *candleData = [[CandleChartData alloc] initWithDataSet:set];
//赋值
self.chartView.data = candleData;

一些需要注意的点:

1.显示美国线

    CandleChartDataSet:
set.showCandleBar = NO

2.影线的颜色跟随k线,这样就会根据k线值自动变成绿色或者红色

    CandleChartDataSet:
set.shadowWidth = 0.7;
set.shadowColorSameAsCandle = YES;

3.k线上升颜色,是否填充等属性,用来设置上升颜色下降颜色,是否是空心实心k线

    CandleChartDataSet:
//下降颜色 是否填充填充
set.decreasingColor = UIColorHex(0x32BE89);
set.decreasingFilled = YES;
//上升颜色 是否填充填充
set.increasingColor = UIColorHex(0xFD4C60);
set.increasingFilled = NO;

4.是否显示k线值

    CandleChartDataSet:
//不显数字
set.drawValuesEnabled = NO;

5.是否显示十字光标

    CandleChartDataSet:
set.drawHorizontalHighlightIndicatorEnabled = NO;
set.drawVerticalHighlightIndicatorEnabled = NO;

6.轴线方向 AxisDependencyRight 这样当我们滑动k线的时候k线会根据当前k线最大值和最小值调整缩放比例

    CandleChartDataSet:
//轴线方向
set.axisDependency = AxisDependencyRight;

7.k线设置缩放比例,最大缩放值 最小缩放值

    //最大缩放值 最小缩放值  y轴不缩放 minScl maxScl根据实际情况调整
[self.chartView setScaleMinima:minScl scaleY:1];
[self.chartView.viewPortHandler setMaximumScaleX:maxScl];

8.当前缩放比例设置

    CGAffineTransform srcMatrix = _chartView.viewPortHandler.touchMatrix;
//scl根据实际情况设置当前缩放比例
srcMatrix.a = scl;
[self.chartView.viewPortHandler refreshWithNewMatrix:srcMatrix chart:self.chartView invalidate:YES];

9.k线移动到指定位置 第一次需要滚动到最新值

    //开局移动到最右边xMax为最大x轴值
[self.chartView moveViewToX:self.chartView.data.xMax];

10.选中k线,拿到当前选中的点的x轴坐标,以便于拿到选中的model

//k线代理方法
-(void)chartValueSelected:(ChartViewBase *)chartView entry:(ChartDataEntry *)entry highlight:(ChartHighlight *)highlight{
NSInteger index = highlight.x;
//index为当前选中的x值
if(index < self.datas.count){
//根据当前的index能拿到对应的数据模型
Model *model = self.datas[index];
}
}

其他待续

转载请标注来源:https://www.cnblogs.com/qqcc1388/

iOS 图表工具charts之CandleStickChartView(K线)的更多相关文章

  1. iOS 图表工具charts之CombinedChartView

    关于charts的系列视图介绍传送门: iOS 图表工具charts介绍 iOS 图表工具charts之LineChartView iOS 图表工具charts之BarChartView iOS 图表 ...

  2. iOS 图表工具charts之LineChartView

    关于charts的系列视图介绍传送门: iOS 图表工具charts介绍 iOS 图表工具charts之LineChartView iOS 图表工具charts之BarChartView iOS 图表 ...

  3. iOS 图表工具charts介绍

    charts是一个很好的绘图工具,功能非常强大,可以用来绘制折线,柱状图,饼状图,k线图,k线分时图,雷达图,气泡图等等,charts是一款仿照安卓 MPAndroidChart而来的一个基于swif ...

  4. iOS 图表工具charts之BarChartView

    关于charts的系列视图介绍传送门: iOS 图表工具charts介绍 iOS 图表工具charts之LineChartView iOS 图表工具charts之BarChartView iOS 图表 ...

  5. iOS 图表工具charts之PieChartView

    关于charts的系列视图介绍传送门: iOS 图表工具charts介绍 iOS 图表工具charts之LineChartView iOS 图表工具charts之BarChartView iOS 图表 ...

  6. JHChart 1.1.0 iOS图表工具库中文ReadMe

    JHChart(最新版本1.1.0) 好吧,的确当前的github上已经存有不少的iOS图表工具库,然而,当公司的项目需要图表时,几乎没有哪个第三方能够完全满足我的项目需求.无奈之下,本人不得不花费一 ...

  7. JHChart iOS图表工具库1.0.3新版本详解

    前言. 从2016年4月14日开始,本人着手开发了JHChart图表工具库.经过断断续续的开发,截止到现在,已经实现了折线图.柱状图.饼状图.环形图和表格样式的图表功能.为了方便使用,我已经将一个简单 ...

  8. iOS图表库Charts集成与使用

    Charts是一个很优秀的图表库,它支持Android.iOS.tvOS和macOS,这样使用起来,可以节省学习成本,可以从GitHub上了解更多信息.本文记录在iOS项目上的集成与使用. Chart ...

  9. iOS 使用Charts框架 折线,柱状,K线,饼状,雷达全攻略

    我是前言: 大约几个月前我在某平台写了一篇文章, 文中简单地介绍了Charts两种图表的样式的使用, 不过有种意犹未尽的感觉, 利用周末的空闲时间再次看了看, 有了新的收获, 今天发出来,分享给大家, ...

随机推荐

  1. python学习-Python简介以及运行环境

    Python语言是全世界几百种编程语言中的一个,诞生时间不算长,但是现在已经成为很热门的语言,近几年在TIOBE排行榜一直呈现上升趋势,截止19年2月,python已经超过C++成为排名第三的语言. ...

  2. html和css牛刀小试

    html和css网上教程很多,这里我也给大家一个网址:https://www.cnblogs.com/majj/ 今天心血来潮就模仿着小米的官网写了部分代码,效果图如下:(本人故意加了个华为广告栏在最 ...

  3. ffmpeg函数02__swr_alloc_set_opts()

    SwrContext *swr_alloc(void);  // 分配重采样的上下文. SwrContext *swr_alloc_set_opts(struct SwrContext *s, int ...

  4. 【每日一包0003】kind-of

    github地址:https://github.com/ABCDdouyae... kind-of 判断数据类型用法:kind-of(date)返回:string 数据类型 January undef ...

  5. selenium相关导入By、Keys、WebDriverWait、ActionChains,显示等待与隐式等待

    # -*- coding: utf-8 -*- """ @author: Dell Created on Tue Dec 24 12:33:56 2019 "& ...

  6. JAVA》eclipse——(二)Tomcat

    一.进入www.apache.org网页(注:图中所有箭头都依据从左到右,从上到下的规则) 二.向下拉网页,然后如下图操作 三.进入之后,在网页的左边选择想要的Tomcat版本 四.选择与本机相同系统 ...

  7. dlerror和dlclose用法

    dlclose() 1. 包含头文件 #include<dlfcn.h> 2. 函数定义  int dlclose(void *handle) dlclose用于关闭指定句柄的动态链接库, ...

  8. buuctf@ciscn_2019_c_1

    from pwn import * context.log_level='debug' #io=remote('node3.buuoj.cn',29121) io=process('./ciscn_2 ...

  9. C# Winform程序之间如何传递和接收参数 Process

    Program: static class Program    { /// <summary>         /// 应用程序的主入口点.         /// </summa ...

  10. ELEMENT-UI 封装el-table 局部刷新row

    //关于封装的el-table行数据更新后如何局部更新row row.status=status; this.$set(this.$refs.elTable.$data.tableData,index ...