Skip to content

Commit e709382

Browse files
committed
Replace if with when
1 parent e83867f commit e709382

File tree

4 files changed

+179
-145
lines changed

4 files changed

+179
-145
lines changed

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

Lines changed: 54 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -95,29 +95,35 @@ open class XAxisRenderer(
9595
paintAxisLabels!!.color = xAxis.textColor
9696

9797
val pointF = MPPointF.getInstance(0f, 0f)
98-
if (xAxis.position == XAxisPosition.TOP) {
99-
pointF.x = 0.5f
100-
pointF.y = 1.0f
101-
drawLabels(c, viewPortHandler.contentTop() - yoffset, pointF)
102-
} else if (xAxis.position == XAxisPosition.TOP_INSIDE) {
103-
pointF.x = 0.5f
104-
pointF.y = 1.0f
105-
drawLabels(c, viewPortHandler.contentTop() + yoffset + xAxis.mLabelHeight, pointF)
106-
} else if (xAxis.position == XAxisPosition.BOTTOM) {
107-
pointF.x = 0.5f
108-
pointF.y = 0.0f
109-
drawLabels(c, viewPortHandler.contentBottom() + yoffset, pointF)
110-
} else if (xAxis.position == XAxisPosition.BOTTOM_INSIDE) {
111-
pointF.x = 0.5f
112-
pointF.y = 0.0f
113-
drawLabels(c, viewPortHandler.contentBottom() - yoffset - xAxis.mLabelHeight, pointF)
114-
} else { // BOTH SIDED
115-
pointF.x = 0.5f
116-
pointF.y = 1.0f
117-
drawLabels(c, viewPortHandler.contentTop() - yoffset, pointF)
118-
pointF.x = 0.5f
119-
pointF.y = 0.0f
120-
drawLabels(c, viewPortHandler.contentBottom() + yoffset, pointF)
98+
when (xAxis.position) {
99+
XAxisPosition.TOP -> {
100+
pointF.x = 0.5f
101+
pointF.y = 1.0f
102+
drawLabels(c, viewPortHandler.contentTop() - yoffset, pointF)
103+
}
104+
XAxisPosition.TOP_INSIDE -> {
105+
pointF.x = 0.5f
106+
pointF.y = 1.0f
107+
drawLabels(c, viewPortHandler.contentTop() + yoffset + xAxis.mLabelHeight, pointF)
108+
}
109+
XAxisPosition.BOTTOM -> {
110+
pointF.x = 0.5f
111+
pointF.y = 0.0f
112+
drawLabels(c, viewPortHandler.contentBottom() + yoffset, pointF)
113+
}
114+
XAxisPosition.BOTTOM_INSIDE -> {
115+
pointF.x = 0.5f
116+
pointF.y = 0.0f
117+
drawLabels(c, viewPortHandler.contentBottom() - yoffset - xAxis.mLabelHeight, pointF)
118+
}
119+
else -> { // BOTH SIDED
120+
pointF.x = 0.5f
121+
pointF.y = 1.0f
122+
drawLabels(c, viewPortHandler.contentTop() - yoffset, pointF)
123+
pointF.x = 0.5f
124+
pointF.y = 0.0f
125+
drawLabels(c, viewPortHandler.contentBottom() + yoffset, pointF)
126+
}
121127
}
122128
MPPointF.recycleInstance(pointF)
123129
}
@@ -377,26 +383,31 @@ open class XAxisRenderer(
377383

378384
val labelPosition = limitLine.labelPosition
379385

380-
if (labelPosition == LimitLabelPosition.RIGHT_TOP) {
381-
val labelLineHeight = Utils.calcTextHeight(limitLinePaint, label).toFloat()
382-
limitLinePaint.textAlign = Align.LEFT
383-
c.drawText(
384-
label, position[0] + xOffset, viewPortHandler.contentTop() + yOffset + labelLineHeight,
385-
limitLinePaint
386-
)
387-
} else if (labelPosition == LimitLabelPosition.RIGHT_BOTTOM) {
388-
limitLinePaint.textAlign = Align.LEFT
389-
c.drawText(label, position[0] + xOffset, viewPortHandler.contentBottom() - yOffset, limitLinePaint)
390-
} else if (labelPosition == LimitLabelPosition.LEFT_TOP) {
391-
limitLinePaint.textAlign = Align.RIGHT
392-
val labelLineHeight = Utils.calcTextHeight(limitLinePaint, label).toFloat()
393-
c.drawText(
394-
label, position[0] - xOffset, viewPortHandler.contentTop() + yOffset + labelLineHeight,
395-
limitLinePaint
396-
)
397-
} else {
398-
limitLinePaint.textAlign = Align.RIGHT
399-
c.drawText(label, position[0] - xOffset, viewPortHandler.contentBottom() - yOffset, limitLinePaint)
386+
when (labelPosition) {
387+
LimitLabelPosition.RIGHT_TOP -> {
388+
val labelLineHeight = Utils.calcTextHeight(limitLinePaint, label).toFloat()
389+
limitLinePaint.textAlign = Align.LEFT
390+
c.drawText(
391+
label, position[0] + xOffset, viewPortHandler.contentTop() + yOffset + labelLineHeight,
392+
limitLinePaint
393+
)
394+
}
395+
LimitLabelPosition.RIGHT_BOTTOM -> {
396+
limitLinePaint.textAlign = Align.LEFT
397+
c.drawText(label, position[0] + xOffset, viewPortHandler.contentBottom() - yOffset, limitLinePaint)
398+
}
399+
LimitLabelPosition.LEFT_TOP -> {
400+
limitLinePaint.textAlign = Align.RIGHT
401+
val labelLineHeight = Utils.calcTextHeight(limitLinePaint, label).toFloat()
402+
c.drawText(
403+
label, position[0] - xOffset, viewPortHandler.contentTop() + yOffset + labelLineHeight,
404+
limitLinePaint
405+
)
406+
}
407+
else -> {
408+
limitLinePaint.textAlign = Align.RIGHT
409+
c.drawText(label, position[0] - xOffset, viewPortHandler.contentBottom() - yOffset, limitLinePaint)
410+
}
400411
}
401412
}
402413
}

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

Lines changed: 66 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ import com.github.mikephil.charting.utils.Utils
1616
import com.github.mikephil.charting.utils.ViewPortHandler
1717
import androidx.core.graphics.withSave
1818

19-
class XAxisRendererHorizontalBarChart(
19+
@Suppress("MemberVisibilityCanBePrivate")
20+
open class XAxisRendererHorizontalBarChart(
2021
viewPortHandler: ViewPortHandler, xAxis: XAxis,
2122
trans: Transformer?
2223
) : XAxisRenderer(viewPortHandler, xAxis, trans) {
@@ -25,7 +26,7 @@ class XAxisRendererHorizontalBarChart(
2526

2627
override fun computeAxis(min: Float, max: Float, inverted: Boolean) {
2728
// calculate the starting and entry point of the y-labels (depending on
28-
// zoom / contentrect bounds)
29+
// zoom / content rect bounds)
2930

3031
var minLocal = min
3132
var maxLocal = max
@@ -74,37 +75,43 @@ class XAxisRendererHorizontalBarChart(
7475
override fun renderAxisLabels(c: Canvas) {
7576
if (!xAxis.isEnabled || !xAxis.isDrawLabelsEnabled) return
7677

77-
val xoffset = xAxis.xOffset
78+
val xOffset = xAxis.xOffset
7879

7980
paintAxisLabels!!.setTypeface(xAxis.typeface)
8081
paintAxisLabels!!.textSize = xAxis.textSize
8182
paintAxisLabels!!.color = xAxis.textColor
8283

8384
val pointF = MPPointF.getInstance(0f, 0f)
8485

85-
if (xAxis.position == XAxisPosition.TOP) {
86-
pointF.x = 0.0f
87-
pointF.y = 0.5f
88-
drawLabels(c, viewPortHandler.contentRight() + xoffset, pointF)
89-
} else if (xAxis.position == XAxisPosition.TOP_INSIDE) {
90-
pointF.x = 1.0f
91-
pointF.y = 0.5f
92-
drawLabels(c, viewPortHandler.contentRight() - xoffset, pointF)
93-
} else if (xAxis.position == XAxisPosition.BOTTOM) {
94-
pointF.x = 1.0f
95-
pointF.y = 0.5f
96-
drawLabels(c, viewPortHandler.contentLeft() - xoffset, pointF)
97-
} else if (xAxis.position == XAxisPosition.BOTTOM_INSIDE) {
98-
pointF.x = 1.0f
99-
pointF.y = 0.5f
100-
drawLabels(c, viewPortHandler.contentLeft() + xoffset, pointF)
101-
} else { // BOTH SIDED
102-
pointF.x = 0.0f
103-
pointF.y = 0.5f
104-
drawLabels(c, viewPortHandler.contentRight() + xoffset, pointF)
105-
pointF.x = 1.0f
106-
pointF.y = 0.5f
107-
drawLabels(c, viewPortHandler.contentLeft() - xoffset, pointF)
86+
when (xAxis.position) {
87+
XAxisPosition.TOP -> {
88+
pointF.x = 0.0f
89+
pointF.y = 0.5f
90+
drawLabels(c, viewPortHandler.contentRight() + xOffset, pointF)
91+
}
92+
XAxisPosition.TOP_INSIDE -> {
93+
pointF.x = 1.0f
94+
pointF.y = 0.5f
95+
drawLabels(c, viewPortHandler.contentRight() - xOffset, pointF)
96+
}
97+
XAxisPosition.BOTTOM -> {
98+
pointF.x = 1.0f
99+
pointF.y = 0.5f
100+
drawLabels(c, viewPortHandler.contentLeft() - xOffset, pointF)
101+
}
102+
XAxisPosition.BOTTOM_INSIDE -> {
103+
pointF.x = 1.0f
104+
pointF.y = 0.5f
105+
drawLabels(c, viewPortHandler.contentLeft() + xOffset, pointF)
106+
}
107+
else -> { // BOTH SIDED
108+
pointF.x = 0.0f
109+
pointF.y = 0.5f
110+
drawLabels(c, viewPortHandler.contentRight() + xOffset, pointF)
111+
pointF.x = 1.0f
112+
pointF.y = 0.5f
113+
drawLabels(c, viewPortHandler.contentLeft() - xOffset, pointF)
114+
}
108115
}
109116

110117
MPPointF.recycleInstance(pointF)
@@ -243,34 +250,39 @@ class XAxisRendererHorizontalBarChart(
243250

244251
val position = l.labelPosition
245252

246-
if (position == LimitLabelPosition.RIGHT_TOP) {
247-
limitLinePaint!!.textAlign = Align.RIGHT
248-
c.drawText(
249-
label,
250-
viewPortHandler.contentRight() - xOffset,
251-
pts[1] - yOffset + labelLineHeight, limitLinePaint!!
252-
)
253-
} else if (position == LimitLabelPosition.RIGHT_BOTTOM) {
254-
limitLinePaint!!.textAlign = Align.RIGHT
255-
c.drawText(
256-
label,
257-
viewPortHandler.contentRight() - xOffset,
258-
pts[1] + yOffset, limitLinePaint!!
259-
)
260-
} else if (position == LimitLabelPosition.LEFT_TOP) {
261-
limitLinePaint!!.textAlign = Align.LEFT
262-
c.drawText(
263-
label,
264-
viewPortHandler.contentLeft() + xOffset,
265-
pts[1] - yOffset + labelLineHeight, limitLinePaint!!
266-
)
267-
} else {
268-
limitLinePaint!!.textAlign = Align.LEFT
269-
c.drawText(
270-
label,
271-
viewPortHandler.offsetLeft() + xOffset,
272-
pts[1] + yOffset, limitLinePaint!!
273-
)
253+
when (position) {
254+
LimitLabelPosition.RIGHT_TOP -> {
255+
limitLinePaint!!.textAlign = Align.RIGHT
256+
c.drawText(
257+
label,
258+
viewPortHandler.contentRight() - xOffset,
259+
pts[1] - yOffset + labelLineHeight, limitLinePaint!!
260+
)
261+
}
262+
LimitLabelPosition.RIGHT_BOTTOM -> {
263+
limitLinePaint!!.textAlign = Align.RIGHT
264+
c.drawText(
265+
label,
266+
viewPortHandler.contentRight() - xOffset,
267+
pts[1] + yOffset, limitLinePaint!!
268+
)
269+
}
270+
LimitLabelPosition.LEFT_TOP -> {
271+
limitLinePaint!!.textAlign = Align.LEFT
272+
c.drawText(
273+
label,
274+
viewPortHandler.contentLeft() + xOffset,
275+
pts[1] - yOffset + labelLineHeight, limitLinePaint!!
276+
)
277+
}
278+
else -> {
279+
limitLinePaint!!.textAlign = Align.LEFT
280+
c.drawText(
281+
label,
282+
viewPortHandler.offsetLeft() + xOffset,
283+
pts[1] + yOffset, limitLinePaint!!
284+
)
285+
}
274286
}
275287
}
276288

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

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -326,41 +326,46 @@ open class YAxisRenderer(viewPortHandler: ViewPortHandler, @JvmField protected v
326326

327327
val position = l.labelPosition
328328

329-
if (position == LimitLabelPosition.RIGHT_TOP) {
330-
limitLinePaint.textAlign = Align.RIGHT
331-
limitLinePaint.let {
332-
c.drawText(
333-
label,
334-
viewPortHandler.contentRight() - xOffset,
335-
pts[1] - yOffset + labelLineHeight, it
336-
)
329+
when (position) {
330+
LimitLabelPosition.RIGHT_TOP -> {
331+
limitLinePaint.textAlign = Align.RIGHT
332+
limitLinePaint.let {
333+
c.drawText(
334+
label,
335+
viewPortHandler.contentRight() - xOffset,
336+
pts[1] - yOffset + labelLineHeight, it
337+
)
338+
}
337339
}
338-
} else if (position == LimitLabelPosition.RIGHT_BOTTOM) {
339-
limitLinePaint.let {
340-
it.textAlign = Align.RIGHT
341-
c.drawText(
342-
label,
343-
viewPortHandler.contentRight() - xOffset,
344-
pts[1] + yOffset, it
345-
)
340+
LimitLabelPosition.RIGHT_BOTTOM -> {
341+
limitLinePaint.let {
342+
it.textAlign = Align.RIGHT
343+
c.drawText(
344+
label,
345+
viewPortHandler.contentRight() - xOffset,
346+
pts[1] + yOffset, it
347+
)
348+
}
346349
}
347-
} else if (position == LimitLabelPosition.LEFT_TOP) {
348-
limitLinePaint.let {
349-
it.textAlign = Align.LEFT
350-
c.drawText(
351-
label,
352-
viewPortHandler.contentLeft() + xOffset,
353-
pts[1] - yOffset + labelLineHeight, it
354-
)
350+
LimitLabelPosition.LEFT_TOP -> {
351+
limitLinePaint.let {
352+
it.textAlign = Align.LEFT
353+
c.drawText(
354+
label,
355+
viewPortHandler.contentLeft() + xOffset,
356+
pts[1] - yOffset + labelLineHeight, it
357+
)
358+
}
355359
}
356-
} else {
357-
limitLinePaint.let {
358-
it.textAlign = Align.LEFT
359-
c.drawText(
360-
label,
361-
viewPortHandler.offsetLeft() + xOffset,
362-
pts[1] + yOffset, it
363-
)
360+
else -> {
361+
limitLinePaint.let {
362+
it.textAlign = Align.LEFT
363+
c.drawText(
364+
label,
365+
viewPortHandler.offsetLeft() + xOffset,
366+
pts[1] + yOffset, it
367+
)
368+
}
364369
}
365370
}
366371
}

0 commit comments

Comments
 (0)