如何使我的 YAxis 标签升高一个间隙,即从给定值开始,如下图所示?如果我尝试使用偏移量,它会使我的 YAxis 标签值根据 Y 轴数据绘制不正确。
到目前为止,这是我的代码:
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);
}
这是我的图表的截图。
最佳答案
这是通过实现 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。
关于android - 如何更改 MPAndroidchart 中 Y 轴标签的间距?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41468599/