K线图实现

作者: FelixSpeck | 来源:发表于2017-11-24 15:24 被阅读41次

写了lines、RKLineView、getData三个对象完成实现。

首先开始调用

rkLine= [[RKLineViewalloc]init];

CGRectframe =riKView.frame;

frame.origin=CGPointMake(0,5);

frame.size=CGSizeMake(315,219);

rkLine.frame= frame;

rkLine.xWidth=315;

rkLine.yHeight=219;

rkLine.kLineWidth=4;

rkLine.kLinePadding=1.25;

rkLine.riKArry= [dicobjectForKey:@"rows"];

[riKViewaddSubview:rkLine];

[rkLinestart];//线图运行

其它3个对象部分代码如下

#import

@interfacelines :UIView

@property(nonatomic,assign)CGPointstartPoint;//线条起点

@property(nonatomic,assign)CGPointendPoint;//线条终点

@property(nonatomic,retain)NSArray*points;//多点连线数组

@property(nonatomic,retain)NSString*color;//线条颜色

@property(nonatomic,assign)CGFloatlineWidth;//线条宽度

@property(nonatomic,assign)BOOLisK;//是否是实体K线默认是连接线

@property(nonatomic,assign)BOOLisVol;//是否是画成交量的实体

@end

#import"lines.h"

#import"colorModel.h"

#import"UIColor+helper.h"

@interfacelines(){

}

@end

@implementationlines

- (id)initWithFrame:(CGRect)frame

{

self= [superinitWithFrame:frame];

if(self) {

// Initialization code

[selfinitSet];

}

returnself;

}

#pragma mark初始化参数

-(void)initSet{

self.backgroundColor= [UIColorclearColor];

self.startPoint=self.frame.origin;

self.endPoint=self.frame.origin;

self.color=@"#000000";

self.lineWidth=1.0f;

self.isK=NO;

self.isVol=NO;

}

-(void)drawRect:(CGRect)rect

{

CGContextRefcontext =UIGraphicsGetCurrentContext();//获取绘图上下文

if(self.isK) {

//画k线

for(NSArray*iteminself.points) {

//转换坐标

CGPointheightPoint,lowPoint,openPoint,closePoint;

heightPoint =CGPointFromString([itemobjectAtIndex:0]);

lowPoint =CGPointFromString([itemobjectAtIndex:1]);

openPoint =CGPointFromString([itemobjectAtIndex:2]);

closePoint =CGPointFromString([itemobjectAtIndex:3]);

[selfdrawKWithContext:contextheight:heightPointLow:lowPointopen:openPointclose:closePointwidth:self.lineWidth];

}

}else{

//画连接线

[selfdrawLineWithContext:context];

}

}

#pragma mark画连接线

-(void)drawLineWithContext:(CGContextRef)context{

CGContextSetLineWidth(context,self.lineWidth);

//NSLog(@"self.lineWidth:%f",self.lineWidth);

CGContextSetShouldAntialias(context,YES);

colorModel*colormodel = [UIColorRGBWithHexString:self.colorwithAlpha:self.alpha];//设置颜色

CGContextSetRGBStrokeColor(context, (CGFloat)colormodel.R/255.0f, (CGFloat)colormodel.G/255.0f, (CGFloat)colormodel.B/255.0f,self.alpha);

if(self.startPoint.x==self.endPoint.x&&self.endPoint.y==self.startPoint.y) {

//定义多个个点画多点连线

for(iditeminself.points) {

CGPointcurrentPoint =CGPointFromString(item);

if((int)currentPoint.y<(int)self.frame.size.height&& currentPoint.y>0) {

if([self.pointsindexOfObject:item]==0) {

CGContextMoveToPoint(context, currentPoint.x, currentPoint.y);

continue;

}

CGContextAddLineToPoint(context, currentPoint.x, currentPoint.y);

CGContextStrokePath(context);//开始画线

if([self.pointsindexOfObject:item]

CGContextMoveToPoint(context, currentPoint.x, currentPoint.y);

}

}

}

}else{

//定义两个点画两点连线

constCGPointpoints[] = {self.startPoint,self.endPoint};

CGContextStrokeLineSegments(context, points,2);//绘制线段(默认不绘制端点)

}

}

#pragma mark画一根K线

-(void)drawKWithContext:(CGContextRef)context height:(CGPoint)heightPoint Low:(CGPoint)lowPoint open:(CGPoint)openPoint close:(CGPoint)closePoint width:(CGFloat)width{

CGContextSetShouldAntialias(context,NO);

//首先判断是绿的还是红的,根据开盘价和收盘价的坐标来计算

BOOLisKong =NO;

colorModel*colormodel = [UIColorRGBWithHexString:@"#FF0000"withAlpha:self.alpha];//设置默认红色

//如果开盘价坐标在收盘价坐标上方则为绿色即空

if(openPoint.y

isKong =YES;

colormodel = [UIColorRGBWithHexString:@"#00a900"withAlpha:self.alpha];//设置为绿色

}

//设置颜色

CGContextSetRGBStrokeColor(context, (CGFloat)colormodel.R/255.0f, (CGFloat)colormodel.G/255.0f, (CGFloat)colormodel.B/255.0f,self.alpha);

//首先画一个垂直的线包含上影线和下影线

//定义两个点画两点连线

if(!self.isVol) {

CGContextSetLineWidth(context,1);//上下阴影线的宽度

if(self.lineWidth<=2) {

CGContextSetLineWidth(context,0.5);//上下阴影线的宽度

}

constCGPointpoints[] = {heightPoint,lowPoint};

CGContextStrokeLineSegments(context, points,2);//绘制线段(默认不绘制端点)

}

//再画中间的实体

CGContextSetLineWidth(context, width);//改变线的宽度

CGFloathalfWidth =0;//width/2;

//纠正实体的中心点为当前坐标

openPoint =CGPointMake(openPoint.x-halfWidth, openPoint.y);

closePoint =CGPointMake(closePoint.x-halfWidth, closePoint.y);

if(self.isVol) {

openPoint =CGPointMake(heightPoint.x-halfWidth, heightPoint.y);

closePoint =CGPointMake(lowPoint.x-halfWidth, lowPoint.y);

}

//开始画实体

constCGPointpoint[] = {openPoint,closePoint};

CGContextStrokeLineSegments(context, point,2);//绘制线段(默认不绘制端点)

}

