MPAndroidChart的K线图上添加均线

0
0
0
1. 云栖社区>
2. 博客>
3. 正文

# MPAndroidChart的K线图上添加均线

• 效果图

## 均线计算方法：

5节点均线为例，5节点均线是从第五根柱子开始画，一直画到最后一柱子（10节点均线就是从第10根柱子开始画，画到最后一根）

## 源码

MPAndroidChart地址：https://github.com/PhilJay/MPAndroidChart

## Demo Code

``````@Override
public void drawData(Canvas c) {

CandleData candleData = mChart.getCandleData();

for (CandleDataSet set : candleData.getDataSets()) {

if (set.isVisible() && set.getEntryCount() > 0)
drawDataSet(c, set);
}
}

protected void drawDataSet(Canvas c, CandleDataSet dataSet) {

Transformer trans = mChart.getTransformer(dataSet.getAxisDependency());

float phaseX = mAnimator.getPhaseX();
float phaseY = mAnimator.getPhaseY();

int dataSetIndex = mChart.getCandleData().getIndexOfDataSet(dataSet);

List<CandleEntry> entries = dataSet.getYVals();

int minx = Math.max(mMinX, 0);
int maxx = Math.min(mMaxX + 1, entries.size());

int range = (maxx - minx) * 4;
int to = (int) Math.ceil((maxx - minx) * phaseX + minx);

CandleBodyBuffer bodyBuffer = mBodyBuffers[dataSetIndex];
bodyBuffer.setBodySpace(dataSet.getBodySpace());
bodyBuffer.setPhases(phaseX, phaseY);
bodyBuffer.limitFrom(minx);
bodyBuffer.limitTo(maxx);
bodyBuffer.feed(entries);

trans.pointValuesToPixel(bodyBuffer.buffer);

ArrayList<Float> closes = new ArrayList<Float>();
ArrayList<Float> jx = new ArrayList<Float>();
// draw the body
for (int j = 0; j < range; j += 4) {

// get the entry
CandleEntry e = entries.get(j / 4 + minx);

if (!fitsBounds(e.getXIndex(), mMinX, to))
continue;

if (e.getOpen() > e.getClose())
mRenderPaint.setColor(
dataSet.getDecreasingColor() == ColorTemplate.COLOR_NONE ?
dataSet.getColor(j) :
dataSet.getDecreasingColor()
);

else if (e.getOpen() < e.getClose())
mRenderPaint.setColor(
dataSet.getIncreasingColor() == ColorTemplate.COLOR_NONE ?
dataSet.getColor(j) :
dataSet.getIncreasingColor()
);

else
mRenderPaint.setColor(
dataSet.getColor(j) :
);

} else {
mRenderPaint.setColor(
dataSet.getColor(j) :
);
}

mRenderPaint.setStyle(Paint.Style.STROKE);

mRenderPaint);

float leftBody = bodyBuffer.buffer[j];
float open = bodyBuffer.buffer[j + 1];
float rightBody = bodyBuffer.buffer[j + 2];
float close = bodyBuffer.buffer[j + 3];

// draw body differently for increasing and decreasing entry
if (open > close) { // decreasing

if (dataSet.getDecreasingColor() == ColorTemplate.COLOR_NONE) {
mRenderPaint.setColor(dataSet.getColor(j / 4 + minx));
} else {
mRenderPaint.setColor(dataSet.getDecreasingColor());
}

mRenderPaint.setStyle(dataSet.getDecreasingPaintStyle());
// draw the body
c.drawRect(leftBody, close, rightBody, open, mRenderPaint);

} else if (open < close) {

if (dataSet.getIncreasingColor() == ColorTemplate.COLOR_NONE) {
mRenderPaint.setColor(dataSet.getColor(j / 4 + minx));
} else {
mRenderPaint.setColor(dataSet.getIncreasingColor());
}

mRenderPaint.setStyle(dataSet.getIncreasingPaintStyle());
// draw the body
c.drawRect(leftBody, open, rightBody, close, mRenderPaint);
} else { // equal values

c.drawLine(leftBody, open, rightBody, close, mRenderPaint);
}

// 5节点均线
drawLine(c, 5, closes, jx);
// 10节点均线
drawLine(c, 10, closes, jx);
// 30节点均线
drawLine(c, 30, closes, jx);
}
}

/**
* 画均线
* @param c
* @param i
* @param closes
* @param jx
*/
private void drawLine(Canvas c, int i, ArrayList<Float> closes, ArrayList<Float> jx) {
Paint paint = new Paint();
// 画线宽度
paint.setStrokeWidth(1);
// 画线颜色
if (5 == i) {
paint.setColor(Color.BLUE);
} else if (10 == i) {
paint.setColor(Color.RED);
} else if (30 == i) {
paint.setColor(Color.BLACK);
} else {
paint.setColor(Color.GREEN);
}

if (closes.size() >= i + 1) {
float startX = jx.get(closes.size() - 2);
float startY = 0;
for (int x = 0; x < i; x++) {
startY += closes.get(closes.size() - 2 - x);
}
startY = startY / i;
float endX = jx.get(closes.size() - 1);
float endY = 0;
for (int y = 0; y < i; y++) {
endY += closes.get(closes.size() - 1 - y);
}
endY = endY / i;
c.drawLine(startX, startY, endX, endY, paint);
}
}``````

+ 关注

corcosa 10627人浏览