Mpandroidchart: Offset Yaxis values when displayed above line chart

Created on 24 Feb 2016  路  4Comments  路  Source: PhilJay/MPAndroidChart

Currently have the following graph.

screen shot 2016-02-24 at 4 44 11 pm

Don't believe from what I've found in the docs, that there is a way to set the margin between the point on a line chart, and where it's associated Value Text appears. Ideally, would want more of a gap between the point on the graph and it's bar, so the values don't intersect the graph when drawing as a bezier curve.

If I'm missing this, any help would be appreciated, other would be an awesome feature to see added in future.

Most helpful comment

There is no need for you to modify the library.
I think you can achieve this by implementing your own LineChartRenderer, override drawValue method and simply add offset.

public class MyLineChartRenderer extends LineChartRenderer {

    @Px
    private int yOffset;

    public MyLineChartRenderer(int yOffset, LineDataProvider chart, ChartAnimator animator, ViewPortHandler viewPortHandler) {
        super(chart, animator, viewPortHandler);
        this.yOffset = yOffset;
    }

    @Override
    public void drawValue(Canvas c, ValueFormatter formatter, float value, Entry entry, int dataSetIndex, float x, float y, int color) {
        super.drawValue(c, formatter, value, entry, dataSetIndex, x, y + this.yOffset, color);
    }

}

Then you would set it like this:

int yValueOffset = getResources().getDimensionPixelSize(R.dimen.chart_y_offset);
lineChart.setRenderer(new MyLineChartRenderer(yValueOffset, lineChart, lineChart.getAnimator(), lineChart.getViewPortHandler()));

All 4 comments

Unfortunately there is currently no way to change this by default.
You will have to modify the library to achieve that.

How to change the library to achieve that?

There is no need for you to modify the library.
I think you can achieve this by implementing your own LineChartRenderer, override drawValue method and simply add offset.

public class MyLineChartRenderer extends LineChartRenderer {

    @Px
    private int yOffset;

    public MyLineChartRenderer(int yOffset, LineDataProvider chart, ChartAnimator animator, ViewPortHandler viewPortHandler) {
        super(chart, animator, viewPortHandler);
        this.yOffset = yOffset;
    }

    @Override
    public void drawValue(Canvas c, ValueFormatter formatter, float value, Entry entry, int dataSetIndex, float x, float y, int color) {
        super.drawValue(c, formatter, value, entry, dataSetIndex, x, y + this.yOffset, color);
    }

}

Then you would set it like this:

int yValueOffset = getResources().getDimensionPixelSize(R.dimen.chart_y_offset);
lineChart.setRenderer(new MyLineChartRenderer(yValueOffset, lineChart, lineChart.getAnimator(), lineChart.getViewPortHandler()));

@cpienovi do you have an updated version for 2020?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

manucheri picture manucheri  路  3Comments

chathudan picture chathudan  路  3Comments

galex picture galex  路  3Comments

SutharRohit picture SutharRohit  路  3Comments

blotfi picture blotfi  路  3Comments