@end

#import

typedefvoid(^updateBlock)(id);

@interfaceRKLineView :UIView

@property(nonatomic,copy)updateBlockfinishUpdateBlock;//定义一个block回调更新界面

@property(nonatomic,assign)CGFloatxWidth;// x轴宽度

@property(nonatomic,assign)CGFloatyHeight;// y轴高度

@property(nonatomic,assign)CGFloatbottomBoxHeight;// y轴高度

@property(nonatomic,assign)CGFloatkLineWidth;// k线的宽度用来计算可存放K线实体的个数,也可以由此计算出起始日期和结束日期的时间段

@property(nonatomic,assign)CGFloatkLinePadding;

@property(nonatomic,assign)intkCount;// k线中实体的总数通过xWidth / kLineWidth计算而来

@property(nonatomic,retain)UIFont*font;

@property(nonatomic,retain)NSMutableArray*data;

@property(nonatomic,retain)NSDate*startDate;

@property(nonatomic,retain)NSDate*endDate;

@property(nonatomic,retain)NSMutableArray*riKArry;//分时数据数组

@property(nonatomic,retain)NSString*jrkp;//今日开盘

@property(nonatomic,retain)NSString*zrsp;//昨日收盘

-(void)start;

-(void)update;

@end

#import"RKLineView.h"

#import"lines.h"

#import"UIColor+helper.h"

#import"getData.h"

#import"commond.h"

@interfaceRKLineView()

{

NSThread*thread;

UIView*mainboxView;// k线图控件

UIView*bottomBoxView;//成交量

getData*getdata;

UIView*movelineone;//手指按下后显示的两根白色十字线

UIView*movelinetwo;

UIView*xxView;//展现信息

UILabel*riqLable;//日期

UILabel*kpLable;//开盘

UILabel*zgLable;//最高

UILabel*zdLable;//最低

UILabel*spLable;//收盘

UILabel*cjlLable;//成交量

UILabel*cjeLable;//成交额

UILabel*zflLable;//涨幅

UILabel*movelinetwoLable;

NSMutableArray*pointArray;// k线所有坐标数组

UILabel*startDateLab;

UILabel*endDateLab;

UILabel*volMaxValueLab;//显示成交量最大值

BOOLisUpdate;

BOOLisUpdateFinish;

NSMutableArray*lineArray ;// k线数组

NSMutableArray*lineOldArray ;// k线数组

UIPinchGestureRecognizer*pinchGesture;

CGPointtouchViewPoint;

BOOLisPinch;

UILabel*MA5;// 5均线显示

UILabel*MA10;// 10均线

UILabel*MA20;// 20均线

UILabel*jg1;//价格1

UILabel*jg2;//价格2

UILabel*jg3;//价格3

UILabel*jg4;//价格4

UILabel*jg5;//价格5

UILabel*zfl1;//涨幅率1

UILabel*zfl2;//涨幅率2

UILabel*zfl3;//涨幅率3

UILabel*zfl4;//涨幅率4

UILabel*zfl5;//涨幅率5

UILabel*shou1;//手1

UILabel*shou2;//手2

UILabel*shou3;//手3

}

@end

@implementationRKLineView

-(id)init{

self= [superinit];

[selfinitSet];

returnself;

}

-(void)initSet

{

self.font= [UIFontsystemFontOfSize:8];

isUpdate=NO;

isUpdateFinish=YES;

isPinch=NO;

lineArray= [[NSMutableArrayalloc]init];

lineOldArray= [[NSMutableArrayalloc]init];

self.finishUpdateBlock= ^(idself){

[selfupdateNib];//十字线显示的view

};

}

#pragma mark更新界面等信息

