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