MPAndroidChart 的X坐标点文案换行问题应该比较常见,但是墙内搜了一下,好像都不甚直接,有一个自定义改源码的,私以为小题大做,以下给出解决方案
效果图
>
重点核心
核心代码
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行
/**图表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)
}
}
}
點(diǎn)擊查看更多內(nèi)容
1人點(diǎn)贊
評(píng)論
評(píng)論
共同學(xué)習(xí),寫下你的評(píng)論
評(píng)論加載中...
作者其他優(yōu)質(zhì)文章
正在加載中
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說(shuō)多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開(kāi)微信掃一掃,即可進(jìn)行掃碼打賞哦