# IOS端K线系列之K线-绘制OHLC图、线段、圆形

1. 蜡烛
2. OHLC
3. 线段
4. 带状
5. 圆形

## OHLC图

OK，知道思路以后，直接上代码：

/**
生成OHLC

@param model 蜡烛坐标模型
@return 返回图层
*/
+ (CAShapeLayer *)getOHLCLayerWithPointModel:(YKCandlePointModel *)model
{
//判断是否为涨跌
BOOL isRed = model.oPoint.y >= model.cPoint.y ? YES : NO;

UIBezierPath *path = [UIBezierPath bezierPath];
//绘制上下影线
[path moveToPoint:model.lPoint];
//开盘线
[path moveToPoint:model.oPoint];
//开盘线
[path moveToPoint:model.cPoint];

CAShapeLayer *layer = [CAShapeLayer layer];
layer.path = path.CGPath;

//判断涨跌来设置颜色
if (isRed)
{
//涨，设置红色
layer.strokeColor = [UIColor redColor].CGColor;
} else
{
//跌，设置绿色
layer.strokeColor = [UIColor greenColor].CGColor;
}
layer.fillColor = [UIColor clearColor].CGColor;

return layer;
}

## 单、多条线

/**
生成单条线

@param pointArr 坐标点数组
@param lineColor 线颜色
@return 返回线段图层
*/
+ (CAShapeLayer *)getSingleLineLayerWithPointArray:(NSArray *)pointArr lineColor:(UIColor *)lineColor
{
UIBezierPath *path = [UIBezierPath getBezierPathWithPointArr:pointArr];

CAShapeLayer *layer = [CAShapeLayer layer];
layer.path = path.CGPath;
layer.lineWidth = 1.f;
layer.strokeColor = lineColor.CGColor;
layer.fillColor = [UIColor clearColor].CGColor;

return layer;
}

/**
生成包含多条线的线段

@param pointArr 坐标点数组
@param lineColor 线颜色
@return 返回线段图层
*/
+ (CAShapeLayer *)getMultipleLineLayerWithPointArray:(NSArray *)pointArr lineColor:(UIColor *)lineColor
{
UIBezierPath *path = [UIBezierPath bezierPath];

for (int idxX=0; idxX<pointArr.count; idxX++)
{
NSArray *idxXArr = pointArr[idxX];

[path moveToPoint:[[idxXArr firstObject] CGPointValue]];
for (int idxY=1; idxY<idxXArr.count; idxY++)
{
}
}

CAShapeLayer *layer = [CAShapeLayer layer];
layer.path = path.CGPath;
layer.lineWidth = 1.f;
layer.strokeColor = lineColor.CGColor;
layer.fillColor = [UIColor clearColor].CGColor;

return layer;
}

## 圆形

/**
生成圆

@param point 圆心坐标
@param color 圆颜色
@return 返回图层
*/
+ (CAShapeLayer *)getCircleLayerWithPoint:(CGPoint )point color:(UIColor *)color
{
//直接用圆描述路径
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:point radius:3.f startAngle:0.f
endAngle:(CGFloat)M_PI * 2 clockwise:true];
//生成图层 并设置路径和属性
CAShapeLayer *layer = [CAShapeLayer layer];
layer.path = path.CGPath;
layer.fillColor = color.CGColor;
layer.strokeColor = color.CGColor;

return layer;
}