MPAndroidChart 的X座標點文案換行問題應該比較常見,可是牆內搜了一下,好像都不甚直接,有一個自定義改源碼的,私覺得小題大作,如下給出解決方案android
核心代碼bash
mBarChart.setXAxisRenderer(CustomXAxisRenderer(mBarChart.viewPortHandler, mBarChart.xAxis, mBarChart.getTransformer(YAxis.AxisDependency.LEFT)))
複製代碼
其中mBarChart爲BarChart 對象, 假設自定義x座標的textSize爲xAxis.textSize = 12f
須要設置底部額外控件提供換行的文字顯示的空間 mBarChart.extraBottomOffset = 3 * 12f
,其中12就是剛剛設置的xAxis.textSize
其中3表示個人X座標文字有3行ide
/**圖表X座標文字換行解決方案 setExtraBottomOffset + 自定義 CustomXAxisRenderer
* https://stackoverflow.com/questions/32509174/in-mpandroidchart-library-how-to-wrap-x-axis-labels-to-two-lines-when-long
*/
class CustomXAxisRenderer(viewPortHandler: ViewPortHandler, xAxis: XAxis, trans: Transformer) : XAxisRenderer(viewPortHandler, xAxis, trans) {
override fun drawLabel(c: Canvas, formattedLabel: String, x: Float, y: Float, anchor: MPPointF, angleDegrees: Float) {
val lines = formattedLabel.split("\n")
for (i in lines.indices) {
val vOffset = i * mAxisLabelPaint.textSize
Utils.drawXAxisValue(c, lines[i], x, y + vOffset, mAxisLabelPaint, anchor, angleDegrees)
}
}
}
複製代碼