MPAndroidChart - 图例标签被切断

MPAndroidChart - Legend labels are being cut off

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

谢谢

由于您的文本太长并且图书馆不支持"wrapping"的标签新行。

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

更新:

现在支持 Legend 的自动换行。

chart.getLegend().setWordWrapEnabled(true);

您必须使用图例颜色和标签来实现自定义图例 通过以下步骤 步骤 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));

希望对您有所帮助

这里我给大家介绍一个简单的方法"Traditional Android Way",很简单,我的代码如下:

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

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

结果如下:

蓝色区域为padding或margin区域

这似乎是自 6 月(2015 年)以来的新 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;
}
 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);

为避免截取图例值,请使用以下代码块

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;
    }

当图例位置为 BelowChartLeft、BelowChartRight、BelowChartCenter

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

在这个设置数据集之后

 pieChart.setData(pieData)

它会很好

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

chart.getLegend().setWordWrapEnabled(true);

试试这个:

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);