android - 如何更改 MPAndroidchart 中 Y 轴标签的间距?

标签 android mpandroidchart

如何使我的 YAxis 标签升高一个间隙,即从给定值开始,如下图所示?如果我尝试使用偏移量,它会使我的 YAxis 标签值根据 Y 轴数据绘制不正确。

a stock price line chart with the YAxis labels starting from $412.66

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

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

这是我的图表的截图。

a chart with the YAxis labels starting from the correct value but with incorrect yValues

最佳答案

这是通过实现 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);
    }
}

然后我使用它:

leftAxis.setValueFormatter(new MyValueFormatter(yMin));

前面定义了 yMin:

private float yMin = 0; 

然后传入赋值图表的最小yValue。

a chart with the YAxis labels starting from the minimum yValue as per OP's requirement

关于android - 如何更改 MPAndroidchart 中 Y 轴标签的间距?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41468599/

相关文章:

android - 无法让 clipChildren=false 属性起作用

java - Android、OpenGL ES 2.0 - 为什么为 ByteBuffer.allocateDirect 调用设置每个 float 4 个字节?

java - MPAndroidChart,如何更改散点图中绘制值的位置?

android - MpChart 在条形图的 X 轴上绘制图标作为标签

android - MPAndroidChart 可以自动转换单位吗?

java - MPAndroidchart mChart 在 onCreate 和 onResume 之间设置为 null

android - 在安卓模拟器上通过su获取root权限

java - firebase mp3播放器中的暂停功能

android - 以编程方式将文件从缓存目录 move 到 SDCard

android - MPAndroidChart 有没有办法为不同的条设置不同的颜色?