MPAndroidChart RadarChart自身崩溃 [英] MPAndroidChart RadarChart collapses itself

查看:296
本文介绍了MPAndroidChart RadarChart自身崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用RadarChart标签时遇到了一些问题.具体来说,如果我使用长文本(例如15-20个字符),并且可用空间不太大,则图表会折叠,并且标签会位于折叠后的图表内(但显然有更多可用空间).

I have some problems with RadarChart labels. Specifically, if I use long texts (e.g. 15-20 chars), and the available space is not too big, the chart gets collapsed and the labels are positioned inside the collapsed chart (but there is clearly more space available).

我尝试对标签轴使用ValueFormatter来截断长度超过5个字符的标签,但是正如我所看到的,图表大小计算基于整个标签文本,因为图表以相同的方式折叠我之前说过.

I tried to use ValueFormatter for the label axis that truncates labels if they are longer than 5 chars, but as I see, the chart size calculaction is based on the full label text, as the chart got collapsed the same way I described before.

XAxis xAxis = radarChart.getXAxis();
xAxis.setValueFormatter(new XAxisValueFormatter() {
    @Override
    public String getXValue(String original, int index, ViewPortHandler viewPortHandler) {
        return original.length() > 5 ? original.substring(0, 5) + "…" : original;
    }
});

这里有一些图片可以阐明问题.图表显示在CardView内部,如您所见,所有面上都有足够的空间.前两张是在ValueFormatter设置下拍摄的,后两张是没有ValueFormatter设置的.

Here are some pictures to clarify the problem. The charts are displayed inside CardViews and as you can see, there is plenty of space remaining on all sides. The first two pictures are taken with the ValueFormatter set, the last two are without it.

推荐答案

这是我的解决方案

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

Now replace RadarChart in your layout file with MyRadarChart At last,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;
        }
    });

这篇关于MPAndroidChart RadarChart自身崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