1. 程式人生 > >IOS 股票K線圖的實現

IOS 股票K線圖的實現

 2015-09-04 by 木易哥哥 

智者精選,每天獲取勵志認知能量 www.5izhjx.com

寫了lines、RKLineView、getData三個物件完成實現。

首先開始呼叫

rkLine = [[RKLineViewalloc]init];

        CGRect frame =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 <UIKit/UIKit.h>

@interface lines : UIView

@property (nonatomic,assign)CGPoint startPoint;//線條起點

@property (

nonatomic,assign)CGPoint endPoint;//線條終點

@property (nonatomic,retain)NSArray *points;//多點連線陣列

@property (nonatomic,retain)NSString *color;//線條顏色

@property (nonatomic,assign)CGFloat lineWidth;//線條寬度

@property (nonatomic,assign)BOOL isK;//是否是實體K預設是連線線

@property (nonatomic,assign)BOOL isVol;//是否是畫成交量的實體

@end

#import "lines.h"

#import "colorModel.h"

#import "UIColor+helper.h"

@interfacelines(){

}

@end

@implementation lines

- (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

{

CGContextRef context =UIGraphicsGetCurrentContext();//獲取繪圖上下文

   if (self.isK) {

       //k

       for (NSArray *iteminself.points) {

           //轉換座標

           CGPoint heightPoint,lowPoint,openPoint,closePoint;

            heightPoint =CGPointFromString([itemobjectAtIndex:0]);

            lowPoint =CGPointFromString([itemobjectAtIndex:1]);

            openPoint =CGPointFromString([itemobjectAtIndex:2]);

            closePoint =CGPointFromString([itemobjectAtIndex:3]);

            [selfdrawKWithContext:contextheight:heightPoint Low:lowPoint open:openPoint close:closePoint width: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 (id iteminself.points) {

           CGPoint currentPoint =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]<self.points.count) {

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

                }

            }

        }

    }else{

// 定義兩個點 畫兩點連線

       constCGPoint points[] = {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);

//首先判斷是綠的還是紅的,根據開盤價和收盤價的座標來計算

   BOOL isKong =NO;

   colorModel *colormodel = [UIColorRGBWithHexString:@"#FF0000"withAlpha:self.alpha];//設定預設紅色

//如果開盤價座標在收盤價座標上方則為綠色即空

   if (openPoint.y<closePoint.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); // 上下陰影線的寬度

        }

       constCGPoint points[] = {heightPoint,lowPoint};

       CGContextStrokeLineSegments(context, points,2);  // 繪製線段(預設不繪製端點)

    }

// 再畫中間的實體

   CGContextSetLineWidth(context, width);// 改變線的寬度

   CGFloat halfWidth =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);

    }

// 開始畫實體

   constCGPoint point[] = {openPoint,closePoint};

CGContextStrokeLineSegments(context, point,2); // 繪製線段(預設不繪製端點)

}

@end

#import <UIKit/UIKit.h>

typedefvoid(^updateBlock)(id);

@interface RKLineView :UIView

@property (nonatomic,copy)updateBlock finishUpdateBlock; // 定義一個block回撥更新介面

@property (nonatomic,assign)CGFloat xWidth;// x軸寬度

@property (nonatomic,assign)CGFloat yHeight;// y軸高度

@property (nonatomic,assign)CGFloat bottomBoxHeight;// y軸高度

@property (nonatomic,assign)CGFloat kLineWidth;// k線的寬度 用來計算可存放K線實體的個數,也可以由此計算出起始日期和結束日期的時間段

@property (nonatomic,assign)CGFloat kLinePadding;

@property (nonatomic,assign)int kCount;// 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;// 顯示成交量最大值

   BOOL isUpdate;

   BOOL isUpdateFinish;

   NSMutableArray *lineArray ;// k線陣列

   NSMutableArray *lineOldArray ;// k線陣列

UIPinchGestureRecognizer *pinchGesture;

   CGPoint touchViewPoint;

   BOOL isPinch;

   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

@implementation RKLineView

