Skip to content

Commit 099c687

Browse files
committed
Kotlin calcTextWidth
1 parent 00757c0 commit 099c687

11 files changed

Lines changed: 69 additions & 135 deletions

File tree

MPChartLib/src/main/java/com/github/mikephil/charting/components/Legend.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import com.github.mikephil.charting.utils.ColorTemplate
66
import com.github.mikephil.charting.utils.FSize
77
import com.github.mikephil.charting.utils.Utils
88
import com.github.mikephil.charting.utils.ViewPortHandler
9+
import com.github.mikephil.charting.utils.calcTextWidth
910
import com.github.mikephil.charting.utils.convertDpToPixel
1011
import java.lang.Float
1112
import kotlin.Array
@@ -284,10 +285,9 @@ class Legend() : ComponentBase() {
284285
entry.formSize).convertDpToPixel()
285286
if (formSize > maxFormSize) maxFormSize = formSize
286287

287-
val label = entry.label
288-
if (label == null) continue
288+
val label = entry.label ?: continue
289289

290-
val length = Utils.calcTextWidth(p, label).toFloat()
290+
val length = p.calcTextWidth(label).toFloat()
291291

292292
if (length > max) max = length
293293
}
@@ -491,7 +491,7 @@ class Legend() : ComponentBase() {
491491
wasStacked = false
492492
}
493493

494-
width += Utils.calcTextWidth(labelpaint, label).toFloat()
494+
width += labelpaint.calcTextWidth(label).toFloat()
495495

