diff --git a/MPChartExample/src/main/java/com/xxmassdeveloper/mpchartexample/PieChartActivity.java b/MPChartExample/src/main/java/com/xxmassdeveloper/mpchartexample/PieChartActivity.java index 830025fbb1..cf59d46648 100644 --- a/MPChartExample/src/main/java/com/xxmassdeveloper/mpchartexample/PieChartActivity.java +++ b/MPChartExample/src/main/java/com/xxmassdeveloper/mpchartexample/PieChartActivity.java @@ -32,6 +32,7 @@ import com.github.mikephil.charting.highlight.Highlight; import com.github.mikephil.charting.interfaces.datasets.IDataSet; import com.github.mikephil.charting.listener.OnChartValueSelectedListener; +import com.github.mikephil.charting.renderer.PieChartRenderer; import com.github.mikephil.charting.utils.ColorTemplate; import com.github.mikephil.charting.utils.MPPointF; import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase; @@ -74,13 +75,14 @@ protected void onCreate(Bundle savedInstanceState) { chart.setCenterText(generateCenterSpannableText()); chart.setDrawHoleEnabled(true); - chart.setHoleColor(Color.WHITE); + chart.setHoleColor(Color.TRANSPARENT); - chart.setTransparentCircleColor(Color.WHITE); + chart.setTransparentCircleColor(Color.TRANSPARENT); chart.setTransparentCircleAlpha(110); - chart.setHoleRadius(58f); - chart.setTransparentCircleRadius(61f); + chart.setHoleRadius(50f); + + chart.setTransparentCircleRadius(0f); chart.setDrawCenterText(true); @@ -169,6 +171,10 @@ private void setData(int count, float range) { // undo all highlights chart.highlightValues(null); + PieChartRenderer renderer =(PieChartRenderer) chart.getRenderer(); + renderer.setRoundedCornerRadius(30f); + dataSet.setSliceSpace(renderer.getRoundedCornerRadius()/2); + chart.invalidate(); } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/PieChartRenderer.java b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/PieChartRenderer.java index f35c775d45..186bfeacbe 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/PieChartRenderer.java +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/PieChartRenderer.java @@ -45,6 +45,11 @@ public class PieChartRenderer extends DataRenderer { protected Paint mTransparentCirclePaint; protected Paint mValueLinePaint; + /** + * paint object used for drawing the rounded corner slice + */ + protected Paint mRoundedCornerPaint; + /** * paint object for the text that can be displayed in the center of the * chart @@ -68,6 +73,24 @@ public class PieChartRenderer extends DataRenderer { protected Canvas mBitmapCanvas; + /** + * Setter for the rounded corner slice paint object + */ + public void setRoundedCornerRadius(float radius){ + mRoundedCornerPaint = new Paint(Paint.ANTI_ALIAS_FLAG); + mRoundedCornerPaint.setStyle(Style.STROKE); + mRoundedCornerPaint.setAntiAlias(true); + mRoundedCornerPaint.setStrokeWidth(radius); + + } + + /** + * Getter for the rounded corner slice paint object + */ + public float getRoundedCornerRadius(){ + return mRoundedCornerPaint.getStrokeWidth(); + } + public PieChartRenderer(PieChart chart, ChartAnimator animator, ViewPortHandler viewPortHandler) { super(animator, viewPortHandler); @@ -245,6 +268,11 @@ protected void drawDataSet(Canvas c, IPieDataSet dataSet) { final float sliceSpace = visibleAngleCount <= 1 ? 0.f : getSliceSpace(dataSet); + if (getRoundedCornerRadius()>0) { + mRoundedCornerPaint.setStrokeCap(Paint.Cap.ROUND); + mRoundedCornerPaint.setStrokeJoin(Paint.Join.ROUND); + } + for (int j = 0; j < entryCount; j++) { float sliceAngle = drawAngles[j]; @@ -268,6 +296,9 @@ protected void drawDataSet(Canvas c, IPieDataSet dataSet) { mRenderPaint.setColor(dataSet.getColor(j)); + // Set current data set color to paint object + mRoundedCornerPaint.setColor(dataSet.getColor(j)); + final float sliceSpaceAngleOuter = visibleAngleCount == 1 ? 0.f : sliceSpace / (Utils.FDEG2RAD * radius); @@ -398,6 +429,11 @@ protected void drawDataSet(Canvas c, IPieDataSet dataSet) { mBitmapCanvas.drawPath(mPathBuffer, mRenderPaint); + // Draw rounded corner path with paint object slice with the given radius + if (getRoundedCornerRadius()>0) { + mBitmapCanvas.drawPath(mPathBuffer, mRoundedCornerPaint); + } + angle += sliceAngle * phaseX; }