【问题标题】:MPAndroidChart - Legend labels are being cut offMPAndroidChart - 图例标签被切断
【发布时间】:2015-03-07 00:11:21
【问题描述】:

我正在使用MPAndroidChart library。有人有这个问题吗? 当我将标签放在 BOTTOM 位置时,这些标签被剪掉了。

谢谢

【问题讨论】:

    标签: android labels mpandroidchart


    【解决方案1】:

    这似乎是自 2015 年 6 月以来的新 feature:

    chart.getLegend().setWordWrapEnabled(true);
    

    Javadoc:

    /**
     * Should the legend word wrap? / this is currently supported only for:
     * BelowChartLeft, BelowChartRight, BelowChartCenter. / note that word
     * wrapping a legend takes a toll on performance. / you may want to set
     * maxSizePercent when word wrapping, to set the point where the text wraps.
     * / default: false
     * 
     * @param enabled
     */
    public void setWordWrapEnabled(boolean enabled) {
        mWordWrapEnabled = enabled;
    }
    

    【讨论】:

    • 非常感谢...我已经解决了 iOS chartView.legend.wordWrapEnabled = true;
    • 太棒了!你节省了我的时间
    【解决方案2】:

    它们被剪切是因为您的文本太长并且库不支持将标签“换行”到新行。

    您要么必须缩短图例标签,要么自己实现所需的功能。

    更新:

    现在支持 Legend 的自动换行。

    chart.getLegend().setWordWrapEnabled(true);
    

    【讨论】:

    • 这不适用于饼图
    【解决方案3】:

    您必须使用它们的图例颜色和标签来实现自定义图例 通过以下步骤 第 1 步

    Legend legend = mChart.getLegend();
    

    第 2 步

    int colorcodes[] = legend.Colors();
    

    步骤 3

    for (int i = 0; i <  legend.Colors().length-1; i++) {
     .....
     .....
     }
    

    步骤 4

    然后您必须采用水平或垂直的一种布局。您必须获取图例颜色代码和图例标签,并根据图例长度创建布局和标签。 代码示例如下

            LinearLayout.LayoutParams parms_left_layout = new LinearLayout.LayoutParams(
                    LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
            parms_left_layout.weight = 1F;
            LinearLayout left_layout = new LinearLayout(context);
            left_layout.setOrientation(LinearLayout.HORIZONTAL);
            left_layout.setGravity(Gravity.CENTER);
            left_layout.setLayoutParams(parms_left_layout);
    
            LinearLayout.LayoutParams parms_legen_layout = new LinearLayout.LayoutParams(
                    20, 20);
            parms_legen_layout.setMargins(0, 0, 20, 0);
            LinearLayout legend_layout = new LinearLayout(context);
            legend_layout.setLayoutParams(parms_legen_layout);
            legend_layout.setOrientation(LinearLayout.HORIZONTAL);
            legend_layout.setBackgroundColor(colorcodes[i]);
            left_layout.addView(legend_layout);
    
            TextView txt_unit = new TextView(context);
            txt_unit.setText(legend.getLabel(i));
    

    希望对你有帮助

    【讨论】:

    • 你能分享示例应用吗
    【解决方案4】:

    这里我将通过“传统Android方式”向您展示一个简单的方法,它很简单,我的代码如下:

    <LinearLayout
        android:id="@+id/i_am_chart_view_container"
        ...
        android:paddingRight="20dp"
        android:clipChildren="false"
        android:clipToPadding="false"
        .../>
    

    只需要在容器布局中添加一些padding,或者在图表视图中添加一些margin,最后将clipChildren&clipToPadding设置为false。

    结果如下:

    蓝色区域是填充或边距区域。

    【讨论】:

      【解决方案5】:

      当图例位置为时,设置换行内容仅支持图例位置 左下图,右下图,下图中心

           Legend legend = pieChart.getLegend();
              legend.setPosition(Legend.LegendPosition.BELOW_CHART_LEFT);
              legend.setWordWrapEnabled(true);
      

      经过这组数据集

       pieChart.setData(pieData)
      

      它会正常工作

      【讨论】:

        【解决方案6】:
         Legend l = pieChart.getLegend();
         l.setPosition(LegendPosition.BELOW_CHART_LEFT);
         l.setXEntrySpace(7f);
         l.setYEntrySpace(0f);
         l.setYOffset(0f);
         l.setDirection(LegendDirection.LEFT_TO_RIGHT);
         l.setWordWrapEnabled(true);
        

        【讨论】:

          【解决方案7】:

          为避免图例值被剪裁,请使用以下代码块

          Legend legend=lineChart.getLegend();
          legend.setWordWrapEnabled(true);
          

          文档(图例):

          /**
               * Should the legend word wrap? / this is currently supported only for:
               * BelowChartLeft, BelowChartRight, BelowChartCenter. / note that word
               * wrapping a legend takes a toll on performance. / you may want to set
               * maxSizePercent when word wrapping, to set the point where the text wraps.
               * / default: false
               * 
               * @param enabled
               */
              public void setWordWrapEnabled(boolean enabled) {
                  mWordWrapEnabled = enabled;
              }
          

          为避免剪切 x 轴标签使用

          XAxis xAxis = lineChart.getXAxis();
          xAxis.setAvoidFirstLastClipping(true);
          

          文档(用于 x 轴标签):

          /**
               * if set to true, the chart will avoid that the first and last label entry
               * in the chart "clip" off the edge of the chart or the screen
               * 
               * @param enabled
               */
              public void setAvoidFirstLastClipping(boolean enabled) {
                  mAvoidFirstLastClipping = enabled;
              }
          

          【讨论】:

          • 谢谢,这行得通,但是第一个和最后一个数据点值也被裁剪了,有什么解决方案吗?
          • @erical 您能否为您的问题提供屏幕截图?
          【解决方案8】:

          试试这个:

          Legend l = pieChart.getLegend();
          l.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
          l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT);
          l.setOrientation(Legend.LegendOrientation.HORIZONTAL);
          l.setDrawInside(false);
          l.setXEntrySpace(4f);
          l.setYEntrySpace(0f);
          l.setWordWrapEnabled(true);
          

          【讨论】:

            【解决方案9】:

            经过长时间的研究,我找到了解决方案。下面的代码解决了它。

            chart.getLegend().setWordWrapEnabled(true);

            【讨论】:

              猜你喜欢
              • 2023-04-08
              • 2021-12-16
              • 1970-01-01
              • 1970-01-01
              • 2018-11-07
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2012-12-05
              相关资源
              最近更新 更多