-(void)updateNib{

//NSLog(@"block");

if(movelineone==Nil) {

movelineone= [[UIViewalloc]initWithFrame:CGRectMake(0,0,0.5,

bottomBoxView.frame.size.height+bottomBoxView.frame.origin.y)];

movelineone.backgroundColor= [UIColorcolorWithHexString:@"#333333"withAlpha:0.5];

[mainboxViewaddSubview:movelineone];

movelineone.hidden=YES;

}

if(movelinetwo==Nil) {

movelinetwo= [[UIViewalloc]initWithFrame:CGRectMake(0,0,mainboxView.frame.size.width,1)];

movelinetwo.backgroundColor= [UIColorcolorWithHexString:@"#333333"withAlpha:0.5];

movelinetwo.hidden=YES;

[mainboxViewaddSubview:movelinetwo];

}

if(xxView==Nil) {

CGRectoneFrame =movelineone.frame;

oneFrame.size=CGSizeMake(60,73);

xxView= [[UIViewalloc]initWithFrame:oneFrame];

xxView.backgroundColor= [UIColorcolorWithHexString:@"#333333"withAlpha:1];

xxView.hidden=YES;

riqLable= [[UILabelalloc]initWithFrame:CGRectMake(2,1,60,8)];

riqLable.font= [UIFontsystemFontOfSize:8];

riqLable.textColor= [UIColorwhiteColor];

kpLable= [[UILabelalloc]initWithFrame:CGRectMake(2,10,60,8)];

kpLable.font= [UIFontsystemFontOfSize:8];

kpLable.textColor= [UIColorwhiteColor];

zgLable= [[UILabelalloc]initWithFrame:CGRectMake(2,19,60,8)];

zgLable.font= [UIFontsystemFontOfSize:8];

zgLable.textColor= [UIColorwhiteColor];

zdLable= [[UILabelalloc]initWithFrame:CGRectMake(2,28,60,8)];

zdLable.font= [UIFontsystemFontOfSize:8];

zdLable.textColor= [UIColorwhiteColor];

spLable= [[UILabelalloc]initWithFrame:CGRectMake(2,37,60,8)];

spLable.font= [UIFontsystemFontOfSize:8];

spLable.textColor= [UIColorwhiteColor];

cjlLable= [[UILabelalloc]initWithFrame:CGRectMake(2,46,60,8)];

cjlLable.font= [UIFontsystemFontOfSize:8];

cjlLable.textColor= [UIColorwhiteColor];

cjeLable= [[UILabelalloc]initWithFrame:CGRectMake(2,55,60,8)];

cjeLable.font= [UIFontsystemFontOfSize:8];

cjeLable.textColor= [UIColorwhiteColor];

zflLable= [[UILabelalloc]initWithFrame:CGRectMake(2,64,60,8)];

zflLable.font= [UIFontsystemFontOfSize:8];

zflLable.textColor= [UIColorwhiteColor];

[xxViewaddSubview:riqLable];

[xxViewaddSubview:kpLable];

[xxViewaddSubview:zgLable];

[xxViewaddSubview:zdLable];

[xxViewaddSubview:spLable];

[xxViewaddSubview:cjlLable];

[xxViewaddSubview:cjeLable];

[xxViewaddSubview:zflLable];

[mainboxViewaddSubview:xxView];

}

if(movelinetwoLable==Nil) {

CGRectoneFrame =movelinetwo.frame;

oneFrame.size=CGSizeMake(36,12);

movelinetwoLable= [[UILabelalloc]initWithFrame:oneFrame];

movelinetwoLable.font=self.font;

movelinetwoLable.layer.cornerRadius=5;

movelinetwoLable.backgroundColor= [UIColorcolorWithHexString:@"#333333"withAlpha:1];

movelinetwoLable.textColor= [UIColorwhiteColor];

movelinetwoLable.textAlignment=UITextAlignmentCenter;

movelinetwoLable.hidden=YES;

[mainboxViewaddSubview:movelinetwoLable];

}

movelineone.frame=CGRectMake(touchViewPoint.x,0,0.5,

bottomBoxView.frame.size.height+bottomBoxView.frame.origin.y);

movelinetwo.frame=CGRectMake(0,touchViewPoint.y,mainboxView.frame.size.width,0.5);

CGRectoneFrame =movelineone.frame;

oneFrame.size=CGSizeMake(60,73);

xxView.frame= oneFrame;

CGRecttowFrame =movelinetwo.frame;

towFrame.size=CGSizeMake(36,12);

movelinetwoLable.frame= towFrame;

movelineone.hidden=NO;

movelinetwo.hidden=NO;

xxView.hidden=NO;

movelinetwoLable.hidden=NO;

[selfisKPointWithPoint:touchViewPoint];//一点击就显示十字状态

}

-(void)start{

[selfdrawBox];

thread= [[NSThreadalloc]initWithTarget:selfselector:@selector(drawLine)object:nil];

[threadstart];

}

#pragma mark画框框和平均线

-(void)drawBox{

//画个k线图的框框

if(mainboxView==nil) {

mainboxView= [[UIViewalloc]initWithFrame:CGRectMake(0,0,315,130)];

mainboxView.layer.borderColor= [UIColorcolorWithHexString:@"#444444"withAlpha:0.5].CGColor;

mainboxView.layer.borderWidth=0.5;

mainboxView.userInteractionEnabled=YES;

[selfaddSubview:mainboxView];

//添加手指捏合手势,放大或缩小k线图

pinchGesture= [[UIPinchGestureRecognizeralloc]initWithTarget:selfaction:@selector(touchBoxAction:)];

[mainboxViewaddGestureRecognizer:pinchGesture];

UILongPressGestureRecognizer*longPressGestureRecognizer = [[UILongPressGestureRecognizeralloc]init];

[longPressGestureRecognizeraddTarget:selfaction:@selector(gestureRecognizerHandle:)];

[longPressGestureRecognizersetMinimumPressDuration:0.3f];

[longPressGestureRecognizersetAllowableMovement:36.0];

[selfaddGestureRecognizer:longPressGestureRecognizer];

}

if(!isUpdate) {

// k线图分割线

CGFloatpadRealValue =mainboxView.frame.size.height/4;

for(inti =0; i<4; i++) {

CGFloaty =mainboxView.frame.size.height-padRealValue * i;

lines*line = [[linesalloc]initWithFrame:CGRectMake(0,0,mainboxView.frame.size.width,mainboxView.frame.size.height)];

line.color=@"#999999";

line.alpha=0.5;

line.startPoint=CGPointMake(0, y);

line.endPoint=CGPointMake(mainboxView.frame.size.width, y);

[mainboxViewaddSubview:line];

}

}

//显示开始日期控件

if(startDateLab==nil) {

startDateLab= [[UILabelalloc]initWithFrame:CGRectMake(1

,131

,50,8)];

startDateLab.font= [UIFontsystemFontOfSize:8];

startDateLab.text=@"--";

startDateLab.textColor= [UIColorgrayColor];

startDateLab.backgroundColor= [UIColorclearColor];

[mainboxViewaddSubview:startDateLab];

}

//显示结束日期控件

if(endDateLab==nil) {

endDateLab= [[UILabelalloc]initWithFrame:CGRectMake(270

,131

,50,8)];

endDateLab.font= [UIFontsystemFontOfSize:8];

endDateLab.text=@"--";

endDateLab.textColor= [UIColorgrayColor];

endDateLab.backgroundColor= [UIColorclearColor];

[mainboxViewaddSubview:endDateLab];

}

// MA5均线价格显示控件

if(MA5==nil) {

MA5= [[UILabelalloc]initWithFrame:CGRectMake(100, -8,30,8)];

MA5.backgroundColor= [UIColorclearColor];

MA5.font= [UIFontsystemFontOfSize:8];

MA5.text=@"MA5";

MA5.textColor= [UIColorcolorWithHexString:@"#d83dd9"withAlpha:1];

[MA5sizeToFit];

[selfaddSubview:MA5];

}

// MA10均线价格显示控件

if(MA10==nil) {

MA10= [[UILabelalloc]initWithFrame:CGRectMake(MA5.frame.origin.x+MA5.frame.size.width+10, -8,30,8)];

MA10.backgroundColor= [UIColorclearColor];

MA10.font= [UIFontsystemFontOfSize:8];

MA10.text=@"MA10";

MA10.textColor= [UIColorcolorWithHexString:@"#ebac31"withAlpha:1];

[MA10sizeToFit];

[selfaddSubview:MA10];

}

// MA20均线价格显示控件

if(MA20==nil) {

MA20= [[UILabelalloc]initWithFrame:CGRectMake(MA10.frame.origin.x+MA10.frame.size.width+10, -8,30,8)];

MA20.backgroundColor= [UIColorclearColor];

MA20.font= [UIFontsystemFontOfSize:8];

MA20.text=@"MA20";

MA20.textColor= [UIColorcolorWithHexString:@"#2e87e2"withAlpha:1];

[MA20sizeToFit];

[selfaddSubview:MA20];

}

//画个成交量的框框

if(bottomBoxView==nil) {

bottomBoxView= [[UIViewalloc]initWithFrame:CGRectMake(0,140,315,75)];

bottomBoxView.layer.borderColor= [UIColorcolorWithHexString:@"#444444"withAlpha:0.5].CGColor;

bottomBoxView.layer.borderWidth=0.5;

bottomBoxView.userInteractionEnabled=YES;

[mainboxViewaddSubview:bottomBoxView];

}

if(!isUpdate) {

//成交量分割线

CGFloatpadRealValue =bottomBoxView.frame.size.height/3;

for(inti =0; i<3; i++) {

CGFloaty =bottomBoxView.frame.size.height-padRealValue * i;

lines*line = [[linesalloc]initWithFrame:CGRectMake(0,0,bottomBoxView.frame.size.width,bottomBoxView.frame.size.height)];

line.color=@"#999999";

line.alpha=0.5;

line.startPoint=CGPointMake(0, y);

line.endPoint=CGPointMake(bottomBoxView.frame.size.width, y);

[bottomBoxViewaddSubview:line];

}

}

//手1

shou1= [[UILabelalloc]initWithFrame:CGRectMake(1,141,50,8)];

shou1.text=@"7.5万";

shou1.font= [UIFontsystemFontOfSize:8];

[shou1setTextColor:[UIColorgrayColor]];

[selfaddSubview:shou1];

//手2

shou2= [[UILabelalloc]initWithFrame:CGRectMake(1,166,50,8)];

shou2.text=@"5万";

shou2.font= [UIFontsystemFontOfSize:8];

[shou2setTextColor:[UIColorgrayColor]];

[selfaddSubview:shou2];

//手3

shou3= [[UILabelalloc]initWithFrame:CGRectMake(1,191,50,8)];

shou3.text=@"2.5万";

shou3.font= [UIFontsystemFontOfSize:8];

[shou3setTextColor:[UIColorgrayColor]];

[selfaddSubview:shou3];

}

