Skip to content

Commit 85691f8

Browse files
authored
Merge pull request #465 from AppDevNext/Cosmetic
Code cosmetic
2 parents 47df367 + 6cabf66 commit 85691f8

File tree

1 file changed

+46
-61
lines changed

1 file changed

+46
-61
lines changed

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

Lines changed: 46 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import android.graphics.RectF
1010
import android.text.Layout
1111
import android.text.StaticLayout
1212
import android.text.TextPaint
13+
import androidx.core.graphics.createBitmap
14+
import androidx.core.graphics.withSave
1315
import com.github.mikephil.charting.animation.ChartAnimator
1416
import com.github.mikephil.charting.charts.PieChart
1517
import com.github.mikephil.charting.data.Entry
@@ -29,8 +31,6 @@ import kotlin.math.pow
2931
import kotlin.math.sin
3032
import kotlin.math.sqrt
3133
import kotlin.math.tan
32-
import androidx.core.graphics.withSave
33-
import androidx.core.graphics.createBitmap
3434

3535
open class PieChartRenderer(
3636
protected var chart: PieChart, animator: ChartAnimator,
@@ -91,9 +91,7 @@ open class PieChartRenderer(
9191
else
9292
mDrawBitmap!!.get()
9393

94-
if (drawBitmap == null || (drawBitmap.width != width)
95-
|| (drawBitmap.height != height)
96-
) {
94+
if (drawBitmap == null || (drawBitmap.width != width) || (drawBitmap.height != height)) {
9795
if (width > 0 && height > 0) {
9896
drawBitmap = createBitmap(width, height, Bitmap.Config.ARGB_4444)
9997
mDrawBitmap = WeakReference(drawBitmap)
@@ -196,7 +194,7 @@ open class PieChartRenderer(
196194
var visibleAngleCount = 0
197195
for (j in 0..<entryCount) {
198196
// draw only if the value is greater than zero
199-
if ((abs(dataSet.getEntryForIndex(j).y.toDouble()) > Utils.FLOAT_EPSILON)) {
197+
if (abs(dataSet.getEntryForIndex(j).y.toDouble()) > Utils.FLOAT_EPSILON) {
200198
visibleAngleCount++
201199
}
202200
}
@@ -259,26 +257,23 @@ open class PieChartRenderer(
259257
mPathBuffer.arcTo(roundedCircleBox, startAngleOuter + 180, -180f)
260258
}
261259

262-
mPathBuffer.arcTo(
263-
circleBox,
264-
startAngleOuter,
265-
sweepAngleOuter
266-
)
260+
mPathBuffer.arcTo(circleBox, startAngleOuter, sweepAngleOuter)
267261
}
268262

269263
// API < 21 does not receive floats in addArc, but a RectF
270264
mInnerRectBuffer[center.x - innerRadius, center.y - innerRadius, center.x + innerRadius] = center.y + innerRadius
271265

272266
if (drawInnerArc && (innerRadius > 0f || accountForSliceSpacing)) {
273267
if (accountForSliceSpacing) {
274-
var minSpacedRadius =
275-
calculateMinimumRadiusForSpacedSlice(
276-
center, radius,
277-
sliceAngle * phaseY,
278-
arcStartPointX, arcStartPointY,
279-
startAngleOuter,
280-
sweepAngleOuter
281-
)
268+
var minSpacedRadius = calculateMinimumRadiusForSpacedSlice(
269+
center,
270+
radius,
271+
sliceAngle * phaseY,
272+
arcStartPointX,
273+
arcStartPointY,
274+
startAngleOuter,
275+
sweepAngleOuter
276+
)
282277

283278
if (minSpacedRadius < 0f) minSpacedRadius = -minSpacedRadius
284279

@@ -307,11 +302,7 @@ open class PieChartRenderer(
307302
center.y + innerRadius * sin((endAngleInner * Utils.FDEG2RAD).toDouble()).toFloat()
308303
)
309304

310-
mPathBuffer.arcTo(
311-
mInnerRectBuffer,
312-
endAngleInner,
313-
-sweepAngleInner
314-
)
305+
mPathBuffer.arcTo(mInnerRectBuffer, endAngleInner, -sweepAngleInner)
315306
}
316307
} else {
317308
if (sweepAngleOuter % 360f > Utils.FLOAT_EPSILON) {
@@ -329,10 +320,8 @@ open class PieChartRenderer(
329320
sweepAngleOuter
330321
)
331322

332-
val arcEndPointX = center.x +
333-
sliceSpaceOffset * cos((angleMiddle * Utils.FDEG2RAD).toDouble()).toFloat()
334-
val arcEndPointY = center.y +
335-
sliceSpaceOffset * sin((angleMiddle * Utils.FDEG2RAD).toDouble()).toFloat()
323+
val arcEndPointX = center.x + sliceSpaceOffset * cos((angleMiddle * Utils.FDEG2RAD).toDouble()).toFloat()
324+
val arcEndPointY = center.y + sliceSpaceOffset * sin((angleMiddle * Utils.FDEG2RAD).toDouble()).toFloat()
336325

337326
mPathBuffer.lineTo(
338327
arcEndPointX,
@@ -448,7 +437,7 @@ open class PieChartRenderer(
448437
// offset needed to center the drawn text in the slice
449438
val angleOffset = (sliceAngle - sliceSpaceMiddleAngle / 2f) / 2f
450439

451-
angle = angle + angleOffset
440+
angle += angleOffset
452441

453442
val transformedAngle = rotationAngle + angle * phaseY
454443

@@ -498,7 +487,7 @@ open class PieChartRenderer(
498487
val pt1x = labelRadius * (1 + valueLineLength1) * sliceXBase + center.x
499488
val pt1y = labelRadius * (1 + valueLineLength1) * sliceYBase + center.y
500489

501-
if (transformedAngle % 360.0 >= 90.0 && transformedAngle % 360.0 <= 270.0) {
490+
if (transformedAngle % 360.0 in 90.0..270.0) {
502491
pt2x = pt1x - polyline2Width
503492
pt2y = pt1y
504493

@@ -552,8 +541,14 @@ open class PieChartRenderer(
552541
}
553542
} else if (drawYOutside) {
554543
drawValue(
555-
this, formatter, value, entry, 0, labelPtx, labelPty + lineHeight / 2f, dataSet
556-
.getValueTextColor(j)
544+
this,
545+
formatter,
546+
value,
547+
entry,
548+
0,
549+
labelPtx,
550+
labelPty + lineHeight / 2f,
551+
dataSet.getValueTextColor(j)
557552
)
558553
}
559554
}
@@ -567,7 +562,16 @@ open class PieChartRenderer(
567562

568563
// draw everything, depending on settings
569564
if (drawXInside && drawYInside) {
570-
drawValue(this, formatter, value, entry, 0, x, y, dataSet.getValueTextColor(j))
565+
drawValue(
566+
this,
567+
formatter,
568+
value,
569+
entry,
570+
0,
571+
x,
572+
y,
573+
dataSet.getValueTextColor(j)
574+
)
571575

572576
if (j < data.entryCount && entryLabel != null) {
573577
drawEntryLabel(this, entryLabel, x, y + lineHeight)
@@ -609,40 +613,31 @@ open class PieChartRenderer(
609613

610614
/**
611615
* Draws an entry label at the specified position.
612-
*
613-
* @param canvas
614-
* @param label
615-
* @param x
616-
* @param y
617616
*/
618617
protected fun drawEntryLabel(canvas: Canvas, label: String, x: Float, y: Float) {
619618
canvas.drawText(label, x, y, paintEntryLabels)
620619
}
621620

622621
override fun drawExtras(canvas: Canvas) {
623-
drawHole(canvas)
622+
drawHole()
624623
canvas.drawBitmap(mDrawBitmap!!.get()!!, 0f, 0f, null)
625624
drawCenterText(canvas)
626625
}
627626

628627
private val mHoleCirclePath = Path()
629628

630629
/**
631-
* draws the hole in the center of the chart and the transparent circle /
632-
* hole
630+
* draws the hole in the center of the chart and the transparent circle / hole
633631
*/
634-
protected fun drawHole(canvas: Canvas?) {
632+
protected fun drawHole() {
635633
if (chart.isDrawHoleEnabled && bitmapCanvas != null) {
636634
val radius = chart.radius
637635
val holeRadius = radius * (chart.holeRadius / 100)
638636
val center = chart.centerCircleBox
639637

640638
if (Color.alpha(paintHole.color) > 0) {
641639
// draw the hole-circle
642-
bitmapCanvas!!.drawCircle(
643-
center.x, center.y,
644-
holeRadius, paintHole
645-
)
640+
bitmapCanvas!!.drawCircle(center.x, center.y, holeRadius, paintHole)
646641
}
647642

648643
// only draw the circle if it can be seen (not covered by the hole)
@@ -774,8 +769,7 @@ open class PieChartRenderer(
774769
}
775770

776771
override fun drawHighlighted(canvas: Canvas, indices: Array<Highlight>) {
777-
/* Skip entirely if using rounded circle slices, because it doesn't make sense to highlight
778-
* in this way.
772+
/* Skip entirely if using rounded circle slices, because it doesn't make sense to highlight in this way.
779773
* TODO: add support for changing slice color with highlighting rather than only shifting the slice
780774
*/
781775

@@ -866,18 +860,15 @@ open class PieChartRenderer(
866860
center.y + highlightedRadius * sin((startAngleShifted * Utils.FDEG2RAD).toDouble()).toFloat()
867861
)
868862

869-
mPathBuffer.arcTo(
870-
highlightedCircleBox,
871-
startAngleShifted,
872-
sweepAngleShifted
873-
)
863+
mPathBuffer.arcTo(highlightedCircleBox, startAngleShifted, sweepAngleShifted)
874864
}
875865

876866
var sliceSpaceRadius = 0f
877867
if (accountForSliceSpacing) {
878868
sliceSpaceRadius =
879869
calculateMinimumRadiusForSpacedSlice(
880-
center, radius,
870+
center,
871+
radius,
881872
sliceAngle * phaseY,
882873
center.x + radius * cos((startAngleOuter * Utils.FDEG2RAD).toDouble()).toFloat(),
883874
center.y + radius * sin((startAngleOuter * Utils.FDEG2RAD).toDouble()).toFloat(),
@@ -917,11 +908,7 @@ open class PieChartRenderer(
917908
center.y + innerRadius * sin((endAngleInner * Utils.FDEG2RAD).toDouble()).toFloat()
918909
)
919910

920-
mPathBuffer.arcTo(
921-
mInnerRectBuffer,
922-
endAngleInner,
923-
-sweepAngleInner
924-
)
911+
mPathBuffer.arcTo(mInnerRectBuffer, endAngleInner, -sweepAngleInner)
925912
}
926913
} else {
927914
if (sweepAngleOuter % 360f > Utils.FLOAT_EPSILON) {
@@ -956,8 +943,6 @@ open class PieChartRenderer(
956943

957944
/**
958945
* This gives all pie-slices a rounded edge.
959-
*
960-
* @param canvas
961946
*/
962947
protected fun drawRoundedSlices(canvas: Canvas?) {
963948
if (!chart.isDrawRoundedSlicesEnabled) return

0 commit comments

Comments
 (0)