1. 程式人生 > >基於AChartEngine繪製股票走勢圖----K線圖一(繪製陰線陽線)

基於AChartEngine繪製股票走勢圖----K線圖一(繪製陰線陽線)

K線圖,繪製陰陽線

繪製上陽線,基於基礎的react圖形,cancas繪製時限定react的top和bottom,資料點處理時,一個點傳遞五個資料,對應開盤,收盤,最高最低,上陽線是上漲顯示紅色,此時收盤大於開盤,當開收低高全部相等時,判斷依據和上一個交易日的收盤價比較;封裝RangeBarSeries,StockBarChart;

/**
     * Transforms the range category series to an XY series.
     *
     * @return the XY series
     */
    public XYSeries toXYSeries
() { XYSeries xySeries = new XYSeries(getTitle()); int length = getItemCount(); for (int k = 0; k < length; k++) { xySeries.add(k + 0, mMaxValues.get(k).dOpen == null ? Double.NaN : mMaxValues.get(k).dOpen); // the new fast XYSeries implementation doesn't allow 2 values at the same X,
// so I had to do a hack until I find a better solution xySeries.add(k + 0.000001, mMaxValues.get(k).dClose == null ? 0.000002 : mMaxValues.get(k).dClose); xySeries.add(k + 0.000002, mMaxValues.get(k).dLow == null ? 0.000003 : mMaxValues.get(k).dLow); xySeries.add(k + 0.000003, mMaxValues.get
(k).dHigh == null ? 0.000004 : mMaxValues.get(k).dHigh); xySeries.add(k + 0.000004, mMaxValues.get(k).dPreClose == null ? 0.000005 : mMaxValues.get(k).dPreClose); } return xySeries; }

把值轉換成座標點對應的值,傳遞給Chart繪製,實際有效值是下標為5倍數,其他值用來判斷漲跌,其中dHigh,dLow繪製陰陽線的上下豎線;在StockRangBarChart裡將座標點轉換成值;

int seriesNr = mDataset.getSeriesCount();
        int length = points.size();
        paint.setColor(seriesRenderer.getColor());
        paint.setStyle(Paint.Style.FILL);
        float halfDiffX = getHalfDiffX(points, length, seriesNr);
        int start = 0;
        if (startIndex > 0) {
            start = 2;
        }
        for (int i = start; i < length; i += 10) {
            if (points.size() > i + 9) {
                //open,開盤價
                float xMin = points.get(i);
                float yMin = points.get(i + 1);
                //close,現價
                float xMax = points.get(i + 2);
                float yMax = points.get(i + 3);
                //low..high
                float kLow = points.get(i + 5);
                float kHigh = points.get(i + 7);
                //昨收
                float dPreClose = points.get(i + 9);

                if (kLow >= kHigh) {
                    float temp = kHigh;
                    kHigh = kLow;
                    kLow = temp;
                }

                if (yMax < yMin) {
                    //紅線
                    paint.setColor(Color.parseColor("#d74c44"));
                } else {
                    //綠線
                    paint.setColor(Color.parseColor("#4bbb59"));
                }

                //漲停,跌停
                if (kLow - kHigh == 0 || yMin - yMax == 0) {
                    if (yMax <= dPreClose) {
                        yMax += 0.1;
                        paint.setColor(Color.parseColor("#d74c44"));
                    } else {
                        yMin += 0.1;
                        paint.setColor(Color.parseColor("#4bbb59"));
                    }
                }

                drawBar(canvas, xMin, yMin, xMax, yMax, halfDiffX, seriesNr, seriesIndex, paint);
                //繪製上下陰影
                if (kHigh > yMax && kHigh > yMin) {
                    drawBar(canvas, xMin + (xMax - xMin) / 2, yMax > yMin ? yMax : yMin, xMin + (xMax - xMin) / 2, kHigh, 1, seriesNr, seriesIndex, paint);
                }
                if (kLow < yMax && kLow < yMin) {
                    drawBar(canvas, xMin + (xMax - xMin) / 2, yMax < yMin ? yMax : yMin, xMin + (xMax - xMin) / 2, kLow, 1, seriesNr, seriesIndex, paint);
                }
            }
        }
        paint.setColor(seriesRenderer.getColor());

需要注意的是每塊的間距是根據getHalfDiffX(List points, int length, int seriesNr)來獲取間距;

float barWidth = mRenderer.getBarWidth();
        if (barWidth > 0) {
            return barWidth / 2;
        }
        int div = length;
        if (length > 4) {
            div = length / 2 - 2;
        }
        int pos = length / 10 * 10 - 10;
        float halfDiffX = pos > 0 ? (points.get(pos) - points.get(0)) / div : 0;
        if (halfDiffX == 0) {
            halfDiffX = 3f;
        }

        if (mType != Type.STACKED && mType != Type.HEAPED) {
            halfDiffX /= seriesNr;
        }
        return (float) (halfDiffX / (getCoeficient() * (1 + mRenderer.getBarSpacing())));

通過第一個點和最後一個點之間的x軸差,比較係數得出每個點之間的距離,需要注意的是單隻有一個點時,設定預設距離引數,可以根據螢幕畫素點來;