解决方案: setExtraBottomOffset + 自定义 CustomXAxisRenderer
1.假设自定义x坐标的textSize为xAxis.textSize = 8f,需要设置底部额外控件提供换行的文字显示的空间 chart.extraBottomOffset = 2 * 8f,其中8就是刚刚设置的xAxis.textSize,其中2表示我的X坐标文字有2行。
xAxis.setTextSize(8f);
mLineChart.setExtraBottomOffset(2 * 8f);
2.重写类XAxisRenderer。
private static class CustomXAxisRenderer extends XAxisRenderer {
public CustomXAxisRenderer(ViewPortHandler viewPortHandler, XAxis xAxis, Transformer trans){
super(viewPortHandler, xAxis, trans);
}
@Override
protected void drawLabel(Canvas c, String formattedLabel, float x, float y, MPPointF anchor, float angleDegrees) {
//super.drawLabel(c, formattedLabel, x, y, anchor, angleDegrees);//注释掉
String[] lines = formattedLabel.split("\n");
for (int i = 0; i < lines.length; i++) {
float vOffset = i * mAxisLabelPaint.getTextSize();
Utils.drawXAxisValue(c, lines[i], x, y + vOffset, mAxisLabelPaint, anchor, angleDegrees);
}
}
}
3.设置表格属性
mLineChart.setXAxisRenderer(new CustomXAxisRenderer(mLineChart.getViewPortHandler(), mLineChart.getXAxis(), mLineChart.getTransformer(YAxis.AxisDependency.LEFT)));