问题
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.
回答1:
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);
}
}
And then I consume it using:
leftAxis.setValueFormatter(new MyValueFormatter(yMin));
where yMin
was defined earlier:
private float yMin = 0;
and then assigned the chart's minimum yValue was passed in.
来源:https://stackoverflow.com/questions/41468599/how-can-i-alter-the-spacing-for-the-y-axis-labels-in-mpandroidchart