#pragma mark画k线

-(void)drawLine{

self.kCount=self.xWidth/ (self.kLineWidth+self.kLinePadding) +1;// K线中实体的总数

getdata= [[getDataalloc]init];

getdata.kCount=self.kCount;

[getdatachangeRKData:_riKArry];

self.data=getdata.data;

//开始画K线图

[selfdrawBoxWithKline];

//NSLog(@"处理得dddd");

//清除旧的k线

if(lineOldArray.count>0&&isUpdate) {

for(lines*lineinlineOldArray) {

[lineremoveFromSuperview];

}

}

lineOldArray=lineArray.copy;

if(_finishUpdateBlock&&isPinch) {

_finishUpdateBlock(self);

}

isUpdateFinish=YES;

//结束线程

[threadcancel];

}

#pragma mark在框框里画k线

-(void)drawBoxWithKline{

//平均线

CGFloatpadValue = (getdata.maxValue-getdata.minValue) /4;

CGFloatpadRealValue =mainboxView.frame.size.height/4;

for(inti =0; i<5; i++) {

CGFloaty =mainboxView.frame.size.height-padRealValue * i;

// lable

UILabel*leftTag = [[UILabelalloc]initWithFrame:CGRectMake(0, y-30/2-3,38,30)];

leftTag.text= [[NSStringalloc]initWithFormat:@"%.2f",padValue*i+getdata.minValue];

leftTag.textColor= [UIColorgrayColor];

leftTag.font=self.font;

//[leftTag sizeToFit];

[mainboxViewaddSubview:leftTag];

[lineArrayaddObject:leftTag];

}

//开始画连接均线

[selfdrawMAWithIndex:7andColor:@"#d83dd9"];//MA5,黄色

[selfdrawMAWithIndex:8andColor:@"#ebac31"];//MA10,蓝色

[selfdrawMAWithIndex:9andColor:@"#2e87e2"];//MA20,蓝色

//开始画连K线

// x轴从0到框框的宽度mainboxView.frame.size.width变化y轴为每个间隔的连线,如,今天的点连接明天的点

NSArray*ktempArray = [selfchangeKPointWithData:getdata.data];//换算成实际每天收盘价坐标数组

lines*kline = [[linesalloc]initWithFrame:CGRectMake(0,0,mainboxView.frame.size.width,mainboxView.frame.size.height)];

kline.points= ktempArray;

kline.lineWidth=self.kLineWidth;

kline.isK=YES;

[mainboxViewaddSubview:kline];

[lineArrayaddObject:kline];

//在成交量视图左右下方显示开始和结束日期

NSString*tempStr1 =[[getdata.dataobjectAtIndex:0]objectAtIndex:0];

startDateLab.text= [NSStringstringWithFormat:@"%@-%@-%@",[tempStr1substringWithRange:NSMakeRange(0,4)],[tempStr1substringWithRange:NSMakeRange(4,2)],[tempStr1substringWithRange:NSMakeRange(6,2)]];

NSString*tempStr2 = [[getdata.dataobjectAtIndex:getdata.data.count-1]objectAtIndex:0];

endDateLab.text= [NSStringstringWithFormat:@"%@-%@-%@",[tempStr2substringWithRange:NSMakeRange(0,4)],[tempStr2substringWithRange:NSMakeRange(4,2)],[tempStr2substringWithRange:NSMakeRange(6,2)]];

//开始画连成交量

NSArray*voltempArray = [selfchangeVolumePointWithData:getdata.data];//换算成实际成交量坐标数组

//NSLog(@"voltempArray:%@",voltempArray);

lines*volline = [[linesalloc]initWithFrame:CGRectMake(0,0,bottomBoxView.frame.size.width,bottomBoxView.frame.size.height)];

volline.points= voltempArray;

volline.lineWidth=self.kLineWidth;

volline.isK=YES;

volline.isVol=YES;

[bottomBoxViewaddSubview:volline];

shou1.text= [commondchangePrice:getdata.volMaxValue];

shou2.text= [commondchangePrice:getdata.volMaxValue/3*2];

shou3.text= [commondchangePrice:getdata.volMaxValue/3];

[lineArrayaddObject:volline];

}

