英文:
How to center the legend in pie chart drawn using MPAndroidChart library?
问题
以下是翻译好的部分:
我所做的: 我使用 MPAndroidChart 库创建了一个饼图,结果图例位于左上角。我尝试改变饼图的宽度,但是当项目增加时,图例会换行到第二行。
我期望的: 我希望每次更新时图例都位于中心。即使图例换行到第二行,我也希望它位于中心。我会对任何关于此的建议表示感激。
感谢!
<com.github.mikephil.charting.charts.PieChart
android:id="@+id/chart"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_gravity="center_horizontal">
</com.github.mikephil.charting.charts.PieChart>
活动代码:
private void setupPieChart(){
// 填充 PieEntry 列表
List<PieEntry> pieEntries = new ArrayList<>();
for (int i = 0; i < time.length; i++) {
pieEntries.add(new PieEntry(time[i], activity[i]));
}
PieDataSet dataSet = new PieDataSet(pieEntries, "");
dataSet.setColors(ColorTemplate.MATERIAL_COLORS);
PieData data = new PieData(dataSet);
// 饼片文本值
data.setValueTextSize(10);
// 获取图表
pieChart.setData(data);
pieChart.invalidate();
pieChart.setCenterText("50%");
pieChart.setDrawEntryLabels(false);
pieChart.setContentDescription("");
//pieChart.setDrawMarkers(true);
//pieChart.setMaxHighlightDistance(34);
pieChart.setEntryLabelTextSize(12);
pieChart.setHoleRadius(75);
// 移除左下角描述
pieChart.getDescription().setEnabled(false);
// 图例属性
Legend legend = pieChart.getLegend();
legend.setForm(Legend.LegendForm.CIRCLE);
legend.setTextSize(12);
legend.setFormSize(20);
legend.setFormToTextSpace(2);
// 换行图例文本
legend.setWordWrapEnabled(true);
Log.d("legend ", legend.getEntries().toString());
}
<details>
<summary>英文:</summary>
**What I have done:** I have created a pie chart using [MPAndroidChart][1] library and ended up having the legend in the left corner. I tried changing the pie chart width but then when the items increase legend tends to wrap to a second line.
**What I expect:** I want the legend to be in the center every time when it updates. Even legend wrap to the second line, I want it to be in the center. I would appreciate any suggestions on this.
Thank you!
[![enter image description here][2]][2]
<com.github.mikephil.charting.charts.PieChart
android:id="@+id/chart"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_gravity="center_horizontal"
>
</com.github.mikephil.charting.charts.PieChart>
Activity Code :
private void setupPieChart(){
//pupulating list of PieEntires
List<PieEntry> pieEntires = new ArrayList<>();
for( int i = 0 ; i<time.length;i++){
pieEntires.add(new PieEntry(time[i],activity[i]));
}
PieDataSet dataSet = new PieDataSet(pieEntires,"");
dataSet.setColors(ColorTemplate.MATERIAL_COLORS);
PieData data = new PieData(dataSet);
//pie slices text value
data.setValueTextSize(10);
//Get the chart
pieChart.setData(data);
pieChart.invalidate();
pieChart.setCenterText("50%");
pieChart.setDrawEntryLabels(false);
pieChart.setContentDescription("");
//pieChart.setDrawMarkers(true);
//pieChart.setMaxHighlightDistance(34);
pieChart.setEntryLabelTextSize(12);
pieChart.setHoleRadius(75);
//to remove bottom left description
pieChart.getDescription().setEnabled(false);
//legend attributes
Legend legend = pieChart.getLegend();
legend.setForm(Legend.LegendForm.CIRCLE);
legend.setTextSize(12);
legend.setFormSize(20);
legend.setFormToTextSpace(2);
//to wrap legend text
legend.setWordWrapEnabled(true);
Log.d("legend " ,legend.getEntries().toString());
}
[1]: https://github.com/PhilJay/MPAndroidChart
[2]: https://i.stack.imgur.com/YFISM.png
</details>
# 答案1
**得分**: 3
我能够通过在上面问题中提到的 `setupPieChart()` 中添加以下行来将图例居中显示。
legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);
<details>
<summary>英文:</summary>
I was able to center the legend by adding the below line in `setupPieChart()` mentioned in the question above.
legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论