我使用的是setFillFormatter,但这对我没有帮助,setFillColor()穿过第二行(黑色),因为无法在第二行的y值处停止第一行(黄色)。
我想实现这样的目标:
dataSet.setFillFormatter(new IFillFormatter() {
@Override
public float getFillLinePosition(ILineDataSet dataSet, LineDataProvider dataProvider) {
return //return Y value of the second line for current X of line being filled;
}
});
对于第一行的每个x,有没有办法找到第二行的y值?我看到dataset和dataprovider都为每次getfilllineposition调用返回固定值。
最佳答案
感谢david rawson把我指向LineChartRenderer
。我能给两行之间的区域上色。
我们需要做两大改变。
实现自定义FillFormator
以返回另一行的数据集。
public class MyFillFormatter implements IFillFormatter {
private ILineDataSet boundaryDataSet;
public MyFillFormatter() {
this(null);
}
//Pass the dataset of other line in the Constructor
public MyFillFormatter(ILineDataSet boundaryDataSet) {
this.boundaryDataSet = boundaryDataSet;
}
@Override
public float getFillLinePosition(ILineDataSet dataSet, LineDataProvider dataProvider) {
return 0;
}
//Define a new method which is used in the LineChartRenderer
public List<Entry> getFillLineBoundary() {
if(boundaryDataSet != null) {
return ((LineDataSet) boundaryDataSet).getValues();
}
return null;
}}
实现一个自定义的
LineChartRenderer
来绘制和填充封闭的路径。public class MyLineLegendRenderer extends LineChartRenderer {
public MyLineLegendRenderer(LineDataProvider chart, ChartAnimator animator, ViewPortHandler viewPortHandler) {
super(chart, animator, viewPortHandler);
}
//This method is same as it's parent implemntation
@Override
protected void drawLinearFill(Canvas c, ILineDataSet dataSet, Transformer trans, XBounds bounds) {
final Path filled = mGenerateFilledPathBuffer;
final int startingIndex = bounds.min;
final int endingIndex = bounds.range + bounds.min;
final int indexInterval = 128;
int currentStartIndex = 0;
int currentEndIndex = indexInterval;
int iterations = 0;
// Doing this iteratively in order to avoid OutOfMemory errors that can happen on large bounds sets.
do {
currentStartIndex = startingIndex + (iterations * indexInterval);
currentEndIndex = currentStartIndex + indexInterval;
currentEndIndex = currentEndIndex > endingIndex ? endingIndex : currentEndIndex;
if (currentStartIndex <= currentEndIndex) {
generateFilledPath(dataSet, currentStartIndex, currentEndIndex, filled);
trans.pathValueToPixel(filled);
final Drawable drawable = dataSet.getFillDrawable();
if (drawable != null) {
drawFilledPath(c, filled, drawable);
} else {
drawFilledPath(c, filled, dataSet.getFillColor(), dataSet.getFillAlpha());
}
}
iterations++;
} while (currentStartIndex <= currentEndIndex);
}
//This is where we define the area to be filled.
private void generateFilledPath(final ILineDataSet dataSet, final int startIndex, final int endIndex, final Path outputPath) {
//Call the custom method to retrieve the dataset for other line
final List<Entry> boundaryEntry = ((MyFillFormatter)dataSet.getFillFormatter()).getFillLineBoundary();
final float phaseY = mAnimator.getPhaseY();
final Path filled = outputPath;
filled.reset();
final Entry entry = dataSet.getEntryForIndex(startIndex);
filled.moveTo(entry.getX(), boundaryEntry.get(0).getY());
filled.lineTo(entry.getX(), entry.getY() * phaseY);
// create a new path
Entry currentEntry = null;
Entry previousEntry = null;
for (int x = startIndex + 1; x <= endIndex; x++) {
currentEntry = dataSet.getEntryForIndex(x);
filled.lineTo(currentEntry.getX(), currentEntry.getY() * phaseY);
}
// close up
if (currentEntry != null && previousEntry!= null) {
filled.lineTo(currentEntry.getX(), previousEntry.getY());
}
//Draw the path towards the other line
for (int x = endIndex ; x > startIndex; x--) {
previousEntry = boundaryEntry.get(x);
filled.lineTo(previousEntry.getX(), previousEntry.getY() * phaseY);
}
filled.close();
}}
活动结束时
将
MyFillFormatter
设置为传递另一个LineDataSet
作为参数的LineDataSet
之一。lineDataSet2.setFillFormatter(new MyFillFormatter(LineDataSet1));
mChart.setRenderer(new MyLineLegendRenderer(mChart, mChart.getAnimator(), mChart.getViewPortHandler()));