Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MPAndroidChart: LineChart fill does not stop at XAxis

I am using MPAndroidChart. When I use the LineChart setDrawFilled() method to fill in the space between the line and the x-axis, and manually adjust the range to be bigger than the default, the filled in portion will go past the x-axis as seen in this screenshot:enter image description here

The blue fill should stop at the green dotted line, which is over the x-axis, however it extends over it and into positive y-values.

*Upon further investigation, I found only seems to happen when the green line is set to 0.

like image 457
gonfy Avatar asked Jan 31 '15 03:01

gonfy


1 Answers

Another alternative would be to use the FillFormatter interface provided by the library: https://github.com/PhilJay/MPAndroidChart/blob/master/MPChartLib/src/com/github/mikephil/charting/utils/FillFormatter.java

What this class allows you to do is creating your own logic on where the "filling-line" should end.

Simply implement the FillFormatter class in your custom formatter class and return the value you want the fill-line to end at.

Example:

class MyCustomFillFormatter implements FillFormatter {

    // return the fill position
    public float getFillLinePosition(LineDataSet dataSet, LineData data, float chartMaxY, float chartMinY) {
        return 0f;
    }
}

Inside the getFillLinePosition(...) method you can of course develop your own logic on where the position of the line should be, depending on various influence factors.

After creating your formatter, set it to the LineChart:

lineChart.setFillFormatter(new MyCustomFillFormatter());
like image 95
Philipp Jahoda Avatar answered Oct 21 '22 04:10

Philipp Jahoda