Skip to content

Commit 42cdba5

Browse files
committed
Add Curved Slices to Pie Chart
Finally added support for rounded slices, and somewhat improved the code for the PieChartRenderer class.
1 parent 29f4cc5 commit 42cdba5

File tree

6 files changed

+193
-119
lines changed

6 files changed

+193
-119
lines changed

MPChartExample/src/main/java/com/xxmassdeveloper/mpchartexample/PieChartActivity.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,18 @@ public boolean onOptionsItemSelected(MenuItem item) {
210210
chart.invalidate();
211211
break;
212212
}
213+
case R.id.actionToggleCurvedSlices: {
214+
boolean toSet = !chart.isDrawRoundedSlicesEnabled() || !chart.isDrawHoleEnabled();
215+
chart.setDrawRoundedSlices(toSet);
216+
if (toSet && !chart.isDrawHoleEnabled()) {
217+
chart.setDrawHoleEnabled(true);
218+
}
219+
if (toSet && chart.isDrawSlicesUnderHoleEnabled()) {
220+
chart.setDrawSlicesUnderHole(false);
221+
}
222+
chart.invalidate();
223+
break;
224+
}
213225
case R.id.actionDrawCenter: {
214226
if (chart.isDrawCenterTextEnabled())
215227
chart.setDrawCenterText(false);

MPChartExample/src/main/java/com/xxmassdeveloper/mpchartexample/PiePolylineChartActivity.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,18 @@ public boolean onOptionsItemSelected(MenuItem item) {
205205
chart.invalidate();
206206
break;
207207
}
208+
case R.id.actionToggleCurvedSlices: {
209+
boolean toSet = !chart.isDrawRoundedSlicesEnabled() || !chart.isDrawHoleEnabled();
210+
chart.setDrawRoundedSlices(toSet);
211+
if (toSet && !chart.isDrawHoleEnabled()) {
212+
chart.setDrawHoleEnabled(true);
213+
}
214+
if (toSet && chart.isDrawSlicesUnderHoleEnabled()) {
215+
chart.setDrawSlicesUnderHole(false);
216+
}
217+
chart.invalidate();
218+
break;
219+
}
208220
case R.id.actionDrawCenter: {
209221
if (chart.isDrawCenterTextEnabled())
210222
chart.setDrawCenterText(false);

MPChartExample/src/main/res/menu/pie.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525
android:id="@+id/actionToggleHole"
2626
android:title="@string/actionToggleHole">
2727
</item>
28+
<item
29+
android:id="@+id/actionToggleCurvedSlices"
30+
android:title="@string/actionToggleCurvedSlices">
31+
</item>
2832
<item
2933
android:id="@+id/actionDrawCenter"
3034
android:title="@string/actionDrawCenterText">

MPChartExample/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141

4242
<string name="actionTogglePercent">Toggle Percent</string>
4343
<string name="actionToggleHole">Toggle Hole</string>
44+
<string name="actionToggleCurvedSlices">Toggle Curved Slices</string>
4445
<string name="actionDrawCenterText">Draw Center Text</string>
4546
<string name="actionToggleHighlightCircle">Toggle Highlight Circle</string>
4647
<string name="actionToggleRotation">Toggle Rotation</string>

MPChartLib/src/main/java/com/github/mikephil/charting/charts/PieChart.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,16 @@ public void setEntryLabelTextSize(float size) {
650650
((PieChartRenderer) mRenderer).getPaintEntryLabels().setTextSize(Utils.convertDpToPixel(size));
651651
}
652652

653+
/**
654+
* Sets whether to draw slices in a curved fashion, only works if drawing the hole is enabled
655+
* and if the slices are not drawn under the hole.
656+
*
657+
* @param enabled draw curved ends of slices
658+
*/
659+
public void setDrawRoundedSlices(boolean enabled) {
660+
mDrawRoundedSlices = enabled;
661+
}
662+
653663
/**
654664
* Returns true if the chart is set to draw each end of a pie-slice
655665
* "rounded".

0 commit comments

Comments
 (0)