开始之前讲述本人遇到的一个问题,就是K线行情分时对应点的时间不太准确。
思路:安卓版的时间是否是准确的,得到回复正确;去请求接口获取到对应model中的Date格式,是以时间戳的形式展现出来,时间戳是10位,这不得上亿的数值,这就涉及到精度问题。从绘制底部时间的方法实现算法中查找涉及到精度的代码
NSDate *date = [NSDate dateWithTimeIntervalSince1970:(self.needDrawKLineModels[idx].Date.floatValue)/1000];
改为
NSDate *date = [NSDate dateWithTimeIntervalSince1970:(self.needDrawKLineModels[idx].Date.doubleValue)/1000];
理由是:当我们针对于精确到分的时候 floatValue精度远小于doubleValue
K线(蜡烛)的一个点是由当前时间对应大开盘价,收盘价,最高价,最低价组成,思路就是绘制四个点然后根据与收盘价和开盘价值进行比较判断涨跌颜色变化。
具体思路:
001 将数据转化成对应的坐标 以时间为X轴 分别生成四个Point
002 绘制这些坐标
1.新建蜡烛Point
2.新建蜡烛Frame
3.创建贝塞尔曲线,绘制上下影线
4.创建蜡烛layer
代码如下
+ (CAShapeLayer *)fl_getCandleLayerWithPointModel:(Y_KLinePositionModel *)model candleWidth:(CGFloat)candleWidth {
//判断是否为涨跌
BOOL isRed = (model.ClosePoint.y - model.OpenPoint.y < 0) ? YES : NO;
CGPoint candlePoint = CGPointMake(isRed ? model.ClosePoint.x : model.OpenPoint.x, isRed ? model.ClosePoint.y : model.OpenPoint.y);
CGRect candleFrame = CGRectMake(candlePoint.x - candleWidth/2 , candlePoint.y, candleWidth, ABS(model.OpenPoint.y - model.ClosePoint.y));
// NSLog(@"%@",NSStringFromCGRect(candleFrame));
UIBezierPath *path = [UIBezierPath bezierPathWithRect:candleFrame];
// //绘制上下影线
[path moveToPoint:model.LowPoint];
[path addLineToPoint:model.HighPoint];
CAShapeLayer *layer = [CAShapeLayer layer];
layer.path = path.CGPath;
layer.lineWidth = 1.0f;
//判断涨跌来设置颜色
if (model.OpenPoint.y - model.ClosePoint.y < 0) {
//跌,设置红色
layer.strokeColor = [UIColor decreaseColor].CGColor;
layer.fillColor = [UIColor decreaseColor].CGColor;
}
else {
//不跌
layer.strokeColor = [UIColor increaseColor].CGColor;
layer.fillColor = [UIColor increaseColor].CGColor;
}
return layer;
}