基于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轴差,比较系数得出每个点之间的距离,需要注意的是单只有一个点时,设置默认距离参数,可以根据屏幕像素点来;

html5和c++开源 K线图工具, Create interactive charts easily for your web projects. Used by tens of thousands of developers and 61 out of the world's 100 largest companies, Highcharts is the simplest yet most flexible charting API on the market. Read more » Download » TemperatureRainfallTokyo climateSunshine hoursJanFebMarAprMayJunJulAugSepOctNovDec5°C10°C15°C20°C25°C30°C0 mm50 mm100 mm150 mm200 mm250 mmHighcharts.com Highstock Highstock lets you create stock or general timeline charts in pure JavaScript. Including sophisticated navigation options like a small navigator series, preset date ranges, date picker, scrolling and panning. Read more » Download » ZoomUSD to EUR14. Feb21. Feb28. Feb7. Mar14. Mar21. Mar28. Mar4. Apr11. Apr18. Apr25. Apr2. May9. May20042006200820100.70.650.751m3m6mYTD1yAllHighcharts.com Highmaps Interactive map charts with drilldown and touch support. Build interactive maps to display sales, election results or any other information linked to geography. Perfect for standalone use or in dashboards in combination with Highcharts! Read more » Download » Highcharts © Natural Earth Highcharts Cloud Online charts for non-techies. Create smashing, interactive diagrams for your news site or blog, or for sharing with your friends on social media. Read more » Highcharts Cloud 1 2 3 4 Free for non-commercial Do you want to use Highcharts for a personal website, a school site or a non-profit organisation? Then you don't need our permission, just go on! HTML 5 Based on native browser technologies, no plugins needed Open Fork us on GitHub and participate in tech discussions Compatible With modern browsers including mobile, tablets and old IE back to IE6 Dynamic Add, remove and modify series and points or modify axes Any chart you'd like Highcharts supports line, spline, area, areaspline, column, bar, pie, scatter, angular gauges, arearange, areasplinerang
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值