在MPAndroidChart库中,如何在不久的时候将X轴标签包装成两行?

Nin*_*der 15 android mpandroidchart

我试图显示X轴标签在它们很长时转到2行.如何在LineChart中实现这一目标?见下面的截图.我想要时间去第二行,而不是留在第二行

在此输入图像描述

Gui*_*nel 25

对于那些想要实现这一目标但保留原始库的人来说,这是一个受@ fgueli修改启发的简单解决方案.这仅适用于一个折线(在标签中添加"\n"),但您可以轻松地根据您的需要进行调整.

  1. 子类XAxisRenderer

    public class CustomXAxisRenderer extends XAxisRenderer {
        public CustomXAxisRenderer(ViewPortHandler viewPortHandler, XAxis xAxis, Transformer trans) {
             super(viewPortHandler, xAxis, trans);
        }
    
        @Override
        protected void drawLabel(Canvas c, String formattedLabel, float x, float y, MPPointF anchor, float angleDegrees) {
            String line[] = formattedLabel.split("\n");
            Utils.drawXAxisValue(c, line[0], x, y, mAxisLabelPaint, anchor, angleDegrees);
            Utils.drawXAxisValue(c, line[1], x + mAxisLabelPaint.getTextSize(), y + mAxisLabelPaint.getTextSize(), mAxisLabelPaint, anchor, angleDegrees);
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)

  1. 在所需的图表上设置此渲染器

    lineChart.setXAxisRenderer(new CustomXAxisRenderer(lineChart.getViewPortHandler(), lineChart.getXAxis(), lineChart.getTransformer(YAxis.AxisDependency.LEFT)));
    
    Run Code Online (Sandbox Code Playgroud)

  1. 请享用!

LineChart多行标签

  • @Ibo 很抱歉,但我不能:_您还不允许在帖子中嵌入图像,因此我们添加了一个链接。一旦您在网站上获得 10 点声誉,您就可以嵌入图像。_ (2认同)
  • 当设置 LEFT YAxis 以启用 false 来隐藏它时,此解决方案不适用于 Horizo​​ntalBarChart。当使用 RIGHT YAxis 获取变压器时,它给出 `12-19 18:46:19.631 10003-10003/com.aegisisc.samplecharts A/libc: Fatal signal 11 (SIGSEGV) at 0x00000004 (code=1), thread 10003 ( sc.samplecharts)` (2认同)
  • @RahulParihar,只需增加视口偏移,就可以看到第二行,如下所示:chart.setViewPortOffsets(0, 0, 0, getResources().getDimensionPixelSize(R.dimen.radiobutton_size)); 在我的情况下,radiobutton_size 为 36dp(文本大小为 8dp) (2认同)
  • 顺便说一句,如果想要让多行文本居中(因此行[0]将位于行[0]居中),他不应该使用`Utils.drawXAxisValue(c,line [1],x + mAxisLabelPaint.getTextSize() ,y + mAxisLabelPaint.getTextSize(),mAxisLabelPaint,anchor,angleDegrees);`而是`Utils.drawXAxisValue(c,line [1],x,y + mAxisLabelPaint.getTextSize(),mAxisLabelPaint,anchor,angleDegrees);` (2认同)

fgu*_*eli 15

我修改了库以允许使用\n在xAxis上允许多行标签

https://github.com/fabriziogueli/MPAndroidChart/tree/axis_multiline_labels

它现在正在工作,但需要进一步测试,也许需要一些代码样式/调整.

XAxis xAxis = mChart.getXAxis();
xAxis.setMultiLineLabel(true);
Run Code Online (Sandbox Code Playgroud)

然后你可以使用像这样的SimpleDateFormat:

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yy\nHH:mm a");
Run Code Online (Sandbox Code Playgroud)

也许您必须为图表设置额外的底部偏移量:

mChart.setExtraBottomOffset(50);
Run Code Online (Sandbox Code Playgroud)


Pet*_*ter 7

@Guillaume Jounel 的改进版本支持多个换行符,以及带换行符的字符串。

    @Override
    protected void drawLabel(Canvas c, String formattedLabel, float x, float y,
                                 MPPointF anchor, float angleDegrees) {
    String line[] = formattedLabel.split("\n");
    Utils.drawXAxisValue(c, line[0], x, y, mAxisLabelPaint, anchor, angleDegrees);
        for (int i = 1; i < line.length; i++) { // we've already processed 1st line
             Utils.drawXAxisValue(c, line[i], x, y + mAxisLabelPaint.getTextSize() * i,
                 mAxisLabelPaint, anchor, angleDegrees);
        }
    }
Run Code Online (Sandbox Code Playgroud)


Phi*_*oda 6

这看起来像是您必须通过根据需要修改库来实现的东西。

当前默认情况下不可能在 x 轴上有多条线。因此,原因是 AndroidCanvas不能简单地将字符串绘制"Line 1\nLine2"成两行,例如像这样。