Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pie chart rounded corners #5422

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand Down Expand Up @@ -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];
Expand All @@ -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);
Expand Down Expand Up @@ -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;
}

Expand Down