#pragma mark画各种均线

-(void)drawMAWithIndex:(int)index andColor:(NSString*)color{

NSArray*tempArray = [selfchangePointWithData:getdata.dataandMA:index];//换算成实际坐标数组

lines*line = [[linesalloc]initWithFrame:CGRectMake(0,0,mainboxView.frame.size.width,mainboxView.frame.size.height)];

line.color= color;

line.points= tempArray;

//NSLog(@"date:%d",_date());

line.isK=NO;

[mainboxViewaddSubview:line];

[lineArrayaddObject:line];

}

#pragma mark把股市数据换算成实际的点坐标数组

-(NSArray*)changeKPointWithData:(NSArray*)data{

NSMutableArray*tempArray = [[NSMutableArrayalloc]init];

pointArray= [[NSMutableArrayalloc]init];

CGFloatPointStartX =self.kLineWidth/2;//起始点坐标

//NSLog(@"date:%@",data);

for(NSArray*itemindata) {

CGFloatheightvalue = [[itemobjectAtIndex:2]floatValue];//得到最高价

CGFloatlowvalue = [[itemobjectAtIndex:3]floatValue];//得到最低价

CGFloatopenvalue = [[itemobjectAtIndex:1]floatValue];//得到开盘价

CGFloatclosevalue = [[itemobjectAtIndex:4]floatValue];//得到收盘价

CGFloatyHeight =getdata.maxValue-getdata.minValue;// y的价格高度

CGFloatyViewHeight =mainboxView.frame.size.height;// y的实际像素高度

//换算成实际的坐标

CGFloatheightPointY = yViewHeight * (1- (heightvalue -getdata.minValue) / yHeight);

CGPointheightPoint =CGPointMake(PointStartX, heightPointY);//最高价换算为实际坐标值

CGFloatlowPointY = yViewHeight * (1- (lowvalue -getdata.minValue) / yHeight);;

CGPointlowPoint =CGPointMake(PointStartX, lowPointY);//最低价换算为实际坐标值

CGFloatopenPointY = yViewHeight * (1- (openvalue -getdata.minValue) / yHeight);;

CGPointopenPoint =CGPointMake(PointStartX, openPointY);//开盘价换算为实际坐标值

CGFloatclosePointY = yViewHeight * (1- (closevalue -getdata.minValue) / yHeight);;

CGPointclosePoint =CGPointMake(PointStartX, closePointY);//收盘价换算为实际坐标值

//实际坐标组装为数组

NSArray*currentArray = [[NSArrayalloc]initWithObjects:

NSStringFromCGPoint(heightPoint),

NSStringFromCGPoint(lowPoint),

NSStringFromCGPoint(openPoint),

NSStringFromCGPoint(closePoint),

[itemobjectAtIndex:0],//保存日期时间

[itemobjectAtIndex:4],//收盘价

[itemobjectAtIndex:7],// MA5

[itemobjectAtIndex:8],// MA10

[itemobjectAtIndex:9],// MA20

[itemobjectAtIndex:2],//最高价

[itemobjectAtIndex:3],//最低价

[itemobjectAtIndex:1],//开盘价

[itemobjectAtIndex:5],//成交量

[itemobjectAtIndex:6],//成交金额

nil];

[tempArrayaddObject:currentArray];//把坐标添加进新数组

currentArray =Nil;

PointStartX +=self.kLineWidth+self.kLinePadding;//生成下一个点的x轴

}

pointArray= tempArray;

returntempArray;

}

#pragma mark把股市数据换算成实际的点坐标数组

-(NSArray*)changePointWithData:(NSArray*)data  andMA:(int)MAIndex{

NSMutableArray*tempArray = [[NSMutableArrayalloc]init];

CGFloatPointStartX =0.0f;//起始点坐标

for(NSArray*itemindata) {

CGFloatcurrentValue = [[itemobjectAtIndex:MAIndex]floatValue];//得到均价价格

//换算成实际的坐标

CGFloatcurrentPointY =mainboxView.frame.size.height- ((currentValue -getdata.minValue) / (getdata.maxValue-getdata.minValue) *mainboxView.frame.size.height);

CGPointcurrentPoint =CGPointMake(PointStartX, currentPointY);//换算到当前的坐标值

[tempArrayaddObject:NSStringFromCGPoint(currentPoint)];//把坐标添加进新数组

PointStartX +=self.kLineWidth+self.kLinePadding;//生成下一个点的x轴

}

returntempArray;

}

#pragma mark把股市数据换算成成交量的实际坐标数组