-(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) {

       CGRect oneFrame =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,

相關推薦

IOS 股票K實現

 2015-09-04 by 木易哥哥 智者精選,每天獲取勵志認知能量 www.5izhjx.com寫了lines、RKLineView、getData三個物件完成實現。首先開始呼叫rkLine = [[RKLineViewalloc]init];        CGRect

iOS股票K、校園助手、適配iPhone X、版本檢測等源碼

0.12 適配 環境 cool row 客戶端 1-1 獲取 launch iOS精選源碼 快速創建menuItem控件 YHPhotoBrowser 優化的網絡圖片瀏覽 cocoaAsynSocket demo (包含客戶端和服務端)

Android版股票K實現方案

前言 介紹   K線圖一般分為日K、周K、月K,顯示的內容有開/收盤價、最高/低價、成交量,額外資訊為均線(ma5/10/20)。例如,日K圖中就為當日開/收盤價、最高/低價、成交量和5/10/20日均線。K線圖支援滑動,滑動過程中,動態改變最高最低

iOS 繪製股票K

//聯絡人:石虎  QQ: 1224614774暱稱:嗡嘛呢叭咪哄 一、k線圖概念      k線圖:有最新價、收盤價、開盤價、最高價、最低價、均價、賣量、買量等等。而我們需要展示給使用者

Canvas繪制股票K

mat restore screen 位置 mar toc rect() store water 最近想在小程序做股票K線圖,於是嘗試用Canvas實現股票K線圖。K線圖用到Canvas的API其實只有劃線和畫矩形,即moveTo(),lineTo(),fillRect()

WPF中使用amCharts繪制股票K

tip \n margin 文件對話框 sda 類結構 lan reset 繪圖 原文:WPF中使用amCharts繪制股票K線圖 本想自己用GDI繪圖, 通過數據直接繪制一張蠟柱圖, 但覺得這樣子的功能比較少, 所以到網上搜索一些能畫出K線圖的控件. 發現D

使用Flask & Plotly 做股票k互動式視覺化處理(二)

接上一篇,這篇要實現搜尋功能: 建立搜尋頁面: 在templates資料夾內新建main.html檔案,內容如下: <!doctype html> <html lang="en"> <head> <

WPF中使用amCharts繪製股票K

        本想自己用GDI繪圖, 通過資料直接繪製一張蠟柱圖, 但覺得這樣子的功能比較少, 所以到網上搜索一些能畫出K線圖的控制元件. 發現DynamicDataDisplay挺好的, 在它的開發截圖裡也發現可以繪製蠟柱圖, 但文件好像做的不是很好, 不知道怎麼用它來畫, 在尋找它比較好的例子的

Numpy簡單繪製K實現上漲下跌效果圖(Python)

# 將原始資料中日-月-年格式的日期字串轉換為numpy可以處理的年月日格式 def dmy2ymd(dmy): dmy = str(dmy, encoding='utf-8') date = dt.datetime.strptime(dmy, '%d-%m-

利用 python numpy +matplotlib 繪製股票k

# -- coding: utf-8 -- import requests import numpy as np from matplotlib import pyplot as plt from matplotlib import animation fig = plt.figure(figs

利用JFreeChart繪製股票K

因為工作的需要,接觸了一些股票圖形繪製類的工作,其中最主要的還是股票K線圖的繪製了,如果利用程式語言最底層的圖形繪製方法去繪製這類圖形,如果對程式語言不是特別熟悉的話,一般是有很大的困難的,通過在網上搜索發現,以自己最熟悉的兩門語言為例,其中有PHP版的開源JPGrap

機器學習(4):python基礎及fft、svd、股票k、分形等實踐

本節我們主要簡單介紹機器學習常用的語言–python。樓主本身是寫java的,在這之前對python並不瞭解,接觸之後發現python比java簡直要好用幾千倍。這裡主要通過常用的統計量、fft、股票k線圖及分形等樣例,介紹python的使用及各種包的載入。

股票的幾種“k”的寓意

k線圖 必須 進行 nbsp 底部 附近 中間 股票 條件 1.早晨之星   前提條件:        (1)整個股票的價格呈現長期的下跌趨勢,切已經到達底部附近(探底:跌幅達到50%以上,必須大於50%,越大越好)。        (2)k線圖中,左邊是一個大陰線,中

基於ECharts圖表實現股票K圖表

  //資料模型 time0 open1 close2 min3 max4 vol5 tag6 macd7 dif8 dea9 //['2015-10-19',18.56,18.25,18.19,18.56,55.00,0,-0.00,0.08,0.09] var data =

一步一步教你寫股票走勢——K二(圖表聯動)

K線圖越做發現坑越多,跟之前做的分時圖完全不是一個重量級的啊,分時圖不需要滾動,少走了很多彎路,K線圖因為滾動的問題,會導致很多其他問題,比如: 多個圖表之間滾動時怎麼聯動 高亮滾動衝突 放縮聯動 … 表對齊 下面我們就開始介紹本

IOSK系列之K-繪製蠟燭

k線系列目錄 檢視目錄請點選這兒 補充知識 K線雖然看上去複雜,但仔細觀察就會知道幾乎所有的k線無非由以下幾種組成: (1)蠟燭 (2)OHLC (3)線段 (4)帶狀 (5)圓形 除此之外還有其他的,都是比

一步一步教你寫股票走勢——K三(新增均

在開篇之前,給大家出個小演算法題,一定要做哦,因為既然你打算看本章節內容了,那麼這個小演算法必須得會的喲! 有一組數,1、2、3……99、100,一共一百個數,假設是ListA,現在將 ListA索引為0、1、2、3、4的數相加

一步一步教你寫股票走勢——K四(高亮聯動一)

k線圖的第二節末尾,已經初步講解了高亮聯動,但是效果沒有達到我們的預期,因為k線圖本身就支援左右滾動,所以在滾動高亮的時候就出現了衝突,我們那會只實現了點選圖表,實現高亮的展示,但是效果還是太差,所以才有了我們今天這節的內容,將會優化體驗效果

Swift3實現的繪製股票K庫, FastImageCache提升圖片的載入和渲染速度,Chameleon顏色框架

程式碼1:用Swift3實現的繪製股票K線庫 for iOS & macOS 程式碼地址:網頁連結 程式碼2:FastImageCache是Path團隊開發的一個開源庫,用於提升圖片的載入和渲染速度,讓基於圖片的列表滑動起來更順暢。 程式碼地址:網頁連結 程式碼3:Chameleon是一款輕量級但功能

使用自定義View來實現K、分時

廢話不多說 先上效果圖 實現原理 1.分時圖 分時圖一般有兩條線,一條均價線,一條時價線。其實每條線都是由很多點組合而來,通過canvas.drawLine方法將每兩個點進行連線,最終繪製出分時圖。 2.K線圖 K線圖是由一個一個的矩形組合而來,每個矩形代表