这是我的解决方案
public class MyRadarChart extends RadarChart {
public MyRadarChart(Context context) {
super(context);
}
public MyRadarChart(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyRadarChart(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void init() {
super.init();
setXAxisRenderer();
}
private void setXAxisRenderer(){
this.mXAxisRenderer = new MyXAxisRendererRadarChart(mViewPortHandler,mXAxis,this);
}}
和
import android.graphics.Canvas;
import com.github.mikephil.charting.charts.RadarChart;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.renderer.XAxisRendererRadarChart;
import com.github.mikephil.charting.utils.FSize;
import com.github.mikephil.charting.utils.MPPointF;
import com.github.mikephil.charting.utils.Utils;
import com.github.mikephil.charting.utils.ViewPortHandler;
public class MyXAxisRendererRadarChart extends XAxisRendererRadarChart {
public MyXAxisRendererRadarChart(ViewPortHandler viewPortHandler, XAxis xAxis, RadarChart chart) {
super(viewPortHandler, xAxis, chart);
}
@Override
protected void drawLabels(Canvas c, float pos, MPPointF anchor) {
super.drawLabels(c, pos, anchor);
}
@Override
protected void computeSize() {
String longest = mXAxis.getLongestLabel();
String line[] = longest.split("\n");
if(line .length >1){
longest = line[0];
}
mAxisLabelPaint.setTypeface(mXAxis.getTypeface());
mAxisLabelPaint.setTextSize(mXAxis.getTextSize());
final FSize labelSize = Utils.calcTextSize(mAxisLabelPaint, longest);
final float labelWidth = labelSize.width;
final float labelHeight = Utils.calcTextHeight(mAxisLabelPaint, "Q");
final FSize labelRotatedSize = Utils.getSizeOfRotatedRectangleByDegrees(
labelWidth,
labelHeight,
mXAxis.getLabelRotationAngle());
mXAxis.mLabelWidth = Math.round(labelWidth);
mXAxis.mLabelHeight = Math.round(labelHeight);
mXAxis.mLabelRotatedWidth = Math.round(labelRotatedSize.width);
mXAxis.mLabelRotatedHeight = Math.round(labelRotatedSize.height);
FSize.recycleInstance(labelRotatedSize);
FSize.recycleInstance(labelSize);
}
@Override
protected void drawLabel(Canvas c, String formattedLabel, float x, float y, MPPointF anchor, float angleDegrees) {
String line[] = formattedLabel.split("\n");
if(line .length >1){
Utils.drawXAxisValue(c, line[0], x, y, mAxisLabelPaint, anchor, angleDegrees);
Utils.drawXAxisValue(c, line[1], x + mAxisLabelPaint.getTextSize(), y + mAxisLabelPaint.getTextSize(), mAxisLabelPaint, anchor, angleDegrees);
}else{
super.drawLabel(c,formattedLabel,x,y,anchor,angleDegrees);
}
}}
现在将布局文件中的 RadarChart 替换为 MyRadarChart 最后,setValueFormatter
XAxis xAxis = radarChart.getXAxis();
xAxis.setValueFormatter(new IAxisValueFormatter() {
@Override
public String getFormattedValue(float value, AxisBase axis) {
String title = "you title";
if(title.length()>6){
title = title.substring(0,6)+"\n"+title.substring(6,title.length());
}
return title;
}
});