我如何改变MPAndroidchart中y轴标签的间距?

时间:2022-09-26 19:08:16

How can I make my YAxis labels elevated with a gap i.e., start from a given value like the below picture? If I try using offset it makes my YAxis label values plot incorrectly against the Y-Axis data.

我如何使我的YAxis标签提升一个间隙。,从如下图的给定值开始?如果我尝试使用偏移量,它会使我的YAxis标签值与y轴数据不一致。

我如何改变MPAndroidchart中y轴标签的间距?

Here is my code so far:

这是我目前的代码:

  public void setChartProperties() {
        YAxis rightAxis = chart.getAxisRight();
        YAxis leftAxis = chart.getAxisLeft();
        XAxis xAxis = chart.getXAxis();
        chart.getLegend().setEnabled(false);
        chart.getDescription().setEnabled(false);
        chart.setDrawBorders(false);
        chart.setPinchZoom(false);
        chart.setAutoScaleMinMaxEnabled(true);
        chart.setExtraOffsets(0, 0, 0, 0);
        xAxis.setLabelCount(6, true);
        xAxis.setGranularity(1f);
        xAxis.setDrawGridLines(false);
        xAxis.setPosition(XAxisPosition.BOTTOM);
        xAxis.setAvoidFirstLastClipping(true);
        leftAxis.setPosition(YAxisLabelPosition.INSIDE_CHART);
        leftAxis.setDrawLabels(true);
        leftAxis.setSpaceBottom(60);
        leftAxis.setDrawGridLines(true);
        leftAxis.setLabelCount(3, true);
        leftAxis.setCenterAxisLabels(true);
        leftAxis.setDrawGridLines(false);
        rightAxis.setEnabled(false);
        xAxis.setAvoidFirstLastClipping(true);
        dataSet.setColor(R.color.graphLineColor);
    }

And here is a screenshot of what my chart looks like.

这是一张我的图表的截图。

我如何改变MPAndroidchart中y轴标签的间距?

1 个解决方案

#1


1  

This was achieved through implementing IAxisValueFormatter because I wanted to keep all values and just modify the labels:

这是通过实现IAxisValueFormatter实现的,因为我想保留所有值,只需修改标签:

public class MyValueFormatter implements IAxisValueFormatter {

    private final float cutoff;
    private final DecimalFormat format;

    public MyValueFormatter(float cutoff) {
        this.cutoff = cutoff;
        this.format = new DecimalFormat("###,###,###,##0.00");
    }

    @Override
    public String getFormattedValue(float value, AxisBase axis) {
        if (value < cutoff) {
            return "";
        }

        return "$" + format.format(value);
    }
}

And then I consume it using:

然后我用:

leftAxis.setValueFormatter(new MyValueFormatter(yMin));

where yMin was defined earlier:

其中yMin的定义较早:

private float yMin = 0; 

and then assigned the chart's minimum yValue was passed in.

然后分配了图表的最小值。

我如何改变MPAndroidchart中y轴标签的间距?

#1


1  

This was achieved through implementing IAxisValueFormatter because I wanted to keep all values and just modify the labels:

这是通过实现IAxisValueFormatter实现的,因为我想保留所有值,只需修改标签:

public class MyValueFormatter implements IAxisValueFormatter {

    private final float cutoff;
    private final DecimalFormat format;

    public MyValueFormatter(float cutoff) {
        this.cutoff = cutoff;
        this.format = new DecimalFormat("###,###,###,##0.00");
    }

    @Override
    public String getFormattedValue(float value, AxisBase axis) {
        if (value < cutoff) {
            return "";
        }

        return "$" + format.format(value);
    }
}

And then I consume it using:

然后我用:

leftAxis.setValueFormatter(new MyValueFormatter(yMin));

where yMin was defined earlier:

其中yMin的定义较早:

private float yMin = 0; 

and then assigned the chart's minimum yValue was passed in.

然后分配了图表的最小值。

我如何改变MPAndroidchart中y轴标签的间距?