这似乎是自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;
}
由于您的文本过长并且库不支持“换行”,因此它们被截断。
您必须缩短图例标签或自己实现所需功能。
更新:
现在支持为Legend
文字换行。
chart.getLegend().setWordWrapEnabled(true);
我将向您展示一种“传统Android方式”的简单方法,它非常简单,我的代码如下:
<LinearLayout
android:id="@+id/i_am_chart_view_container"
...
android:paddingRight="20dp"
android:clipChildren="false"
android:clipToPadding="false"
.../>
只需将 padding
添加到容器布局中,或将 margin
添加到图表视图中,最后将 clipChildren
和 clipToPadding
设置为 false。
下面是结果:
蓝色区域是填充或边缘区域。
您需要按照以下步骤实现带有图例颜色和标签的自定义图例: 步骤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));
希望这能帮助您
当图例位置为BelowChartLeft、BelowChartRight、BelowChartCenter时,仅支持设置包裹内容。
Legend legend = pieChart.getLegend();
legend.setPosition(Legend.LegendPosition.BELOW_CHART_LEFT);
legend.setWordWrapEnabled(true);
pieChart.setData(pieData)
它将能够良好地工作。
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);
/**
* 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;
}
试试这个:
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);
chart.getLegend().setWordWrapEnabled(true);
piechart.getLegend().setWordWrapEnabled(true);
chartView.legend.wordWrapEnabled = true;
- HirenisWordWrapEnabled
,您必须在设置数据之前设置它。 - M_droid