如何使用 MPAndroidChart 实现此目的?
使用版本:com.github.PhilJay:MPAndroidChart:v3.1.0-alpha
添加图例和饼图边距的代码:
private void setPieChartData() {
//https://stackoverflow.com/a/50551488/1427037
pieChart.setUsePercentValues(true);
pieChart.getDescription().setEnabled(false);
pieChart.setExtraOffsets(5,5,10,5);
pieChart.setDragDecelerationFrictionCoef(0.9f);
pieChart.setDrawCenterText(false);
pieChart.setDrawHoleEnabled(false);
//pieChart.animateY(1000, Easing.EasingOption.EaseInOutCubic);
ArrayList<PieEntry> yValues = new ArrayList<>();
yValues.add(new PieEntry(34f,"Ilala"));
yValues.add(new PieEntry(56f,"Temeke"));
yValues.add(new PieEntry(66f,"Kinondoni"));
yValues.add(new PieEntry(45f,"Kigamboni"));
yValues.add(new PieEntry(45f,"Kigamboni2"));
pieChart.setDrawEntryLabels(false);
PieDataSet dataSet = new PieDataSet(yValues, "Desease Per Regions");
dataSet.setSliceSpace(0.1f);
dataSet.setDrawValues(false);
dataSet.setSelectionShift(5f);
dataSet.setColors(ColorTemplate.VORDIPLOM_COLORS);
PieData pieData = new PieData((dataSet));
pieData.setValueTextSize(10f);
pieData.setValueTextColor(Color.BLACK);
pieChart.setData(pieData);
pieChart.setDrawSliceText(false);
setLegend();
}
private void setLegend() {
Legend l = pieChart.getLegend();
l.setFormSize(10f); // set the size of the legend forms/shapes
l.setForm(Legend.LegendForm.SQUARE); // set what type of form/shape should be used
l.setYOffset(5f);
//l.setPosition(Legend.LegendPosition.RIGHT_OF_CHART);
l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP); // set vertical alignment for legend
l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT); // set horizontal alignment for legend
l.setOrientation(Legend.LegendOrientation.VERTICAL);
l.setTextSize(12f);
l.setTextColor(Color.BLACK);
//l.setXEntrySpace(5f); // set the space between the legend entries on the x-axis
l.setYEntrySpace(1f); // set the space between the legend entries on the y-axis
// set custom labels and colors
List<LegendEntry> entries = new ArrayList<>();
ArrayList<PieEntry> yValues = new ArrayList<>();
yValues.add(new PieEntry(34f,"Ilala"));
yValues.add(new PieEntry(56f,"Temeke"));
yValues.add(new PieEntry(66f,"Kinondoni"));
yValues.add(new PieEntry(45f,"Kigamboni"));
yValues.add(new PieEntry(45f,"Kigamboni2"));
for (int i = 0; i < 3; i++) {
LegendEntry entry = new LegendEntry();
entry.formColor = ColorTemplate.VORDIPLOM_COLORS[i];
entry.label = yValues.get(i).getLabel() ;
entries.add(entry);
}
l.setCustom(entries);
}
[![code result][2]][2]
如何将图例的位置设置为与实际图像相同我在设置其布局时遇到问题。请更正代码并分享任何链接以寻求帮助 请分享详细信息以解决此问题
最佳答案
要删除条目值和标签,请使用方法:
myPieChart.setDrawSliceText(false); // To remove slice text
myPieChart.setDrawMarkers(false); // To remove markers when click
myPieChart.setDrawEntryLabels(false); // To remove labels from piece of pie
myPieChart.getDescription().setEnabled(false); // To remove description of pie
要在图表旁边显示图例,请尝试以下方法:
Legend l = myPieChart.getLegend(); // get legend of pie
l.setVerticalAlignment(Legend.LegendVerticalAlignment.CENTER); // set vertical alignment for legend
l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT); // set horizontal alignment for legend
l.setOrientation(Legend.LegendOrientation.VERTICAL); // set orientation for legend
l.setDrawInside(false); // set if legend should be drawn inside or not
从 here 查看更多信息& 来自 MPAndroidChart 的主要文档页面.
关于Android MP 饼图如何从饼图中删除文本和值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53741738/