Skip to content

Commit 4aabae1

Browse files
committed
Allow setting transparency amout for transparent circle / hole (#902).
1 parent 4bb9076 commit 4aabae1

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,9 @@ protected void onCreate(Bundle savedInstanceState) {
6767

6868
mChart.setDrawHoleEnabled(true);
6969
mChart.setHoleColorTransparent(true);
70-
70+
7171
mChart.setTransparentCircleColor(Color.WHITE);
72+
mChart.setTransparentCircleAlpha(110);
7273

7374
mChart.setHoleRadius(58f);
7475
mChart.setTransparentCircleRadius(61f);

MPChartLib/src/com/github/mikephil/charting/charts/PieChart.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import android.content.Context;
55
import android.graphics.Canvas;
6+
import android.graphics.Paint;
67
import android.graphics.PointF;
78
import android.graphics.PorterDuff;
89
import android.graphics.PorterDuffXfermode;
@@ -66,7 +67,7 @@ public class PieChart extends PieRadarChartBase<PieData> {
6667
/**
6768
* the radius of the transparent circle next to the chart-hole in the center
6869
*/
69-
private float mTransparentCircleRadiusPercent = 55f;
70+
protected float mTransparentCircleRadiusPercent = 55f;
7071

7172
/** if enabled, centertext is drawn */
7273
private boolean mDrawCenterText = true;
@@ -455,8 +456,16 @@ public float getHoleRadius() {
455456
return mHoleRadiusPercent;
456457
}
457458

459+
/**
460+
* Sets the color the transparent-circle should have.
461+
* @param color
462+
*/
458463
public void setTransparentCircleColor(int color) {
459-
((PieChartRenderer) mRenderer).getPaintTransparentCircle().setColor(color);
464+
465+
Paint p = ((PieChartRenderer) mRenderer).getPaintTransparentCircle();
466+
int alpha = p.getAlpha();
467+
p.setColor(color);
468+
p.setAlpha(alpha);
460469
}
461470

462471
/**
@@ -475,6 +484,17 @@ public float getTransparentCircleRadius() {
475484
return mTransparentCircleRadiusPercent;
476485
}
477486

487+
/**
488+
* Sets the amount of transparency the transparent circle should have 0 = fully transparent, 255 = fully opaque.
489+
* Default value is 100.
490+
*
491+
* @param alpha
492+
* 0-255
493+
*/
494+
public void setTransparentCircleAlpha(int alpha) {
495+
((PieChartRenderer) mRenderer).getPaintTransparentCircle().setAlpha(alpha);
496+
}
497+
478498
/**
479499
* set this to true to draw the x-value text into the pie slices
480500
*

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ public PieChartRenderer(PieChart chart, ChartAnimator animator,
6363
mTransparentCirclePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
6464
mTransparentCirclePaint.setColor(Color.WHITE);
6565
mTransparentCirclePaint.setStyle(Style.FILL);
66+
mTransparentCirclePaint.setAlpha(100);
6667

6768
mCenterTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
6869
mCenterTextPaint.setColor(Color.BLACK);
@@ -260,16 +261,16 @@ protected void drawHole(Canvas c) {
260261
if (transparentCircleRadius > holeRadius && mAnimator.getPhaseX() >= 1f
261262
&& mAnimator.getPhaseY() >= 1f) {
262263

263-
int color = mTransparentCirclePaint.getColor();
264+
//int color = mTransparentCirclePaint.getColor();
264265

265266
// make transparent
266-
mTransparentCirclePaint.setColor(color & 0x60FFFFFF);
267+
//mTransparentCirclePaint.setColor(color & 0x60FFFFFF);
267268

268269
// draw the transparent-circle
269270
mBitmapCanvas.drawCircle(center.x, center.y,
270271
radius / 100 * transparentCircleRadius, mTransparentCirclePaint);
271272

272-
mTransparentCirclePaint.setColor(color);
273+
//mTransparentCirclePaint.setColor(color);
273274
}
274275

275276
// draw the hole-circle

0 commit comments

Comments
 (0)