Skip to content

Commit 621d924

Browse files
Mohini Katarahannesa2
authored andcommitted
Added Rounded corner for Pie chart
1 parent 1d2853d commit 621d924

4 files changed

Lines changed: 40 additions & 566 deletions

File tree

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

Lines changed: 5 additions & 0 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;
@@ -170,6 +171,10 @@ private void setData(int count, float range) {
170171
// undo all highlights
171172
chart.highlightValues(null);
172173

174+
PieChartRenderer renderer =(PieChartRenderer) chart.getRenderer();
175+
renderer.setRoundedCornerRadius(10f);
176+
dataSet.setSliceSpace(renderer.getRoundedCornerRadius()/2);
177+
173178
chart.invalidate();
174179
}
175180

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

Lines changed: 35 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,23 @@ 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.setStrokeWidth(radius);
82+
mRoundedCornerPaint.setStyle(Style.STROKE);
83+
mRoundedCornerPaint.setAntiAlias(true);
84+
}
85+
86+
/**
87+
* Getter for the rounded corner slice paint object
88+
*/
89+
public float getRoundedCornerRadius(){
90+
return mRoundedCornerPaint.getStrokeWidth();
91+
}
92+
7193
public PieChartRenderer(PieChart chart, ChartAnimator animator,
7294
ViewPortHandler viewPortHandler) {
7395
super(animator, viewPortHandler);
@@ -245,6 +267,11 @@ protected void drawDataSet(Canvas c, IPieDataSet dataSet) {
245267

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

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

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

269296
mRenderPaint.setColor(dataSet.getColor(j));
270297

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

399429
mBitmapCanvas.drawPath(mPathBuffer, mRenderPaint);
400430

431+
// Draw rounded corner path with paint object for the given radius
432+
if (getRoundedCornerRadius()>0) {
433+
mBitmapCanvas.drawPath(mPathBuffer, mRoundedCornerPaint);
434+
}
435+
401436
angle += sliceAngle * phaseX;
402437
}
403438

0 commit comments

Comments
 (0)