496496
maxHeight += labelLineHeight + yEntrySpace
497497
} else {

MPChartLib/src/main/java/com/github/mikephil/charting/components/YAxis.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.github.mikephil.charting.components
33
import android.graphics.Color
44
import android.graphics.Paint
55
import com.github.mikephil.charting.utils.Utils
6+
import com.github.mikephil.charting.utils.calcTextWidth
67
import com.github.mikephil.charting.utils.convertDpToPixel
78
import kotlin.math.abs
89
import kotlin.math.max
@@ -169,7 +170,7 @@ class YAxis : AxisBase {
169170
* use Inifinity for disabling the maximum
170171
* default: Float.POSITIVE_INFINITY (no maximum specified)
171172
*/
172-
var maxWidth: Float = Float.Companion.POSITIVE_INFINITY
173+
var maxWidth: Float = Float.POSITIVE_INFINITY
173174

174175
/**
175176
* Enum that specifies the axis a DataSet should be plotted against, either LEFT or RIGHT.
@@ -238,17 +239,17 @@ class YAxis : AxisBase {
238239
* This is for normal (not horizontal) charts horizontal spacing.
239240
*/
240241
fun getRequiredWidthSpace(p: Paint): Float {
241-
p.setTextSize(mTextSize)
242+
p.textSize = mTextSize
242243

243244
val label = getLongestLabel(p)
244-
var width = Utils.calcTextWidth(p, label).toFloat() + xOffset * 2f
245+
var width = p.calcTextWidth(label).toFloat() + xOffset * 2f
245246

246247
var minWidth = this.minWidth
247248
var maxWidth = this.maxWidth
248249

249250
if (minWidth > 0f) minWidth = minWidth.convertDpToPixel()
250251

251-
if (maxWidth > 0f && maxWidth != Float.Companion.POSITIVE_INFINITY) maxWidth = maxWidth.convertDpToPixel()
252+
if (maxWidth > 0f && maxWidth != Float.POSITIVE_INFINITY) maxWidth = maxWidth.convertDpToPixel()
252253

253254
width = max(minWidth, min(width, if (maxWidth > 0.0) maxWidth else width))
254255

@@ -259,7 +260,7 @@ class YAxis : AxisBase {
259260
* This is for HorizontalBarChart vertical spacing.
260261
*/
261262
fun getRequiredHeightSpace(p: Paint): Float {
262-
p.setTextSize(mTextSize)
263+
p.textSize = mTextSize
263264

264265
val label = getLongestLabel(p)
265266
return Utils.calcTextHeight(p, label).toFloat() + yOffset * 2f
@@ -269,12 +270,11 @@ class YAxis : AxisBase {
269270
* Returns true if this axis needs horizontal offset, false if no offset is needed.
270271
*/
271272
fun needsOffset(): Boolean {
272-
if (isEnabled && isDrawLabelsEnabled && this.labelPosition == YAxisLabelPosition.OUTSIDE_CHART) return true
273-
else return false
273+
return isEnabled && isDrawLabelsEnabled && this.labelPosition == YAxisLabelPosition.OUTSIDE_CHART
274274
}
275275

276276

277-
public override fun calculate(dataMin: Float, dataMax: Float) {
277+
override fun calculate(dataMin: Float, dataMax: Float) {
278278
var min = dataMin
279279
var max = dataMax
280280

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import com.github.mikephil.charting.utils.MPPointF
1414
import com.github.mikephil.charting.utils.Transformer
1515
import com.github.mikephil.charting.utils.Utils
1616
import com.github.mikephil.charting.utils.ViewPortHandler
17+
import com.github.mikephil.charting.utils.calcTextWidth
1718
import com.github.mikephil.charting.utils.convertDpToPixel
1819
import kotlin.math.ceil
1920
import kotlin.math.min
@@ -225,7 +226,7 @@ open class HorizontalBarChartRenderer(
225226
val valueY = barEntry.y
226227
val formattedValue = formatter.getFormattedValue(valueY, barEntry, i, viewPortHandler)
227228
// calculate the correct offset depending on the draw position of the value
228-
val valueTextWidth = Utils.calcTextWidth(paintValues, formattedValue).toFloat()
229+
val valueTextWidth = paintValues.calcTextWidth(formattedValue).toFloat()
229230
posOffset = (if (drawValueAboveBar) valueOffsetPlus else -(valueTextWidth + valueOffsetPlus))
230231
negOffset = ((if (drawValueAboveBar) -(valueTextWidth + valueOffsetPlus) else valueOffsetPlus)
231232
- (buffer.buffer[j + 2] - buffer.buffer[j]))
@@ -302,7 +303,7 @@ open class HorizontalBarChartRenderer(
302303
)
303304

304305
// calculate the correct offset depending on the draw position of the value
305-
val valueTextWidth = Utils.calcTextWidth(paintValues, formattedValue).toFloat()
306+
val valueTextWidth = paintValues.calcTextWidth(formattedValue).toFloat()
306307
posOffset = (if (drawValueAboveBar) valueOffsetPlus else -(valueTextWidth + valueOffsetPlus))
307308
negOffset = (if (drawValueAboveBar) -(valueTextWidth + valueOffsetPlus) else valueOffsetPlus)
308309

@@ -380,7 +381,7 @@ open class HorizontalBarChartRenderer(
380381
)
381382

382383
// calculate the correct offset depending on the draw position of the value
383-
val valueTextWidth = Utils.calcTextWidth(paintValues, formattedValue).toFloat()
384+
val valueTextWidth = paintValues.calcTextWidth(formattedValue).toFloat()
384385
posOffset = (if (drawValueAboveBar) valueOffsetPlus else -(valueTextWidth + valueOffsetPlus))
385386
negOffset = (if (drawValueAboveBar) -(valueTextWidth + valueOffsetPlus) else valueOffsetPlus)
386387

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import com.github.mikephil.charting.interfaces.datasets.IPieDataSet
2020
import com.github.mikephil.charting.utils.ColorTemplate
2121
import com.github.mikephil.charting.utils.Utils
2222
import com.github.mikephil.charting.utils.ViewPortHandler
23+
import com.github.mikephil.charting.utils.calcTextWidth
2324
import com.github.mikephil.charting.utils.convertDpToPixel
2425
import java.util.Collections
2526
import kotlin.math.min
@@ -396,7 +397,7 @@ open class LegendRenderer(
396397
-formToTextSpace
397398
else if (wasStacked) posX = originPosX
398399

399-
if (direction == LegendDirection.RIGHT_TO_LEFT) posX -= Utils.calcTextWidth(labelPaint, e.label).toFloat()
400+
if (direction == LegendDirection.RIGHT_TO_LEFT) posX -= labelPaint.calcTextWidth(e.label).toFloat()
400401

401402
if (e.label != null)
402403
if (!wasStacked) {

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

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import com.github.mikephil.charting.utils.MPPointF
1414
import com.github.mikephil.charting.utils.Utils
1515
import com.github.mikephil.charting.utils.ViewPortHandler
1616
import com.github.mikephil.charting.utils.convertDpToPixel
17+
import com.github.mikephil.charting.utils.getPosition
1718

1819
open class RadarChartRenderer(
1920
protected var chart: RadarChart, animator: ChartAnimator,
@@ -60,7 +61,7 @@ open class RadarChartRenderer(
6061
val factor = chart.factor
6162

6263
val center = chart.centerOffsets
63-
val pOut = MPPointF.getInstance(0f, 0f)
64+
var pOut = MPPointF.getInstance(0f, 0f)
6465
val surface = drawDataSetSurfacePathBuffer
6566
surface.reset()
6667

@@ -71,10 +72,9 @@ open class RadarChartRenderer(
7172

7273
dataSet.getEntryForIndex(j)?.let { e ->
7374

74-
Utils.getPosition(
75-
center,
75+
pOut = center.getPosition(
7676
(e.y - chart.yChartMin) * factor * phaseY,
77-
sliceAngle * j * phaseX + chart.rotationAngle, pOut
77+
sliceAngle * j * phaseX + chart.rotationAngle
7878
)
7979
}
8080
if (java.lang.Float.isNaN(pOut.x)) continue
@@ -122,8 +122,8 @@ open class RadarChartRenderer(
122122
val factor = chart.factor
123123

124124
val center = chart.centerOffsets
125-
val pOut = MPPointF.getInstance(0f, 0f)
126-
val pIcon = MPPointF.getInstance(0f, 0f)
125+
var pOut = MPPointF.getInstance(0f, 0f)
126+
var pIcon = MPPointF.getInstance(0f, 0f)
127127

128128
val yOffset = 5f.convertDpToPixel()
129129

@@ -146,12 +146,10 @@ open class RadarChartRenderer(
146146
for (j in 0..<dataSet.entryCount) {
147147
dataSet.getEntryForIndex(j)?.let { entry ->
148148

149-
Utils.getPosition(
150-
center,
149+
pOut = center.getPosition(
151150
(entry.y - chart.yChartMin) * factor * phaseY,
152-
sliceAngle * j * phaseX + chart.rotationAngle,
153-
pOut
154-
)
151+
sliceAngle * j * phaseX + chart.rotationAngle
152+
)
155153

156154
if (dataSet.isDrawValues) {
157155
drawValue(
@@ -169,12 +167,10 @@ open class RadarChartRenderer(
169167
if (entry.icon != null && dataSet.isDrawIcons) {
170168
val icon = entry.icon
171169

172-
Utils.getPosition(
173-
center,
170+
pIcon = center.getPosition(
174171
(entry.y) * factor * phaseY + iconsOffset.y,
175-
sliceAngle * j * phaseX + chart.rotationAngle,
176-
pIcon
177-
)
172+
sliceAngle * j * phaseX + chart.rotationAngle
173+
)
178174

179175
pIcon.y += iconsOffset.x
180176

@@ -220,14 +216,12 @@ open class RadarChartRenderer(
220216
val xIncrements = 1 + chart.skipWebLineCount
221217
val maxEntryCount = chart.data!!.maxEntryCountSet.entryCount
222218

223-
val p = MPPointF.getInstance(0f, 0f)
219+
var p = MPPointF.getInstance(0f, 0f)
224220
var i = 0
225221
while (i < maxEntryCount) {
226-
Utils.getPosition(
227-
center,
222+
p = center.getPosition(
228223
chart.yRange * factor,
229-
sliceAngle * i + rotationAngle,
230-
p
224+
sliceAngle * i + rotationAngle
231225
)
232226

233227
canvas.drawLine(center.x, center.y, p.x, p.y, webPaint)
@@ -242,8 +236,8 @@ open class RadarChartRenderer(
242236

243237
val labelCount = chart.yAxis.mEntryCount
244238

245-
val p1out = MPPointF.getInstance(0f, 0f)
246-
val p2out = MPPointF.getInstance(0f, 0f)
239+
var p1out = MPPointF.getInstance(0f, 0f)
240+
var p2out = MPPointF.getInstance(0f, 0f)
247241
for (j in 0..<labelCount) {
248242
if (chart.isCustomLayerColorEnable) {
249243
innerAreaPath.rewind()
@@ -252,8 +246,8 @@ open class RadarChartRenderer(
252246
for (i in 0..<chart.data!!.entryCount) {
253247
val r = (chart.yAxis.mEntries[j] - chart.yChartMin) * factor
254248

255-
Utils.getPosition(center, r, sliceAngle * i + rotationAngle, p1out)
256-
Utils.getPosition(center, r, sliceAngle * (i + 1) + rotationAngle, p2out)
249+
p1out = center.getPosition(r, sliceAngle * i + rotationAngle)
250+
p2out = center.getPosition(r, sliceAngle * (i + 1) + rotationAngle)
257251

258252
canvas.drawLine(p1out.x, p1out.y, p2out.x, p2out.y, webPaint)
259253
if (chart.isCustomLayerColorEnable) {
@@ -290,7 +284,7 @@ open class RadarChartRenderer(
290284
val factor = chart.factor
291285

292286
val center = chart.centerOffsets
293-
val pOut = MPPointF.getInstance(0f, 0f)
287+
var pOut = MPPointF.getInstance(0f, 0f)
294288

295289
val radarData = chart.data
296290

@@ -307,11 +301,9 @@ open class RadarChartRenderer(
307301

308302
val y = (radarEntry.y - chart.yChartMin)
309303

310-
Utils.getPosition(
311-
center,
304+
pOut = center.getPosition(
312305
y * factor * animator.phaseY,
313-
sliceAngle * high.x * animator.phaseX + chart.rotationAngle,
314-
pOut
306+
sliceAngle * high.x * animator.phaseX + chart.rotationAngle
315307
)
316308
}
317309
high.setDraw(pOut.x, pOut.y)

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ import com.github.mikephil.charting.utils.MPPointF
1818
import com.github.mikephil.charting.utils.Transformer
1919
import com.github.mikephil.charting.utils.Utils
2020
import com.github.mikephil.charting.utils.ViewPortHandler
21+
import com.github.mikephil.charting.utils.calcTextWidth
2122
import com.github.mikephil.charting.utils.convertDpToPixel
23+
import com.github.mikephil.charting.utils.drawXAxisValue
2224
import kotlin.math.roundToInt
2325

2426
open class XAxisRenderer(
@@ -205,15 +207,15 @@ open class XAxisRenderer(
205207
// avoid clipping of the last
206208

207209
if (i / 2 == xAxis.mEntryCount - 1 && xAxis.mEntryCount > 1) {
208-
val width = Utils.calcTextWidth(paintAxisLabels, label).toFloat()
210+
val width = paintAxisLabels.calcTextWidth(label).toFloat()
209211

210212
if (width > viewPortHandler.offsetRight() * 2
211213
&& x + width > viewPortHandler.chartWidth
212214
) x -= width / 2
213215

214216
// avoid clipping of the first
215217
} else if (i == 0) {
216-
val width = Utils.calcTextWidth(paintAxisLabels, label).toFloat()
218+
val width = paintAxisLabels.calcTextWidth(label).toFloat()
217219
x += width / 2
218220
}
219221
}
@@ -225,7 +227,7 @@ open class XAxisRenderer(
225227
}
226228

227229
protected fun drawLabel(canvas: Canvas, formattedLabel: String?, x: Float, y: Float, anchor: MPPointF, angleDegrees: Float) {
228-
Utils.drawXAxisValue(canvas, formattedLabel, x, y, paintAxisLabels, anchor, angleDegrees)
230+
canvas.drawXAxisValue(formattedLabel, x, y, paintAxisLabels, anchor, angleDegrees)
229231
}
230232

231233
protected open var mRenderGridLinesPath: Path = Path()

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import android.graphics.Canvas
44
import com.github.mikephil.charting.charts.RadarChart
55
import com.github.mikephil.charting.components.XAxis
66
import com.github.mikephil.charting.utils.MPPointF
7-
import com.github.mikephil.charting.utils.Utils
87
import com.github.mikephil.charting.utils.ViewPortHandler
8+
import com.github.mikephil.charting.utils.getPosition
99

1010
class XAxisRendererRadarChart(viewPortHandler: ViewPortHandler, xAxis: XAxis, private val chart: RadarChart) : XAxisRenderer(viewPortHandler, xAxis, null) {
1111
override fun renderAxisLabels(canvas: Canvas) {
@@ -26,14 +26,14 @@ class XAxisRendererRadarChart(viewPortHandler: ViewPortHandler, xAxis: XAxis, pr
2626
val factor = chart.factor
2727

2828
val center = chart.centerOffsets
29-
val pOut = MPPointF.getInstance(0f, 0f)
29+
var pOut = MPPointF.getInstance(0f, 0f)
3030
for (i in 0..<chart.data!!.maxEntryCountSet.entryCount) {
3131
val label = xAxis.valueFormatter?.getFormattedValue(i.toFloat(), xAxis)
3232

3333
val angle = (sliceAngle * i + chart.rotationAngle) % 360f
3434

35-
Utils.getPosition(
36-
center, chart.yRange * factor + xAxis.mLabelWidth / 2f, angle, pOut
35+
pOut = center.getPosition(
36+
chart.yRange * factor + xAxis.mLabelWidth / 2f, angle
3737
)
3838

3939
drawLabel(

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import android.graphics.Path
55
import com.github.mikephil.charting.charts.RadarChart
66
import com.github.mikephil.charting.components.YAxis
77
import com.github.mikephil.charting.utils.MPPointF
8-
import com.github.mikephil.charting.utils.Utils
98
import com.github.mikephil.charting.utils.ViewPortHandler
9+
import com.github.mikephil.charting.utils.getPosition
1010
import com.github.mikephil.charting.utils.roundToNextSignificant
1111
import kotlin.math.abs
1212
import kotlin.math.ceil
@@ -144,7 +144,7 @@ class YAxisRendererRadarChart(viewPortHandler: ViewPortHandler, yAxis: YAxis, pr
144144
paintAxisLabels.color = yAxis.textColor
145145

146146
val center = chart.centerOffsets
147-
val pOut = MPPointF.getInstance(0f, 0f)
147+
var pOut = MPPointF.getInstance(0f, 0f)
148148
val factor = chart.factor
149149

150150
val from = if (yAxis.isDrawBottomYLabelEntryEnabled) 0 else 1
@@ -158,7 +158,7 @@ class YAxisRendererRadarChart(viewPortHandler: ViewPortHandler, yAxis: YAxis, pr
158158
for (j in from..<to) {
159159
val r = (yAxis.mEntries[j] - yAxis.mAxisMinimum) * factor
160160

161-
Utils.getPosition(center, r, chart.rotationAngle, pOut)
161+
pOut = center.getPosition(r, chart.rotationAngle)
162162

163163
val label = yAxis.getFormattedLabel(j)
164164

@@ -178,7 +178,7 @@ class YAxisRendererRadarChart(viewPortHandler: ViewPortHandler, yAxis: YAxis, pr
178178
val factor = chart.factor
179179

180180
val center = chart.centerOffsets
181-
val pOut = MPPointF.getInstance(0f, 0f)
181+
var pOut = MPPointF.getInstance(0f, 0f)
182182
for (i in limitLines.indices) {
183183
val limitLine = limitLines[i]
184184

@@ -195,7 +195,7 @@ class YAxisRendererRadarChart(viewPortHandler: ViewPortHandler, yAxis: YAxis, pr
195195

196196

197197
for (j in 0..<chart.data!!.maxEntryCountSet.entryCount) {
198-
Utils.getPosition(center, r, sliceAngle * j + chart.rotationAngle, pOut)
198+
pOut = center.getPosition(r, sliceAngle * j + chart.rotationAngle)
199199

200200
if (j == 0) limitPath.moveTo(pOut.x, pOut.y)
201201
else limitPath.lineTo(pOut.x, pOut.y)

0 commit comments

Comments
 (0)