MPAndroidChart icon indicating copy to clipboard operation
MPAndroidChart copied to clipboard

Offset Yaxis values when displayed above line chart

Open CameronBanga opened this issue 10 years ago • 5 comments

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.

CameronBanga avatar Feb 24 '16 22:02 CameronBanga

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

PhilJay avatar Feb 24 '16 23:02 PhilJay

How to change the library to achieve that?

razon30 avatar Oct 15 '16 07:10 razon30

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 avatar Oct 03 '17 20:10 cpienovi

@cpienovi do you have an updated version for 2020?

odahcam avatar May 12 '20 12:05 odahcam

my linechart has two datasets,use LineChartRenderer can make them change offsets, how can i just change one of them,the second doesn't need change position

andrewChen-a avatar May 26 '22 07:05 andrewChen-a