android 股票K线图

来源:互联网 发布:达内大数据 编辑:程序博客网 时间:2024/04/27 17:43

现在在手上的是一个证券资讯类型的app,其中有涉及到股票行情界面,行情中有K线图等,看到网上很多人在求这方面的资料,所以我特地写了一个demo在此处给大家分享一下。

下面是做出来的效果图:




这个 界面 是如何画出来的我就不做介绍了,大家可以去下载项目源码。

背景图是利用canvas先画出一个矩形,然后再画几根虚线,均线图是通过path来绘制的,总之图的绘制是很简单的,我就不在这里作介绍了,大家可以去github下载源码看看。涉及到均线、最高价、最低价、收盘价、开盘价的概念大家可以百度一下。

我再这里要介绍的是计算问题:

大家可以看到分时图、日K、月K的左边的成交价格都是不一样的,而我们的k线都是通过这个价格来绘制的,也就是说价格是时刻变动,那么我们的k线绘制也是变动的。假设我们要计算分时图中价格为25.69的那一分钟应该如何画,画在屏幕中的哪一个位置,那么这个应该怎么画呢,价格是变动的,画的位置也是变动的,但是有一点我们屏幕的大小是不变的。所以我们可以通过背景图的高度来计算某个价格的线图应该从哪个地方开始画。我们可以计算出一个像素点对应多少个价格,分析图如下:

价格和像素形成个一个比例计算是:double   heightScale = (endY - startY)/(highPrice - lowPrice);

所以价格25.69应该是画在mStartY = (float) (startY+ (highPrice - 25.69) * heightScale);

