Skip to content

Commit 21fc088

Browse files
committed
Minor perf for pie chart
1 parent 6ee101c commit 21fc088

1 file changed

Lines changed: 24 additions & 12 deletions

File tree

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

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,9 @@ protected void drawDataSet(Canvas c, IPieDataSet dataSet) {
131131
float angle = 0;
132132
float rotationAngle = mChart.getRotationAngle();
133133

134+
float phaseX = mAnimator.getPhaseX();
135+
float phaseY = mAnimator.getPhaseY();
136+
134137
float[] drawAngles = mChart.getDrawAngles();
135138

136139
for (int j = 0; j < dataSet.getEntryCount(); j++) {
@@ -148,13 +151,13 @@ protected void drawDataSet(Canvas c, IPieDataSet dataSet) {
148151

149152
mRenderPaint.setColor(dataSet.getColor(j));
150153
mBitmapCanvas.drawArc(mChart.getCircleBox(),
151-
rotationAngle + (angle + sliceSpace / 2f) * mAnimator.getPhaseY(),
152-
(sliceAngle - sliceSpace / 2f) * mAnimator.getPhaseY(),
154+
rotationAngle + (angle + sliceSpace / 2f) * phaseY,
155+
(sliceAngle - sliceSpace / 2f) * phaseY,
153156
true, mRenderPaint);
154157
}
155158
}
156159

157-
angle += sliceAngle * mAnimator.getPhaseX();
160+
angle += sliceAngle * phaseX;
158161
}
159162
}
160163

@@ -169,6 +172,9 @@ public void drawValues(Canvas c) {
169172
float[] drawAngles = mChart.getDrawAngles();
170173
float[] absoluteAngles = mChart.getAbsoluteAngles();
171174

175+
float phaseX = mAnimator.getPhaseX();
176+
float phaseY = mAnimator.getPhaseY();
177+
172178
float off = r / 10f * 3.6f;
173179

174180
if (mChart.isDrawHoleEnabled()) {
@@ -202,14 +208,14 @@ public void drawValues(Canvas c) {
202208
int entryCount = dataSet.getEntryCount();
203209

204210
for (int j = 0, maxEntry = Math.min(
205-
(int) Math.ceil(entryCount * mAnimator.getPhaseX()), entryCount); j < maxEntry; j++) {
211+
(int) Math.ceil(entryCount * phaseX), entryCount); j < maxEntry; j++) {
206212

207213
Entry entry = dataSet.getEntryForIndex(j);
208214

209215
// offset needed to center the drawn text in the slice
210216
float offset = drawAngles[cnt] / 2;
211217

212-
float angle = (absoluteAngles[cnt] - offset) * mAnimator.getPhaseY();
218+
float angle = (absoluteAngles[cnt] - offset) * phaseY;
213219
// calculate the text position
214220
float x = (float) (r
215221
* Math.cos(Math.toRadians(rotationAngle + angle))
@@ -390,8 +396,11 @@ protected void drawCenterText(Canvas c) {
390396
@Override
391397
public void drawHighlighted(Canvas c, Highlight[] indices) {
392398

399+
float phaseX = mAnimator.getPhaseX();
400+
float phaseY = mAnimator.getPhaseY();
401+
402+
float angle;
393403
float rotationAngle = mChart.getRotationAngle();
394-
float angle = 0f;
395404

396405
float[] drawAngles = mChart.getDrawAngles();
397406
float[] absoluteAngles = mChart.getAbsoluteAngles();
@@ -413,7 +422,7 @@ public void drawHighlighted(Canvas c, Highlight[] indices) {
413422
if (xIndex == 0)
414423
angle = 0.f;
415424
else
416-
angle = absoluteAngles[xIndex - 1] * mAnimator.getPhaseX();
425+
angle = absoluteAngles[xIndex - 1] * phaseX;
417426

418427
float sliceAngle = drawAngles[xIndex];
419428
float sliceSpace = set.getSliceSpace();
@@ -437,8 +446,8 @@ public void drawHighlighted(Canvas c, Highlight[] indices) {
437446
// redefine the rect that contains the arc so that the
438447
// highlighted pie is not cut off
439448
mBitmapCanvas.drawArc(highlighted,
440-
rotationAngle + (angle + sliceSpace / 2f) * mAnimator.getPhaseY(),
441-
(sliceAngle - sliceSpace / 2f) * mAnimator.getPhaseY(),
449+
rotationAngle + (angle + sliceSpace / 2f) * phaseY,
450+
(sliceAngle - sliceSpace / 2f) * phaseY,
442451
true, mRenderPaint);
443452
}
444453
}
@@ -458,6 +467,9 @@ protected void drawRoundedSlices(Canvas c) {
458467
if (!dataSet.isVisible())
459468
return;
460469

470+
float phaseX = mAnimator.getPhaseX();
471+
float phaseY = mAnimator.getPhaseY();
472+
461473
PointF center = mChart.getCenterCircleBox();
462474
float r = mChart.getRadius();
463475

@@ -478,16 +490,16 @@ protected void drawRoundedSlices(Canvas c) {
478490

479491
float x = (float) ((r - circleRadius)
480492
* Math.cos(Math.toRadians((angle + sliceAngle)
481-
* mAnimator.getPhaseY())) + center.x);
493+
* phaseY)) + center.x);
482494
float y = (float) ((r - circleRadius)
483495
* Math.sin(Math.toRadians((angle + sliceAngle)
484-
* mAnimator.getPhaseY())) + center.y);
496+
* phaseY)) + center.y);
485497

486498
mRenderPaint.setColor(dataSet.getColor(j));
487499
mBitmapCanvas.drawCircle(x, y, circleRadius, mRenderPaint);
488500
}
489501

490-
angle += sliceAngle * mAnimator.getPhaseX();
502+
angle += sliceAngle * phaseX;
491503
}
492504
}
493505

0 commit comments

Comments
 (0)