-(NSArray*)changeVolumePointWithData:(NSArray*)data{

NSMutableArray*tempArray = [[NSMutableArrayalloc]init];

CGFloatPointStartX =self.kLineWidth/2;//起始点坐标

for(NSArray*itemindata) {

CGFloatvolumevalue = [[itemobjectAtIndex:5]floatValue];//得到每份成交量

CGFloatyHeight =getdata.volMaxValue-getdata.volMinValue;// y的价格高度

CGFloatyViewHeight =bottomBoxView.frame.size.height;// y的实际像素高度

//换算成实际的坐标

CGFloatvolumePointY = yViewHeight * (1- (volumevalue -getdata.volMinValue) / yHeight);

CGPointvolumePoint =CGPointMake(PointStartX, volumePointY);//成交量换算为实际坐标值

CGPointvolumePointStart =CGPointMake(PointStartX, yViewHeight);

//把开盘价收盘价放进去好计算实体的颜色

CGFloatopenvalue = [[itemobjectAtIndex:1]floatValue];//得到开盘价

CGFloatclosevalue = [[itemobjectAtIndex:4]floatValue];//得到收盘价

CGPointopenPoint =CGPointMake(PointStartX, closevalue);//开盘价换算为实际坐标值

CGPointclosePoint =CGPointMake(PointStartX, openvalue);//收盘价换算为实际坐标值

//实际坐标组装为数组

NSArray*currentArray = [[NSArrayalloc]initWithObjects:

NSStringFromCGPoint(volumePointStart),

NSStringFromCGPoint(volumePoint),

NSStringFromCGPoint(openPoint),

NSStringFromCGPoint(closePoint),

nil];

[tempArrayaddObject:currentArray];//把坐标添加进新数组

currentArray =Nil;

PointStartX +=self.kLineWidth+self.kLinePadding;//生成下一个点的x轴

}

// NSLog(@"处理完成");

returntempArray;

}

#pragma mark手指捏合动作

-(void)touchBoxAction:(UIPinchGestureRecognizer*)pGesture{

isPinch=NO;

//NSLog(@"状态:%li==%f",(long)pinchGesture.state,pGesture.scale);

if(pGesture.state==2&&isUpdateFinish) {

if(pGesture.scale>1) {

//放大手势

self.kLineWidth++;

[selfupdateSelf];

}else{

//缩小手势

self.kLineWidth--;

[selfupdateSelf];

}

}

if(pGesture.state==3) {

isUpdateFinish=YES;

}

}

//缩小手势,放大手势

-(void)updateSelf{

if(isUpdateFinish) {

if(self.kLineWidth>20)

self.kLineWidth=20;

if(self.kLineWidth<1)

self.kLineWidth=1;

isUpdateFinish=NO;

isUpdate=YES;

self.data=nil;

pointArray=nil;

if(!thread.isCancelled) {

[threadcancel];

}

self.clearsContextBeforeDrawing=YES;

[threadcancel];

thread= [[NSThreadalloc]initWithTarget:selfselector:@selector(drawLine)object:nil];

[threadstart];

}

}

#pragma mark长按就开始生成十字线

-(void)gestureRecognizerHandle:(UILongPressGestureRecognizer*)longResture{

isPinch=YES;

//NSLog(@"gestureRecognizerHandle%li",(long)longResture.state);

touchViewPoint= [longResturelocationInView:mainboxView];

//手指长按开始时更新一般

if(longResture.state==UIGestureRecognizerStateBegan){

[selfupdate];

}

//手指移动时候开始显示十字线

if(longResture.state==UIGestureRecognizerStateChanged) {

[selfisKPointWithPoint:touchViewPoint];

}

//手指离开的时候移除十字线

if(longResture.state==UIGestureRecognizerStateEnded) {

[movelineoneremoveFromSuperview];

[movelinetworemoveFromSuperview];

[xxViewremoveFromSuperview];

[movelinetwoLableremoveFromSuperview];

movelineone=nil;

movelinetwo=nil;

xxView=nil;

movelinetwoLable=nil;

isPinch=NO;

}

}

#pragma mark判断并在十字线上显示提示信息

-(void)isKPointWithPoint:(CGPoint)point{

CGFloatitemPointX =0;

for(NSArray*iteminpointArray) {

//NSLog(@"item:%@",item);

CGPointitemPoint =CGPointFromString([itemobjectAtIndex:0]);//收盘价的坐标

itemPointX = itemPoint.x;

intitemX = (int)itemPointX;

intpointX = (int)point.x;

if(itemX==pointX || point.x-itemX<=self.kLineWidth/2) {

movelineone.frame=CGRectMake(itemPointX,movelineone.frame.origin.y,movelineone.frame.size.width,movelineone.frame.size.height);

movelinetwo.frame=CGRectMake(movelinetwo.frame.origin.x,itemPoint.y,movelinetwo.frame.size.width,movelinetwo.frame.size.height);

//垂直提示成交价格

NSString*tempStr =[itemobjectAtIndex:4];

riqLable.text= [NSStringstringWithFormat:@"%@-%@-%@",[tempStrsubstringWithRange:NSMakeRange(0,4)],[tempStrsubstringWithRange:NSMakeRange(4,2)],[tempStrsubstringWithRange:NSMakeRange(6,2)]];//日期

kpLable.text=[NSStringstringWithFormat:@"开盘:%@",[itemobjectAtIndex:11]];

zgLable.text=[NSStringstringWithFormat:@"最高:%@",[itemobjectAtIndex:9]];

zdLable.text=[NSStringstringWithFormat:@"最低:%@",[itemobjectAtIndex:10]];

spLable.text=[NSStringstringWithFormat:@"收盘:%@",[itemobjectAtIndex:5]];

cjlLable.text=[NSStringstringWithFormat:@"成交量:%@",[commondchangePrice:[[itemobjectAtIndex:12]floatValue]]];

cjeLable.text=[NSStringstringWithFormat:@"成交额:%@",[commondchangePrice:[[itemobjectAtIndex:13]floatValue]]];

zflLable.text=[NSStringstringWithFormat:@"涨幅:%.2f%%",([[itemobjectAtIndex:5]floatValue]-[[itemobjectAtIndex:11]floatValue])/[[itemobjectAtIndex:11]floatValue]*100];

CGFloatoneLableY =bottomBoxView.frame.origin.y;

CGFloatoneLableX =0;

if(itemPointX

oneLableX =xxView.frame.size.width/2- itemPointX;

}

if((mainboxView.frame.size.width- itemPointX)

oneLableX = -(xxView.frame.size.width/2- (mainboxView.frame.size.width- itemPointX));

}

xxView.frame=CGRectMake(itemPointX -xxView.frame.size.width/2+ oneLableX, oneLableY,

xxView.frame.size.width,xxView.frame.size.height);

//NSLog(@"item:%@",item);

//横向提示成交数量

movelinetwoLable.text= [itemobjectAtIndex:5];//

CGFloattwoLableX =movelinetwoLable.frame.origin.x;

//如果滑动到了左半边则提示向右跳转

if((mainboxView.frame.size.width- itemPointX) >mainboxView.frame.size.width/2) {

twoLableX =mainboxView.frame.size.width-movelinetwoLable.frame.size.width;

}else{

twoLableX =0;

}

movelinetwoLable.frame=CGRectMake(twoLableX,itemPoint.y-movelinetwoLable.frame.size.height/2,

movelinetwoLable.frame.size.width,movelinetwoLable.frame.size.height);

break;

}

}

}

