Android - 使用MPAndroidChart在两行之间填充颜色

时间:2017-04-11 09:54:57

标签: android linechart mpandroidchart

enter image description here

我正在使用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返回固定值。

2 个答案:

答案 0 :(得分:5)

感谢David Rawson指点我LineChartRenderer。我可以为两条线之间的区域着色。

我们需要做出两个重大改变。

  1. 实施自定义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;
    }}
    
  2. 实施自定义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();
    }}
    
  3. 活动结束时

    MyFillFormatter设置为LineDataSet之一,将另一个LineDataSet作为参数。

    lineDataSet2.setFillFormatter(new MyFillFormatter(LineDataSet1));
    
    mChart.setRenderer(new MyLineLegendRenderer(mChart, mChart.getAnimator(), mChart.getViewPortHandler()));
    
  4. https://technotailor.wordpress.com/2017/04/17/thread-leak-with-imageloadworker-in-flying-saucer-jar/

答案 1 :(得分:2)

我使用了Amit的accepted answer,但修改了他的MyLineLegendRenderer,这样你也可以填充两个水平bezier 行 - 例如,如果你使用的是{{1} }}

我还清理了一些代码 - 例如,添加了注释,删除了冗余代码等。

所以这是我替换Amit的myDataSet.setMode(LineDataSet.Mode.HORIZONTAL_BEZIER);课程:

MyLineLegendRenderer

您应该将此类与Amit's answer中的其他代码一起使用。