@@ -57,11 +57,15 @@ public class PieChart extends Chart {
5757
5858 /** indicates the selection distance of a pie slice */
5959 private float mShift = 20f ;
60-
60+
6161 /**
62- * indicates the size of the hole in the center of the piechart, default: diameter / 4
62+ * indicates the size of the hole in the center of the piechart, default:
63+ * radius / 2
6364 */
64- private float mHoleRadius = 0f ;
65+ private float mHoleRadiusPercent = 50f ;
66+
67+ /** the radius of the transparent circle next to the chart-hole in the center */
68+ private float mTransparentCircleRadius = 55f ;
6569
6670 /** if enabled, centertext is drawn */
6771 private boolean mDrawCenterText = true ;
@@ -105,9 +109,9 @@ protected void init() {
105109 super .init ();
106110
107111 // // piechart has no offsets
108- // mOffsetTop = 0;
112+ // mOffsetTop = 0;
109113 // mOffsetBottom = 0;
110- // mOffsetLeft = 0;
114+ // mOffsetLeft = 0;
111115 // mOffsetRight = 0;
112116
113117 mShift = Utils .convertDpToPixel (mShift );
@@ -283,13 +287,11 @@ public void updateRotation(float x, float y) {
283287 protected void prepareContentRect () {
284288 super .prepareContentRect ();
285289
286- if (mHoleRadius <= 0 ) mHoleRadius = getDiameter () / 4 ;
287-
288290 int width = mContentRect .width () + mOffsetLeft + mOffsetRight ;
289291 int height = mContentRect .height () + mOffsetTop + mOffsetBottom ;
290292
291293 float diameter = getDiameter ();
292-
294+
293295 // create the circle box that will contain the pie-chart (the bounds of
294296 // the pie-chart)
295297 mCircleBox .set (width / 2 - diameter / 2 + mShift , height / 2 - diameter / 2
@@ -425,9 +427,24 @@ private void drawHole() {
425427
426428 if (mDrawHole ) {
427429
430+ float radius = getRadius ();
431+
428432 PointF c = getCenterCircleBox ();
433+
434+ int color = mHolePaint .getColor ();
435+
436+ // draw the hole-circle
437+ mDrawCanvas .drawCircle (c .x , c .y ,
438+ radius / 100 * mHoleRadiusPercent , mHolePaint );
439+
440+ // make transparent
441+ mHolePaint .setColor (color & 0x60FFFFFF );
442+
443+ // draw the transparent-circle
429444 mDrawCanvas .drawCircle (c .x , c .y ,
430- mHoleRadius , mHolePaint );
445+ radius / 100 * mTransparentCircleRadius , mHolePaint );
446+
447+ mHolePaint .setColor (color );
431448 }
432449 }
433450
@@ -471,15 +488,16 @@ protected void drawValues() {
471488
472489 PointF center = getCenterCircleBox ();
473490
474- float off = mCircleBox .width () / 8 ;
491+ // get whole the radius
492+ float r = getRadius ();
493+
494+ float off = r / 2f ;
475495
476- // increase offset if there is no hole
477- if (! mDrawHole )
478- off += off / 2 ;
496+ if ( mDrawHole ) {
497+ off = ( r - ( r / 100f * mHoleRadiusPercent )) / 2f ;
498+ }
479499
480- // get the radius
481- float r = mCircleBox .width () / 2 - off ; // offset to keep things inside
482- // the chart
500+ r -= off ; // offset to keep things inside the chart
483501
484502 ArrayList <DataSet > dataSets = mCurrentData .getDataSets ();
485503
@@ -742,7 +760,7 @@ public float getRadius() {
742760 if (mCircleBox == null )
743761 return 0 ;
744762 else
745- return mCircleBox .width () / 2f ;
763+ return Math . min ( mCircleBox .width () / 2f , mCircleBox . height () / 2f ) ;
746764 }
747765
748766 /**
@@ -858,20 +876,40 @@ public void setCenterTextTypeface(Typeface t) {
858876 public void setCenterTextSize (float size ) {
859877 mCenterTextPaint .setTextSize (Utils .convertDpToPixel (size ));
860878 }
861-
879+
862880 /**
863- * sets the radius of the hole in the center of the piechart in percent of the total piechart size, default 25%
881+ * sets the radius of the hole in the center of the piechart in percent of
882+ * the maximum radius (max = the radius of the whole chart), default 50%
883+ *
864884 * @param size
865885 */
866886 public void setHoleRadius (final float percent ) {
867-
887+
868888 Handler h = new Handler ();
869889 h .post (new Runnable () {
870-
890+
891+ @ Override
892+ public void run () {
893+ mHoleRadiusPercent = percent ;
894+ }
895+ });
896+ }
897+
898+ /**
899+ * sets the radius of the transparent circle that is drawn next to the hole
900+ * in the piechart in percent of the maximum radius (max = the radius of the
901+ * whole chart), default 55% -> means 5% larger than the center-hole by
902+ * default
903+ *
904+ * @param percent
905+ */
906+ public void setTransparentCircleRadius (final float percent ) {
907+ Handler h = new Handler ();
908+ h .post (new Runnable () {
909+
871910 @ Override
872911 public void run () {
873- float smaller = Math .min (getWidth (), getHeight ());
874- mHoleRadius = smaller / 100f * percent ;
912+ mTransparentCircleRadius = percent ;
875913 }
876914 });
877915 }
0 commit comments