-(void)update{

if(self.kLineWidth>20)

self.kLineWidth=20;

if(self.kLineWidth<1)

self.kLineWidth=1;

isUpdate=YES;

if(!thread.isCancelled) {

[threadcancel];

}

self.clearsContextBeforeDrawing=YES;

[threadcancel];

thread= [[NSThreadalloc]initWithTarget:selfselector:@selector(drawLine)object:nil];

[threadstart];

}

-(void)dealloc{

thread=nil;

}

@end

#import

@interfacegetData :NSObject

@property(nonatomic,retain)NSMutableArray*data;

@property(nonatomic,retain)NSArray*dayDatas;

@property(nonatomic,retain)NSMutableArray*category;

@property(nonatomic,retain)NSString*lastTime;

@property(nonatomic,retain)UILabel*status;

@property(nonatomic,assign)BOOLisFinish;

@property(nonatomic,assign)CGFloatmaxValue;

@property(nonatomic,assign)CGFloatminValue;

@property(nonatomic,assign)CGFloatvolMaxValue;

@property(nonatomic,assign)CGFloatvolMinValue;

@property(nonatomic,assign)NSIntegerkCount;

@property(nonatomic,retain)NSString*req_type;

-(void)changeFSData:(NSArray*)lines;

-(void)changeRKData:(NSArray*)lines;

@end

#import"getData.h"

#import"commond.h"

@implementationgetData

-(id)init{

self= [superinit];

if(self){

self.isFinish=NO;

self.maxValue=0;

self.minValue=CGFLOAT_MAX;

self.volMaxValue=0;

self.volMinValue=CGFLOAT_MAX;

}

returnself;

}

//解析具体数据并赋值组合

-(void)changeFSData:(NSArray*)lines{

NSMutableArray*data =[[NSMutableArrayalloc]init];

//NSMutableArray *category =[[NSMutableArray alloc] init];

NSArray*newArray = lines;

newArray = [newArrayobjectsAtIndexes:[[NSIndexSetalloc]initWithIndexesInRange:NSMakeRange(0,self.kCount>=newArray.count?newArray.count:self.kCount)]];//只要前面指定的数据

NSIntegeridx;

for(idx =0; idx < newArray.count; idx++) {

if(idx >0)

{

NSString*bfLine = [newArrayobjectAtIndex:idx-1];//前面一个数组

NSString*line = [newArrayobjectAtIndex:idx];

if([lineisEqualToString:@""]){

continue;

}

NSArray*bfArr = [bfLinecomponentsSeparatedByCharactersInSet:[NSCharacterSetcharacterSetWithCharactersInString:@","]];

NSArray*arr = [linecomponentsSeparatedByCharactersInSet:[NSCharacterSetcharacterSetWithCharactersInString:@","]];

//收盘价的最小值和最大值

if([[arrobjectAtIndex:1]floatValue]>self.maxValue) {

self.maxValue= [[arrobjectAtIndex:1]floatValue];

}

if([[arrobjectAtIndex:1]floatValue]

self.minValue= [[arrobjectAtIndex:1]floatValue];

}

floatcrtVol = [[arrobjectAtIndex:2]floatValue] - [[bfArrobjectAtIndex:2]floatValue];//当前累加的数据减去上一个得到成交量

//NSLog(@"crtVol:%.2f",crtVol);

//成交量的最大值最小值

if(crtVol>self.volMaxValue) {

self.volMaxValue= crtVol;

}

if(crtVol

self.volMinValue= crtVol;

}

NSMutableArray*item =[[NSMutableArrayalloc]init];

[itemaddObject:[arrobjectAtIndex:0]];//成交均价

[itemaddObject:[arrobjectAtIndex:1]];//成交价格

[itemaddObject:[NSStringstringWithFormat:@"%.0f",crtVol]];//成交数量

[itemaddObject:[arrobjectAtIndex:3]];//成交金额

[dataaddObject:item];//组合总data

}

else{

NSString*line = [newArrayobjectAtIndex:idx];

if([lineisEqualToString:@""]){

continue;

}

NSArray*arr = [linecomponentsSeparatedByCharactersInSet:[NSCharacterSetcharacterSetWithCharactersInString:@","]];

//收盘价的最小值和最大值

if([[arrobjectAtIndex:1]floatValue]>self.maxValue) {

self.maxValue= [[arrobjectAtIndex:1]floatValue];

}

if([[arrobjectAtIndex:1]floatValue]

self.minValue= [[arrobjectAtIndex:1]floatValue];

}

//成交量的最大值最小值

if([[arrobjectAtIndex:2]floatValue]>self.volMaxValue) {

self.volMaxValue= [[arrobjectAtIndex:2]floatValue];

//NSLog(@"volMaxValue:%f",self.volMaxValue);

}

if([[arrobjectAtIndex:2]floatValue]

self.volMinValue= [[arrobjectAtIndex:2]floatValue];

//NSLog(@"volMinValue:%f",self.volMinValue);

}

NSMutableArray*item =[[NSMutableArrayalloc]init];

[itemaddObject:[arrobjectAtIndex:0]];//成交均价

[itemaddObject:[arrobjectAtIndex:1]];//成交价格

[itemaddObject:[arrobjectAtIndex:2]];//成交数量

[itemaddObject:[arrobjectAtIndex:3]];//成交金额

[dataaddObject:item];//组合总data

//NSLog(@"self.volMaxValue:%.2f",self.volMaxValue);

}

}

//NSLog(@"data:%@",data);

if(data.count==0){

self.status.text=@"Error!";

return;

}

self.data= data;

}

