MPAndroidChart - 图例标签被裁剪

27
我正在使用 MPAndroidChart库,有人遇到同样的问题吗? 当我把标签放在底部位置时,它们被截断了。
谢谢 输入图像描述
10个回答

52

这似乎是自2015年6月以来的一个新的功能

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

2
非常感谢...我已经解决了iOS的问题 chartView.legend.wordWrapEnabled = true; - Hiren
太好了!你节省了我的时间。 - milan manvar
注意:使用MPAndroidChart时,始终要注意调用的顺序。对于isWordWrapEnabled,您必须在设置数据之前设置它。 - M_droid

34

由于您的文本过长并且库不支持“换行”,因此它们被截断。

您必须缩短图例标签或自己实现所需功能。

更新:

现在支持为Legend文字换行。

chart.getLegend().setWordWrapEnabled(true);

这个无法与饼图一起使用。 - WISHY

8

我将向您展示一种“传统Android方式”的简单方法,它非常简单,我的代码如下:

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

只需将 padding 添加到容器布局中,或将 margin 添加到图表视图中,最后将 clipChildrenclipToPadding 设置为 false。

下面是结果:

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

enter image description here


7

您需要按照以下步骤实现带有图例颜色和标签的自定义图例: 步骤1

Legend legend = mChart.getLegend();

第二步

int colorcodes[] = legend.Colors();

步骤三

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

希望这能帮助您


你能分享一个样例应用程序吗? - WISHY

4

当图例位置为BelowChartLeft、BelowChartRight、BelowChartCenter时,仅支持设置包裹内容。

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

在设置数据集之后
 pieChart.setData(pieData)

它将能够良好地工作。


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

1
为了避免图例数值被裁剪,请使用以下代码块。
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 轴标签被截断,请使用:

使用 保留 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
@erical 你能否提供一下你的问题的截图吗? - Balalakshmi Sadhasivam

1

试试这个:

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

0
经过长时间的研究,我找到了解决方案。下面的代码解决了这个问题。

chart.getLegend().setWordWrapEnabled(true);


0
您可以使用以下函数来绘制饼图:
piechart.getLegend().setWordWrapEnabled(true);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接