本文介紹了使用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
參數設置最小角度,以便可以看到每個扇區并保持它們之間的一定距離。
這篇關于使用MPandroidChart庫重疊餅圖標簽的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,