我在MPAndroidChart的柱状图的x轴文本太长了。我该如何旋转 x 轴标签 90 度?
现在在库的版本2.1.6中是可行的
尝试这个
XAxis xAxis=barChart.getXAxis();
xAxis.setLabelRotationAngle(-45);
很简单,试试这个...
XAxis xAxis = barChart.getXAxis();
xAxis.setLabelRotationAngle(90);
public class VerticalTextView extends TextView {
final boolean topDown;
public VerticalTextView(Context context, AttributeSet attrs) {
super(context, attrs);
final int gravity = getGravity();
if (Gravity.isVertical(gravity) && (gravity & Gravity.VERTICAL_GRAVITY_MASK) == Gravity.BOTTOM) {
setGravity((gravity & Gravity.HORIZONTAL_GRAVITY_MASK) | Gravity.TOP);
topDown = false;
} else
topDown = true;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(heightMeasureSpec, widthMeasureSpec);
setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth());
}
@Override
protected void onDraw(Canvas canvas) {
TextPaint textPaint = getPaint();
textPaint.setColor(getCurrentTextColor());
textPaint.drawableState = getDrawableState();
canvas.save();
if (topDown) {
canvas.translate(getWidth(), 0);
canvas.rotate(90);
} else {
canvas.translate(0, getHeight());
canvas.rotate(-90);
}
canvas.translate(getCompoundPaddingLeft(), getExtendedPaddingTop());
getLayout().draw(canvas);
canvas.restore();
}
}
只需要重写 XAxisRenderer
和 renderAxisLabels(c: Canvas)
方法。
然后将其设置为 barChart itemView.barChart.setXAxisRenderer(CustomBarChartRenderer(itemView.barChart.viewPortHandler, itemView.barChart.xAxis, itemView.barChart.rendererXAxis.transformer))
import android.graphics.Canvas
import com.github.mikephil.charting.renderer.XAxisRenderer
import com.github.mikephil.charting.utils.ViewPortHandler
import com.github.mikephil.charting.components.XAxis
import com.github.mikephil.charting.utils.MPPointF
import com.github.mikephil.charting.utils.Transformer
class CustomBarChartRenderer(viewPortHandler: ViewPortHandler, xAxis: XAxis, trans: Transformer) : XAxisRenderer(viewPortHandler, xAxis, trans) {
override fun renderAxisLabels(c: Canvas) {
if (!mXAxis.isEnabled || !mXAxis.isDrawLabelsEnabled)
return
val yoffset = mXAxis.yOffset
mAxisLabelPaint.typeface = mXAxis.typeface
mAxisLabelPaint.textSize = mXAxis.textSize
mAxisLabelPaint.color = mXAxis.textColor
val pointF = MPPointF.getInstance(0f, 0f)
if (mXAxis.position == XAxis.XAxisPosition.TOP) {
pointF.x = 0.5f
pointF.y = 1.0f
drawLabels(c, mViewPortHandler.contentTop() - yoffset, pointF)
} else if (mXAxis.position == XAxis.XAxisPosition.TOP_INSIDE) {
pointF.x = 0.5f
pointF.y = 1.0f
drawLabels(c, mViewPortHandler.contentBottom() - yoffset, pointF)
} else if (mXAxis.position == XAxis.XAxisPosition.BOTTOM) {
pointF.x = 0.5f
pointF.y = 0.0f
drawLabels(c, mViewPortHandler.contentBottom() + yoffset, pointF)
} else if (mXAxis.position == XAxis.XAxisPosition.BOTTOM_INSIDE) {
pointF.x = 0.5f
pointF.y = 0.0f
drawLabels(c, mViewPortHandler.contentBottom() - yoffset - mXAxis.mLabelRotatedHeight.toFloat(), pointF)
} else { // BOTH SIDED
pointF.x = 0.5f
pointF.y = 1.0f
drawLabels(c, mViewPortHandler.contentTop() - yoffset, pointF)
pointF.x = 0.5f
pointF.y = 0.0f
drawLabels(c, mViewPortHandler.contentBottom() + yoffset, pointF)
}
MPPointF.recycleInstance(pointF)
}
}