如何更改MPAndroidchart中Y轴标签的间距? [英] How can I alter the spacing for the Y-Axis Labels in MPAndroidchart?

查看:1518
本文介绍了如何更改MPAndroidchart中Y轴标签的间距?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从下图所示的给定值开始将YAxis标签抬高一个间隙?如果我尝试使用偏移量,则会使我的YAxis标签值相对于Y轴数据错误地绘制.

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.

到目前为止,这是我的代码:

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.

推荐答案

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

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

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);
    }
}

然后我使用:

leftAxis.setValueFormatter(new MyValueFormatter(yMin));

其中yMin的定义较早:

private float yMin = 0; 

,然后分配了图表的最小yValue.

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

这篇关于如何更改MPAndroidchart中Y轴标签的间距?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