Skip to content

Commit 33e2fa4

Browse files
Mohini Katarahannesa2
authored andcommitted
Added Rounded corner for Pie chart
1 parent c666bec commit 33e2fa4

File tree

2 files changed

+46
-4
lines changed

2 files changed

+46
-4
lines changed

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import com.github.mikephil.charting.highlight.Highlight;
3333
import com.github.mikephil.charting.interfaces.datasets.IDataSet;
3434
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;
35+
import com.github.mikephil.charting.renderer.PieChartRenderer;
3536
import com.github.mikephil.charting.utils.ColorTemplate;
3637
import com.github.mikephil.charting.utils.MPPointF;
3738
import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase;
@@ -74,13 +75,14 @@ protected void onCreate(Bundle savedInstanceState) {
7475
chart.setCenterText(generateCenterSpannableText());
7576

7677
chart.setDrawHoleEnabled(true);
77-
chart.setHoleColor(Color.WHITE);
78+
chart.setHoleColor(Color.TRANSPARENT);
7879

79-
chart.setTransparentCircleColor(Color.WHITE);
80+
chart.setTransparentCircleColor(Color.TRANSPARENT);
8081
chart.setTransparentCircleAlpha(110);
8182

82-
chart.setHoleRadius(58f);
83-
chart.setTransparentCircleRadius(61f);
83+
chart.setHoleRadius(50f);
84+
85+
chart.setTransparentCircleRadius(0f);
8486

8587
chart.setDrawCenterText(true);
8688

@@ -170,6 +172,10 @@ private void setData(int count, float range) {
170172
// undo all highlights
171173
chart.highlightValues(null);
172174

175+
PieChartRenderer renderer =(PieChartRenderer) chart.getRenderer();
176+
renderer.setRoundedCornerRadius(30f);
177+
dataSet.setSliceSpace(renderer.getRoundedCornerRadius()/2);
178+
173179
chart.invalidate();
174180
}
175181

MPChartLib/src/main/java/com/github/mikephil/charting/renderer/PieChartRenderer.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ public class PieChartRenderer extends DataRenderer {
4545
protected Paint mTransparentCirclePaint;
4646
protected Paint mValueLinePaint;
4747

48+
/**
49+
* paint object used for drawing the rounded corner slice
50+
*/
51+
protected Paint mRoundedCornerPaint;
52+
4853
/**
4954
* paint object for the text that can be displayed in the center of the
5055
* chart
@@ -68,6 +73,24 @@ public class PieChartRenderer extends DataRenderer {
6873

6974
protected Canvas mBitmapCanvas;
7075

76+
/**
77+
* Setter for the rounded corner slice paint object
78+
*/
79+
public void setRoundedCornerRadius(float radius){
80+
mRoundedCornerPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
81+
mRoundedCornerPaint.setStyle(Style.STROKE);
82+
mRoundedCornerPaint.setAntiAlias(true);
83+
mRoundedCornerPaint.setStrokeWidth(radius);
84+
85+
}
86+
87+
/**
88+
* Getter for the rounded corner slice paint object
89+
*/
90+
public float getRoundedCornerRadius(){
91+
return mRoundedCornerPaint.getStrokeWidth();
92+
}
93+
7194
public PieChartRenderer(PieChart chart, ChartAnimator animator,
7295
ViewPortHandler viewPortHandler) {
7396
super(animator, viewPortHandler);
@@ -245,6 +268,11 @@ protected void drawDataSet(Canvas c, IPieDataSet dataSet) {
245268

246269
final float sliceSpace = visibleAngleCount <= 1 ? 0.f : getSliceSpace(dataSet);
247270

271+
if (getRoundedCornerRadius()>0) {
272+
mRoundedCornerPaint.setStrokeCap(Paint.Cap.ROUND);
273+
mRoundedCornerPaint.setStrokeJoin(Paint.Join.ROUND);
274+
}
275+
248276
for (int j = 0; j < entryCount; j++) {
249277

250278
float sliceAngle = drawAngles[j];
@@ -268,6 +296,9 @@ protected void drawDataSet(Canvas c, IPieDataSet dataSet) {
268296

269297
mRenderPaint.setColor(dataSet.getColor(j));
270298

299+
// Set current data set color to paint object
300+
mRoundedCornerPaint.setColor(dataSet.getColor(j));
301+
271302
final float sliceSpaceAngleOuter = visibleAngleCount == 1 ?
272303
0.f :
273304
sliceSpace / (Utils.FDEG2RAD * radius);
@@ -398,6 +429,11 @@ protected void drawDataSet(Canvas c, IPieDataSet dataSet) {
398429

399430
mBitmapCanvas.drawPath(mPathBuffer, mRenderPaint);
400431

432+
// Draw rounded corner path with paint object slice with the given radius
433+
if (getRoundedCornerRadius()>0) {
434+
mBitmapCanvas.drawPath(mPathBuffer, mRoundedCornerPaint);
435+
}
436+
401437
angle += sliceAngle * phaseX;
402438
}
403439

0 commit comments

Comments
 (0)