这个明白了之后其他的原理都是一样的,我就不介绍了,下面是部分代码:

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. @Override  
  2.     protected void drawKChatBackGround() {  
  3.         Rect dirty = new Rect(left, kChartTop, right, KChartbottom);  
  4.         // 画背景图的矩形  
  5.         mCanvas.drawRect(dirty, LineGrayPaint);  
  6.         PathEffect effects = new DashPathEffect(new float[] { 5555 }, 1);  
  7.         LineGrayPaint.setPathEffect(effects);  
  8.         Path path = new Path();  
  9.         int y = kChartTop + 15;  
  10.         // 画上面的虚线  
  11.         path.moveTo(left, y );  
  12.         path.lineTo(right, y );  
  13.         String text = getPriceText(highPrice);  
  14.         int textHeight = (int) (textGrayPaint.descent() - textGrayPaint.ascent());  
  15.         mCanvas.drawText(text,left - textGrayPaint.measureText(text) - 5,y + textHeight/2 ,textGrayPaint);  
  16.         double max = highPrice - lowPrice;  
  17.         if (max > 10){  
  18.             // 分成四等分  
  19.             // 画中间的三根虚线  
  20.             int n = 4;  
  21.             double sper = (highPrice - lowPrice) / 4;// 每一等分代表的价格  
  22.             for(int i=1;i<n;i++){  
  23.                 y  =  i*((KChartbottom - kChartTop)/n) + kChartTop;  
  24.                 path.moveTo(left, y);  
  25.                 path.lineTo(right,y);  
  26.                 text = getPriceText(highPrice - i*sper);  
  27.                 mCanvas.drawText(text,left - textGrayPaint.measureText(text) - 5,y + textHeight/2,textGrayPaint);  
  28.             }  
  29.         }else{  
  30.             // 分成两等分  
  31.             // 画中间的虚线  
  32.             y = (KChartbottom - kChartTop)/2 + kChartTop;  
  33.             path.moveTo(left, y);  
  34.             path.lineTo(right, y);  
  35.             text = getPriceText(highPrice - (highPrice - lowPrice) / 2);  
  36.             mCanvas.drawText(text,left - textGrayPaint.measureText(text) - 5,y + textHeight/2,textGrayPaint);  
  37.         }  
  38.         // 画下面的虚线  
  39.         y = KChartbottom - 15;  
  40.         path.moveTo(left, y);  
  41.         path.lineTo(right, y);  
  42.         text = getPriceText(lowPrice);  
  43.         mCanvas.drawText(text,left - textGrayPaint.measureText(text) - 5,y + textHeight/2,textGrayPaint);  
  44. //      // 画等分的虚线和下面的日期  
  45.         for (int i = num - 1; i > 0; i--) {  
  46.             int x = left + perWidth * i;  
  47.             path.moveTo(x, kChartTop);  
  48.             path.lineTo(x, KChartbottom);  
  49.             perXPoint[i - 1] = x;  
  50.         }  
  51.         mCanvas.drawPath(path, LineGrayPaint);  
  52.     }  

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. @Override  
  2.     protected void drawMAChart() {  
  3.         // 画均线  
  4.         Path path5 = new Path();  
  5.         Path path10 = new Path();  
  6.         Path path20 = new Path();  
  7.         double heightScale = (KChartbottom - kChartTop)/(highPrice - lowPrice);  
  8.         int maStart = left;  
  9.         float maStartY;  
  10.         path5.moveTo(maStart, (float) (kChartTop + (highPrice - infos.get(0).getMaValue5()) * heightScale));  
  11.         path10.moveTo(maStart, (float) (kChartTop + (highPrice - infos.get(0).getMaValue10()) * heightScale));  
  12.         path20.moveTo(maStart, (float) (kChartTop + (highPrice - infos.get(0).getMaValue20()) * heightScale));  
  13.           
  14.         for(SingleStockInfo info:infos){  
  15.             maStart += per * perHalf;// 每一天实际所占的数据是4/6,左右边距各1/6   
  16.             maStartY = (float) (kChartTop + (highPrice - info.getMaValue5()) * heightScale);  
  17.             path5.lineTo(maStart, maStartY);  
  18.             maStartY = (float) (kChartTop + (highPrice - info.getMaValue10()) * heightScale);  
  19.             path10.lineTo(maStart, maStartY);  
  20.             maStartY = (float) (kChartTop + (highPrice - info.getMaValue20()) * heightScale);  
  21.             path20.lineTo(maStart, maStartY);  
  22.             maStart += per * perHalf;  
  23.         }  
  24.           
  25.         Paint paint = new Paint();  
  26.         paint.setColor(Color.BLUE);  
  27.         paint.setAntiAlias(true);  
  28.         paint.setStrokeWidth(2);  
  29.         paint.setStyle(Style.STROKE);  
  30.         mCanvas.drawPath(path5, paint);  
  31.         paint.setColor(Color.MAGENTA);  
  32.         mCanvas.drawPath(path10, paint);  
  33.         paint.setColor(Color.GREEN);  
  34.         mCanvas.drawPath(path20, paint);  
  35.     }  

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. /** 
  2.      * 下面的柱形图 
  3.      */  
  4.     @Override  
  5.     protected void drawPillarsChart(int flag) {  
  6.         LineGrayPaint.setPathEffect(null);  
  7.         Rect dirty = new Rect(left, pillarsChartTop, right, pillarsChartbottom);  
  8.         // 画背景图的矩形  
  9.         mCanvas.drawRect(dirty, LineGrayPaint);  
  10.           
  11.         int y = pillarsChartTop + (pillarsChartbottom - pillarsChartTop)/2;  
  12.         mCanvas.drawLine(left,y,right, y, LineGrayPaint);  
  13.           
  14.         // 中间的值  
  15.         String totalCount = getPriceText(maxCount/2/10000);  
  16.         float maginLeft = left - textGrayPaint.measureText(totalCount)- 5;  
  17.         mCanvas.drawText(totalCount, maginLeft, y,textGrayPaint);  
  18.         // 上面的值  
  19.         totalCount = getPriceText(maxCount/10000);  
  20.         maginLeft = left - textGrayPaint.measureText(totalCount)- 5;  
  21.         mCanvas.drawText(totalCount, maginLeft, pillarsChartTop,textGrayPaint);  
  22.         // 下面的值  
  23.         totalCount = "万手";  
  24.         maginLeft = left - textGrayPaint.measureText(totalCount) - 5;  
  25.         mCanvas.drawText(totalCount, maginLeft, pillarsChartbottom,textGrayPaint);  
  26.         int pStart = left;  
  27.         float pStartY;  
  28.         double heightScale = (pillarsChartbottom - pillarsChartTop)/maxCount;  
  29.         Paint paint = new Paint();  
  30.         paint.setAntiAlias(true);  
  31.         paint.setStyle(Paint.Style.FILL);  
  32.         if (flag == StockService.FLAG){  
  33.             for(MinuteInfo info:minuteInfos){  
  34.                 pStart += per * per16;// 每一天实际所占的数据是4/6,加上1/6  
  35.                 pStartY = (float) (pillarsChartTop + (maxCount - info.getVolume()) * heightScale);  
  36.                 dirty = new Rect(pStart, (int) pStartY, (int) (pStart + per * per46), pillarsChartbottom-2);  
  37.                 paint.setColor(info.getColor());  
  38.                 // 画背景图的矩形  
  39.                 mCanvas.drawRect(dirty, paint);  
  40.                 pStart += per * per56;// 右边的间距 5/6  
  41.             }  
  42.         }else{  
  43.             for(SingleStockInfo info:infos){  
  44.                 pStart += per * per16;// 每一天实际所占的数据是4/6,加上1/6  
  45.                 pStartY = (float) (pillarsChartTop + (maxCount - info.getTotalCount()) * heightScale);  
  46.                 dirty = new Rect(pStart, (int) pStartY, (int) (pStart + per * per46), pillarsChartbottom-2);  
  47.                 paint.setColor(info.getColor());  
  48.                 // 画背景图的矩形  
  49.                 mCanvas.drawRect(dirty, paint);  
  50.                 pStart += per * per56;// 右边的间距 5/6  
  51.             }  
  52.         }  
  53.     }  

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. /** 
  2.      * 分时图 
  3.      */  
  4.     @Override  
  5.     public void drawHoursChart(){  
  6.         double heightScale = (KChartbottom - kChartTop)/(highPrice - lowPrice);  
  7.         int cLeft = left;  
  8.         int cTop = 0;  
  9.         Path path = new Path();  
  10.         path.moveTo(cLeft, KChartbottom-2);  
  11.         int position = 0;  
  12.         int perPointX = perXPoint[position];// 记录第一条垂直虚线的x坐标  
  13.         for(MinuteInfo info:minuteInfos){  
  14.             cLeft += per * per16;  
  15.             cTop = (int) (kChartTop + (highPrice - info.getNow()) * heightScale);  
  16.             path.lineTo(cLeft + per * per26, cTop);  
  17.             if (cLeft >= perPointX){  
  18.                 // 恰好画到第一条垂直虚线的地方,需要画下面的时间  
  19.                 String text = KChartUtil.getMinute(info.getMinute());  
  20.                 float textWidth = textGrayPaint.measureText(text);  
  21.                 int textHeight = (int) (textGrayPaint.descent()- textGrayPaint.ascent());  
  22.                 mCanvas.drawText(text, perPointX - textWidth/2, KChartbottom + textHeight, textGrayPaint);  
  23.                 if (!(position == perXPoint.length-1)){  
  24.                     Log.e(TAG, perPointX+"----------"+info.getMinute()+"---"+text);  
  25.                     perPointX = perXPoint[++position];  
  26.                 }  
  27.             }  
  28.             cLeft += per * per56;// 右边的间距 5/6  
  29.         }  
  30.         path.lineTo(cLeft, KChartbottom-2);  
  31.         Paint LinePaint = new Paint();  
  32.         LinePaint.setColor(Color.BLUE);  
  33.         LinePaint.setAntiAlias(true);  
  34.         LinePaint.setStrokeWidth(1);  
  35.         LinePaint.setStyle(Style.STROKE);  
  36. //      LinePaint.setStyle(Style.STROKE);  
  37.         mCanvas.drawPath(path, LinePaint);  
  38.         LinePaint.setAlpha(50);  
  39.         LinePaint.setStyle(Style.FILL);  
  40.         mCanvas.drawPath(path, LinePaint);  
  41.     }  

demo链接:https://github.com/chuanlongwu/kChart4Android 
1 0
原创粉丝点击