使用MPandroidChart库重叠饼图标签

2022-06-15 00:00:00 pie-chart android java mpandroidchart

我正在使用PhilJayMPAndroidChart库:

implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0'

并且我实现了饼图。条目较多的饼图会导致标签重叠,如下所示:

但我需要类似以下图表的内容。这是非常非常漂亮的,没有任何重叠和标签背景:

这是我的代码:

PieDataSet dataSet = new PieDataSet(entries, getString(R.string.chart_guide));
dataSet.setDrawValues(DRAW_VALUES); // To show/not show Slice Texts
dataSet.setDrawIcons(DRAW_ICONS);
dataSet.setSliceSpace(0f);
dataSet.setIconsOffset(new MPPointF(0, 40));
dataSet.setSelectionShift(5f);
setSlicesColors(dataSet);
dataSet.setSelectionShift(0f);

// Outside values
dataSet.setXValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);
dataSet.setYValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);
dataSet.setValueLinePart1OffsetPercentage(20f);
dataSet.setValueLinePart1Length(0.2f);
dataSet.setValueLinePart2Length(0.4f);
dataSet.setUsingSliceColorAsValueLineColor(USING_SLICE_COLOR_AS_VALUE_LINE_COLOR);
binding.pieChart.setExtraOffsets(5, 10, 5, 5);

PieData data = new PieData(dataSet);
data.setValueFormatter(new PercentFormatter());
data.setValueTextSize(11f);
data.setValueTextColor(Color.BLUE);

binding.pieChart.setData(data);

binding.pieChart.highlightValues(null);
binding.pieChart.invalidate();

binding.pieChart.setDrawMarkers(DRAW_MARKERS); // To remove markers when click
binding.pieChart.setDrawEntryLabels(DRAW_ENTRY_LABELS); // To remove labels from piece of pie
binding.pieChart.setEntryLabelColor(Color.BLACK);
binding.pieChart.setEntryLabelTextSize(12f);

binding.pieChart.setUsePercentValues(USE_PERCENT_VALUES);
binding.pieChart.getDescription().setEnabled(DESCRIPTION_ENABLED); // To remove description of pie

binding.pieChart.setDragDecelerationFrictionCoef(0.95f);

binding.pieChart.setCenterText(generateCenterSpannableText());

binding.pieChart.setDrawHoleEnabled(DRAW_HOLE_ENABLED);
binding.pieChart.setHoleColor(Color.WHITE);

binding.pieChart.setTransparentCircleColor(Color.LTGRAY);
binding.pieChart.setTransparentCircleAlpha(110);

binding.pieChart.setHoleRadius(58f);
binding.pieChart.setTransparentCircleRadius(61f);

binding.pieChart.setDrawCenterText(DRAW_CENTER_TEXT);

binding.pieChart.setRotationAngle(0);
// enable rotation of the chart by touch
binding.pieChart.setRotationEnabled(ROTATION_ENABLED);
binding.pieChart.setHighlightPerTapEnabled(HIGHLIGHT_PER_TAP_ENABLED);

binding.pieChart.animateY(2000, Easing.EaseInOutQuad); // Starter Animation

legend = binding.pieChart.getLegend();
legend.setEnabled(GUIDE_ENABLED); // To show/not show Chart Guide
legend.setDrawInside(DRAW_INSIDE);

是否有实现我发送的图表的解决方案?


解决方案

尝试使用minAngleForSlices参数设置最小角度,以便可以看到每个扇区并保持它们之间的一定距离。

相关文章