-(void)changeRKData:(NSArray*)lines{

NSMutableArray*data =[[NSMutableArrayalloc]init];

NSArray*newArray = lines;

newArray = [newArrayobjectsAtIndexes:[[NSIndexSetalloc]initWithIndexesInRange:NSMakeRange(0,self.kCount>=newArray.count?newArray.count:self.kCount)]];//只要前面指定的数据

NSIntegeridx;

intMA5=5,MA10=10,MA20=20;//均线统计

for(idx = newArray.count-1; idx >=0; idx--) {

NSString*line = [newArrayobjectAtIndex:idx];

if([lineisEqualToString:@""]){

continue;

}

NSArray*arr = [linecomponentsSeparatedByCharactersInSet:[NSCharacterSetcharacterSetWithCharactersInString:@","]];

//收盘价的最小值和最大值

if([[arrobjectAtIndex:4]floatValue]>self.maxValue) {

self.maxValue= [[arrobjectAtIndex:4]floatValue];

}

if([[arrobjectAtIndex:4]floatValue]

self.minValue= [[arrobjectAtIndex:4]floatValue];

}

//成交量的最大值最小值

if([[arrobjectAtIndex:5]floatValue]>self.volMaxValue) {

self.volMaxValue= [[arrobjectAtIndex:5]floatValue];

//NSLog(@"volMaxValue:%f",self.volMaxValue);

}

if([[arrobjectAtIndex:5]floatValue]

self.volMinValue= [[arrobjectAtIndex:5]floatValue];

//NSLog(@"volMinValue:%f",self.volMinValue);

}

NSMutableArray*item =[[NSMutableArrayalloc]init];

[itemaddObject:[arrobjectAtIndex:0]];//日期

[itemaddObject:[arrobjectAtIndex:1]];//今日开盘

[itemaddObject:[arrobjectAtIndex:2]];//最高成交

[itemaddObject:[arrobjectAtIndex:3]];//最低成交

[itemaddObject:[arrobjectAtIndex:4]];//最近成交

[itemaddObject:[arrobjectAtIndex:5]];//成交数量

[itemaddObject:[arrobjectAtIndex:6]];//成交金额

CGFloatidxLocation = [linesindexOfObject:line];

// MA5

[itemaddObject:[NSNumbernumberWithFloat:[selfsumArrayWithData:linesandRange:NSMakeRange(idxLocation, MA5)]]];//前五日收盘价平均值

// MA10

[itemaddObject:[NSNumbernumberWithFloat:[selfsumArrayWithData:linesandRange:NSMakeRange(idxLocation, MA10)]]];//前十日收盘价平均值

// MA20

[itemaddObject:[NSNumbernumberWithFloat:[selfsumArrayWithData:linesandRange:NSMakeRange(idxLocation, MA20)]]];//前二十日收盘价平均值

[dataaddObject:item];//组合总data

}

//NSLog(@"data:%@",data);

if(data.count==0){

self.status.text=@"Error!";

return;

}

self.data= data;

}

-(CGFloat)sumArrayWithData:(NSArray*)data andRange:(NSRange)range{

CGFloatvalue =0;

if(data.count- range.location>range.length) {

NSArray*newArray = [dataobjectsAtIndexes:[[NSIndexSetalloc]initWithIndexesInRange:range]];

for(NSString*iteminnewArray) {

NSArray*arr = [itemcomponentsSeparatedByCharactersInSet:[NSCharacterSetcharacterSetWithCharactersInString:@","]];

value += [[arrobjectAtIndex:4]floatValue];//最近成交

}

if(value>0) {

value = value/ newArray.count;

}

}

returnvalue;

}

@end

相关文章

  • iOS股票K线图

    mark:iOS股票K线图 iOS 股票K线图绘制 iOS 股票K线图绘制 从零开始实现k线图走势图绘制(iOS实战篇)

  • Android开发之基于MPAndroidChart实现股票K线

    接上文Android开发之基于MPAndroidChart实现股票K线图(二),实现了最基本的K线图效果后,接下来...

  • K线图实现

    写了lines、RKLineView、getData三个对象完成实现。 首先开始调用 rkLine= [[RKLi...

  • 你这跌一看的K线图基础知识口诀

    K线图基础知识,K线图运用技巧,经典K线图运用口诀,下面为大家介绍经典K线图运用口决系列。 1、低档五阳 【K线形...

  • 散户必看:如何捕捉放量涨停板

    如何看懂k线图,作为刚入市的股票新手,第一步要学习的就是k线图了,如何看懂k线图,怎么快速看懂k线图尤为重要,我们...

  • 初探K线图

    什么是K线图呢?K线图要怎么看呢?你能透过K线图看到什么东西呢?今天看相关资料,复盘如下,一起来总结下吧。 K线图...

  • K线经典形态图解 技术学到家 操作活如水中鱼

    类别 - K线图经典图解 K线图经典图解专门针对K线展开全面的分析,涵盖了K线图中经常出现的经典图解。 主要包含了...

  • k线经典形态解析买卖点 主攻震荡行情

    类别 - K线图经典图解 K线图经典图解专门针对K线展开全面的分析,涵盖了K线图中经常出现的经典图解。接下来叶昕继...

  • 一步步教你画股票图,K线图,高仿雪球股票

    股票图,K线图,蜡烛图,高仿雪球股票,教你一步步实现股票图 项目地址 讲K线图之前,先来一个引言. 前两天听了朱凯...

  • kLine行情图

    使用CoreGraphics框架实现了k线行情图的基本功能,只有日、周、月k线图,手势包括拖动、缩放、长按。 提示...

网友评论

    本文标题:K线图实现

    本文链接:https://www.haomeiwen.com/subject/sbfllttx.html