上两篇说了分时图的画法,这篇来说下股票k线图的画法
因为股票k线图有蜡烛图和线性图所以我们需要用组合图来完成,首先我们需要重写组合图来实现markerView的自定义
根据自己需求而定这里我只是怎么实现的思路及部分代码
1.画markerview
1>继承CombinedChart并重写drawMarkers这个方法
2>将自定义的view画上去,
1.画左边的标记值
3>底部标记
2.画最高最低的标记
因为我们画k线的最高最低点所以我们找到CandleStickChartRenderer这个类,在这个类中我们找到drawValues这个方法,我们添加以下代码
//这个方法是我自己加的,是否显示最大最小
if (dataSet.getIsShowMaxAndMin()) {
//计算最大最小值
float maxValue = 0, minValue = 0;
int maxIndex = 0, minIndex = 0;
CandleEntry maxEntry = null;
boolean firstInit = true;
for (int j = 0; j < positions.length; j += 2) {
float x = positions[j];
float y = positions[j + 1];
if (!mViewPortHandler.isInBoundsRight(x))
break;
if (!mViewPortHandler.isInBoundsLeft(x) || !mViewPortHandler.isInBoundsY(y))
continue;
CandleEntry entry = dataSet.getEntryForIndex((j / 2 + mXBounds.min));
if (firstInit) {
maxValue = entry.getHigh();
minValue = entry.getLow();
firstInit = false;
maxEntry = entry;
} else {
if (entry.getHigh() > maxValue) {
maxValue = entry.getHigh();
maxIndex = j;
maxEntry = entry;
}
if (entry.getLow() < minValue) {
minValue = entry.getLow();
minIndex = j;
}
}
}
//绘制最大值和最小值
float x = positions[minIndex];
if (maxIndex > minIndex) {
//画右边
String highString = "← " + Float.toString(minValue);
//计算显示位置
//计算文本宽度
int highStringWidth = Utils.calcTextWidth(mValuePaint, highString);
float[] tPosition = new float[2];
tPosition[1] = minValue;
trans.pointValuesToPixel(tPosition);
mValuePaint.setTextSize(20);
mValuePaint.setColor(dataSet.getValueTextColor(minIndex / 2));
mValuePaint.setStrokeWidth(3f);
c.drawText(highString, x + highStringWidth / 2, tPosition[1], mValuePaint);
} else {
//画左边
String highString = Float.toString(minValue) + " →";
//计算显示位置
int highStringWidth = Utils.calcTextWidth(mValuePaint, highString);
float[] tPosition = new float[2];
tPosition[1] = minValue;
trans.pointValuesToPixel(tPosition);
mValuePaint.setTextSize(20);
mValuePaint.setColor(dataSet.getValueTextColor(minIndex / 2));
mValuePaint.setStrokeWidth(3f);
c.drawText(highString, x - highStringWidth / 2, tPosition[1], mValuePaint);
}
if (maxIndex > minIndex) {
//画左边
String highString = Float.toString(maxValue) + " →";
int highStringWidth = Utils.calcTextWidth(mValuePaint, highString);
float[] tPosition = new float[2];
tPosition[0] = maxEntry == null ? 0f : maxEntry.getX();
tPosition[1] = maxEntry == null ? 0f : maxEntry.getHigh();
trans.pointValuesToPixel(tPosition);
mValuePaint.setTextSize(20);
mValuePaint.setColor(dataSet.getValueTextColor(maxIndex / 2));
mValuePaint.setStrokeWidth(3f);
c.drawText(highString, tPosition[0] - highStringWidth / 2, tPosition[1], mValuePaint);
} else {
//画右边
String highString = "← " + Float.toString(maxValue);
//计算显示位置
int highStringWidth = Utils.calcTextWidth(mValuePaint, highString);
float[] tPosition = new float[2];
tPosition[0] = maxEntry == null ? 0f : maxEntry.getX();
tPosition[1] = maxEntry == null ? 0f : maxEntry.getHigh();
trans.pointValuesToPixel(tPosition);
mValuePaint.setTextSize(20);
mValuePaint.setColor(dataSet.getValueTextColor(maxIndex / 2));
mValuePaint.setStrokeWidth(3f);
c.drawText(highString, tPosition[0] + highStringWidth / 2, tPosition[1], mValuePaint);
}
}
这样就可以实现最大最小了。
下篇文章我们说说k线的分页滑动加载更多和高亮线的相关
有问题希望大家提出来