From 099c6873fd549d71bfa910658809636e83db415e Mon Sep 17 00:00:00 2001 From: Hannes Achleitner Date: Tue, 16 Dec 2025 19:38:51 +0100 Subject: [PATCH 1/2] Kotlin calcTextWidth --- .../mikephil/charting/components/Legend.kt | 8 +- .../mikephil/charting/components/YAxis.kt | 16 ++-- .../renderer/HorizontalBarChartRenderer.kt | 7 +- .../charting/renderer/LegendRenderer.kt | 3 +- .../charting/renderer/RadarChartRenderer.kt | 52 ++++++------- .../charting/renderer/XAxisRenderer.kt | 8 +- .../renderer/XAxisRendererRadarChart.kt | 8 +- .../renderer/YAxisRendererRadarChart.kt | 10 +-- .../mikephil/charting/utils/CanvasUtils.kt | 5 ++ .../github/mikephil/charting/utils/Utils.java | 77 ------------------- .../github/mikephil/charting/utils/UtilsKt.kt | 10 +++ 11 files changed, 69 insertions(+), 135 deletions(-) diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/components/Legend.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/components/Legend.kt index a8596b1326..e3f4ad7726 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/components/Legend.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/components/Legend.kt @@ -6,6 +6,7 @@ import com.github.mikephil.charting.utils.ColorTemplate import com.github.mikephil.charting.utils.FSize import com.github.mikephil.charting.utils.Utils import com.github.mikephil.charting.utils.ViewPortHandler +import com.github.mikephil.charting.utils.calcTextWidth import com.github.mikephil.charting.utils.convertDpToPixel import java.lang.Float import kotlin.Array @@ -284,10 +285,9 @@ class Legend() : ComponentBase() { entry.formSize).convertDpToPixel() if (formSize > maxFormSize) maxFormSize = formSize - val label = entry.label - if (label == null) continue + val label = entry.label ?: continue - val length = Utils.calcTextWidth(p, label).toFloat() + val length = p.calcTextWidth(label).toFloat() if (length > max) max = length } @@ -491,7 +491,7 @@ class Legend() : ComponentBase() { wasStacked = false } - width += Utils.calcTextWidth(labelpaint, label).toFloat() + width += labelpaint.calcTextWidth(label).toFloat() maxHeight += labelLineHeight + yEntrySpace } else { diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/components/YAxis.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/components/YAxis.kt index f470c43a42..7f140e4315 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/components/YAxis.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/components/YAxis.kt @@ -3,6 +3,7 @@ package com.github.mikephil.charting.components import android.graphics.Color import android.graphics.Paint import com.github.mikephil.charting.utils.Utils +import com.github.mikephil.charting.utils.calcTextWidth import com.github.mikephil.charting.utils.convertDpToPixel import kotlin.math.abs import kotlin.math.max @@ -169,7 +170,7 @@ class YAxis : AxisBase { * use Inifinity for disabling the maximum * default: Float.POSITIVE_INFINITY (no maximum specified) */ - var maxWidth: Float = Float.Companion.POSITIVE_INFINITY + var maxWidth: Float = Float.POSITIVE_INFINITY /** * Enum that specifies the axis a DataSet should be plotted against, either LEFT or RIGHT. @@ -238,17 +239,17 @@ class YAxis : AxisBase { * This is for normal (not horizontal) charts horizontal spacing. */ fun getRequiredWidthSpace(p: Paint): Float { - p.setTextSize(mTextSize) + p.textSize = mTextSize val label = getLongestLabel(p) - var width = Utils.calcTextWidth(p, label).toFloat() + xOffset * 2f + var width = p.calcTextWidth(label).toFloat() + xOffset * 2f var minWidth = this.minWidth var maxWidth = this.maxWidth if (minWidth > 0f) minWidth = minWidth.convertDpToPixel() - if (maxWidth > 0f && maxWidth != Float.Companion.POSITIVE_INFINITY) maxWidth = maxWidth.convertDpToPixel() + if (maxWidth > 0f && maxWidth != Float.POSITIVE_INFINITY) maxWidth = maxWidth.convertDpToPixel() width = max(minWidth, min(width, if (maxWidth > 0.0) maxWidth else width)) @@ -259,7 +260,7 @@ class YAxis : AxisBase { * This is for HorizontalBarChart vertical spacing. */ fun getRequiredHeightSpace(p: Paint): Float { - p.setTextSize(mTextSize) + p.textSize = mTextSize val label = getLongestLabel(p) return Utils.calcTextHeight(p, label).toFloat() + yOffset * 2f @@ -269,12 +270,11 @@ class YAxis : AxisBase { * Returns true if this axis needs horizontal offset, false if no offset is needed. */ fun needsOffset(): Boolean { - if (isEnabled && isDrawLabelsEnabled && this.labelPosition == YAxisLabelPosition.OUTSIDE_CHART) return true - else return false + return isEnabled && isDrawLabelsEnabled && this.labelPosition == YAxisLabelPosition.OUTSIDE_CHART } - public override fun calculate(dataMin: Float, dataMax: Float) { + override fun calculate(dataMin: Float, dataMax: Float) { var min = dataMin var max = dataMax diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/HorizontalBarChartRenderer.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/HorizontalBarChartRenderer.kt index 0948c4b125..2e78311ad5 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/HorizontalBarChartRenderer.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/HorizontalBarChartRenderer.kt @@ -14,6 +14,7 @@ import com.github.mikephil.charting.utils.MPPointF import com.github.mikephil.charting.utils.Transformer import com.github.mikephil.charting.utils.Utils import com.github.mikephil.charting.utils.ViewPortHandler +import com.github.mikephil.charting.utils.calcTextWidth import com.github.mikephil.charting.utils.convertDpToPixel import kotlin.math.ceil import kotlin.math.min @@ -225,7 +226,7 @@ open class HorizontalBarChartRenderer( val valueY = barEntry.y val formattedValue = formatter.getFormattedValue(valueY, barEntry, i, viewPortHandler) // calculate the correct offset depending on the draw position of the value - val valueTextWidth = Utils.calcTextWidth(paintValues, formattedValue).toFloat() + val valueTextWidth = paintValues.calcTextWidth(formattedValue).toFloat() posOffset = (if (drawValueAboveBar) valueOffsetPlus else -(valueTextWidth + valueOffsetPlus)) negOffset = ((if (drawValueAboveBar) -(valueTextWidth + valueOffsetPlus) else valueOffsetPlus) - (buffer.buffer[j + 2] - buffer.buffer[j])) @@ -302,7 +303,7 @@ open class HorizontalBarChartRenderer( ) // calculate the correct offset depending on the draw position of the value - val valueTextWidth = Utils.calcTextWidth(paintValues, formattedValue).toFloat() + val valueTextWidth = paintValues.calcTextWidth(formattedValue).toFloat() posOffset = (if (drawValueAboveBar) valueOffsetPlus else -(valueTextWidth + valueOffsetPlus)) negOffset = (if (drawValueAboveBar) -(valueTextWidth + valueOffsetPlus) else valueOffsetPlus) @@ -380,7 +381,7 @@ open class HorizontalBarChartRenderer( ) // calculate the correct offset depending on the draw position of the value - val valueTextWidth = Utils.calcTextWidth(paintValues, formattedValue).toFloat() + val valueTextWidth = paintValues.calcTextWidth(formattedValue).toFloat() posOffset = (if (drawValueAboveBar) valueOffsetPlus else -(valueTextWidth + valueOffsetPlus)) negOffset = (if (drawValueAboveBar) -(valueTextWidth + valueOffsetPlus) else valueOffsetPlus) diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/LegendRenderer.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/LegendRenderer.kt index 2799b6c966..a9d4f33eac 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/LegendRenderer.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/LegendRenderer.kt @@ -20,6 +20,7 @@ import com.github.mikephil.charting.interfaces.datasets.IPieDataSet import com.github.mikephil.charting.utils.ColorTemplate import com.github.mikephil.charting.utils.Utils import com.github.mikephil.charting.utils.ViewPortHandler +import com.github.mikephil.charting.utils.calcTextWidth import com.github.mikephil.charting.utils.convertDpToPixel import java.util.Collections import kotlin.math.min @@ -396,7 +397,7 @@ open class LegendRenderer( -formToTextSpace else if (wasStacked) posX = originPosX - if (direction == LegendDirection.RIGHT_TO_LEFT) posX -= Utils.calcTextWidth(labelPaint, e.label).toFloat() + if (direction == LegendDirection.RIGHT_TO_LEFT) posX -= labelPaint.calcTextWidth(e.label).toFloat() if (e.label != null) if (!wasStacked) { diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/RadarChartRenderer.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/RadarChartRenderer.kt index 7a2b290f44..453ccd2991 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/RadarChartRenderer.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/RadarChartRenderer.kt @@ -14,6 +14,7 @@ import com.github.mikephil.charting.utils.MPPointF import com.github.mikephil.charting.utils.Utils import com.github.mikephil.charting.utils.ViewPortHandler import com.github.mikephil.charting.utils.convertDpToPixel +import com.github.mikephil.charting.utils.getPosition open class RadarChartRenderer( protected var chart: RadarChart, animator: ChartAnimator, @@ -60,7 +61,7 @@ open class RadarChartRenderer( val factor = chart.factor val center = chart.centerOffsets - val pOut = MPPointF.getInstance(0f, 0f) + var pOut = MPPointF.getInstance(0f, 0f) val surface = drawDataSetSurfacePathBuffer surface.reset() @@ -71,10 +72,9 @@ open class RadarChartRenderer( dataSet.getEntryForIndex(j)?.let { e -> - Utils.getPosition( - center, + pOut = center.getPosition( (e.y - chart.yChartMin) * factor * phaseY, - sliceAngle * j * phaseX + chart.rotationAngle, pOut + sliceAngle * j * phaseX + chart.rotationAngle ) } if (java.lang.Float.isNaN(pOut.x)) continue @@ -122,8 +122,8 @@ open class RadarChartRenderer( val factor = chart.factor val center = chart.centerOffsets - val pOut = MPPointF.getInstance(0f, 0f) - val pIcon = MPPointF.getInstance(0f, 0f) + var pOut = MPPointF.getInstance(0f, 0f) + var pIcon = MPPointF.getInstance(0f, 0f) val yOffset = 5f.convertDpToPixel() @@ -146,12 +146,10 @@ open class RadarChartRenderer( for (j in 0.. - Utils.getPosition( - center, + pOut = center.getPosition( (entry.y - chart.yChartMin) * factor * phaseY, - sliceAngle * j * phaseX + chart.rotationAngle, - pOut - ) + sliceAngle * j * phaseX + chart.rotationAngle + ) if (dataSet.isDrawValues) { drawValue( @@ -169,12 +167,10 @@ open class RadarChartRenderer( if (entry.icon != null && dataSet.isDrawIcons) { val icon = entry.icon - Utils.getPosition( - center, + pIcon = center.getPosition( (entry.y) * factor * phaseY + iconsOffset.y, - sliceAngle * j * phaseX + chart.rotationAngle, - pIcon - ) + sliceAngle * j * phaseX + chart.rotationAngle + ) pIcon.y += iconsOffset.x @@ -220,14 +216,12 @@ open class RadarChartRenderer( val xIncrements = 1 + chart.skipWebLineCount val maxEntryCount = chart.data!!.maxEntryCountSet.entryCount - val p = MPPointF.getInstance(0f, 0f) + var p = MPPointF.getInstance(0f, 0f) var i = 0 while (i < maxEntryCount) { - Utils.getPosition( - center, + p = center.getPosition( chart.yRange * factor, - sliceAngle * i + rotationAngle, - p + sliceAngle * i + rotationAngle ) canvas.drawLine(center.x, center.y, p.x, p.y, webPaint) @@ -242,8 +236,8 @@ open class RadarChartRenderer( val labelCount = chart.yAxis.mEntryCount - val p1out = MPPointF.getInstance(0f, 0f) - val p2out = MPPointF.getInstance(0f, 0f) + var p1out = MPPointF.getInstance(0f, 0f) + var p2out = MPPointF.getInstance(0f, 0f) for (j in 0.. 1) { - val width = Utils.calcTextWidth(paintAxisLabels, label).toFloat() + val width = paintAxisLabels.calcTextWidth(label).toFloat() if (width > viewPortHandler.offsetRight() * 2 && x + width > viewPortHandler.chartWidth @@ -213,7 +215,7 @@ open class XAxisRenderer( // avoid clipping of the first } else if (i == 0) { - val width = Utils.calcTextWidth(paintAxisLabels, label).toFloat() + val width = paintAxisLabels.calcTextWidth(label).toFloat() x += width / 2 } } @@ -225,7 +227,7 @@ open class XAxisRenderer( } protected fun drawLabel(canvas: Canvas, formattedLabel: String?, x: Float, y: Float, anchor: MPPointF, angleDegrees: Float) { - Utils.drawXAxisValue(canvas, formattedLabel, x, y, paintAxisLabels, anchor, angleDegrees) + canvas.drawXAxisValue(formattedLabel, x, y, paintAxisLabels, anchor, angleDegrees) } protected open var mRenderGridLinesPath: Path = Path() diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/XAxisRendererRadarChart.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/XAxisRendererRadarChart.kt index a3df4c77b7..4eaf3b85ab 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/XAxisRendererRadarChart.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/XAxisRendererRadarChart.kt @@ -4,8 +4,8 @@ import android.graphics.Canvas import com.github.mikephil.charting.charts.RadarChart import com.github.mikephil.charting.components.XAxis import com.github.mikephil.charting.utils.MPPointF -import com.github.mikephil.charting.utils.Utils import com.github.mikephil.charting.utils.ViewPortHandler +import com.github.mikephil.charting.utils.getPosition class XAxisRendererRadarChart(viewPortHandler: ViewPortHandler, xAxis: XAxis, private val chart: RadarChart) : XAxisRenderer(viewPortHandler, xAxis, null) { override fun renderAxisLabels(canvas: Canvas) { @@ -26,14 +26,14 @@ class XAxisRendererRadarChart(viewPortHandler: ViewPortHandler, xAxis: XAxis, pr val factor = chart.factor val center = chart.centerOffsets - val pOut = MPPointF.getInstance(0f, 0f) + var pOut = MPPointF.getInstance(0f, 0f) for (i in 0.. Date: Tue, 16 Dec 2025 19:45:10 +0100 Subject: [PATCH 2/2] Kotlin calcTextHeight --- .../github/mikephil/charting/components/Legend.kt | 9 +++------ .../github/mikephil/charting/components/YAxis.kt | 4 ++-- .../mikephil/charting/renderer/BarChartRenderer.kt | 3 ++- .../charting/renderer/BubbleChartRenderer.kt | 3 ++- .../renderer/HorizontalBarChartRenderer.kt | 3 ++- .../mikephil/charting/renderer/LegendRenderer.kt | 3 ++- .../mikephil/charting/renderer/PieChartRenderer.kt | 4 ++-- .../mikephil/charting/renderer/XAxisRenderer.kt | 7 ++++--- .../renderer/XAxisRendererHorizontalBarChart.kt | 3 ++- .../mikephil/charting/renderer/YAxisRenderer.kt | 8 ++++---- .../renderer/YAxisRendererHorizontalBarChart.kt | 8 ++++---- .../github/mikephil/charting/utils/CanvasUtils.kt | 11 +++++++++++ .../com/github/mikephil/charting/utils/Utils.java | 14 -------------- 13 files changed, 40 insertions(+), 40 deletions(-) diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/components/Legend.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/components/Legend.kt index e3f4ad7726..cbbf90157f 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/components/Legend.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/components/Legend.kt @@ -6,6 +6,7 @@ import com.github.mikephil.charting.utils.ColorTemplate import com.github.mikephil.charting.utils.FSize import com.github.mikephil.charting.utils.Utils import com.github.mikephil.charting.utils.ViewPortHandler +import com.github.mikephil.charting.utils.calcTextHeight import com.github.mikephil.charting.utils.calcTextWidth import com.github.mikephil.charting.utils.convertDpToPixel import java.lang.Float @@ -14,9 +15,6 @@ import kotlin.Boolean import kotlin.IntArray import kotlin.String import kotlin.arrayOf -import kotlin.collections.ArrayList -import kotlin.collections.MutableList -import kotlin.collections.toTypedArray import kotlin.math.max import kotlin.math.min import kotlin.requireNotNull @@ -304,10 +302,9 @@ class Legend() : ComponentBase() { var max = 0f for (entry in this.entries) { - val label = entry.label - if (label == null) continue + val label = entry.label ?: continue - val length = Utils.calcTextHeight(p, label).toFloat() + val length = p.calcTextHeight(label).toFloat() if (length > max) max = length } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/components/YAxis.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/components/YAxis.kt index 7f140e4315..e9283749ad 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/components/YAxis.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/components/YAxis.kt @@ -2,7 +2,7 @@ package com.github.mikephil.charting.components import android.graphics.Color import android.graphics.Paint -import com.github.mikephil.charting.utils.Utils +import com.github.mikephil.charting.utils.calcTextHeight import com.github.mikephil.charting.utils.calcTextWidth import com.github.mikephil.charting.utils.convertDpToPixel import kotlin.math.abs @@ -263,7 +263,7 @@ class YAxis : AxisBase { p.textSize = mTextSize val label = getLongestLabel(p) - return Utils.calcTextHeight(p, label).toFloat() + yOffset * 2f + return p.calcTextHeight(label).toFloat() + yOffset * 2f } /** diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/BarChartRenderer.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/BarChartRenderer.kt index 96f4c245ce..a1b0756313 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/BarChartRenderer.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/BarChartRenderer.kt @@ -14,6 +14,7 @@ import com.github.mikephil.charting.utils.MPPointF import com.github.mikephil.charting.utils.Transformer import com.github.mikephil.charting.utils.Utils import com.github.mikephil.charting.utils.ViewPortHandler +import com.github.mikephil.charting.utils.calcTextHeight import com.github.mikephil.charting.utils.convertDpToPixel import kotlin.math.ceil import kotlin.math.min @@ -277,7 +278,7 @@ open class BarChartRenderer( // calculate the correct offset depending on the draw position of // the value - val valueTextHeight = Utils.calcTextHeight(paintValues, "8").toFloat() + val valueTextHeight = paintValues.calcTextHeight("8").toFloat() posOffset = (if (drawValueAboveBar) -valueOffsetPlus else valueTextHeight + valueOffsetPlus) negOffset = (if (drawValueAboveBar) valueTextHeight + valueOffsetPlus else -valueOffsetPlus) diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/BubbleChartRenderer.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/BubbleChartRenderer.kt index fa3e2e5b34..8c18b359e0 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/BubbleChartRenderer.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/BubbleChartRenderer.kt @@ -10,6 +10,7 @@ import com.github.mikephil.charting.interfaces.datasets.IBubbleDataSet import com.github.mikephil.charting.utils.MPPointF import com.github.mikephil.charting.utils.Utils import com.github.mikephil.charting.utils.ViewPortHandler +import com.github.mikephil.charting.utils.calcTextHeight import com.github.mikephil.charting.utils.convertDpToPixel import kotlin.math.abs import kotlin.math.max @@ -97,7 +98,7 @@ open class BubbleChartRenderer( if (isDrawingValuesAllowed(dataProvider)) { val dataSets = bubbleData.dataSets - val lineHeight = Utils.calcTextHeight(paintValues, "1").toFloat() + val lineHeight = paintValues.calcTextHeight("1").toFloat() for (i in dataSets.indices) { val dataSet = dataSets[i] diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/HorizontalBarChartRenderer.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/HorizontalBarChartRenderer.kt index 2e78311ad5..a9b90544d6 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/HorizontalBarChartRenderer.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/HorizontalBarChartRenderer.kt @@ -14,6 +14,7 @@ import com.github.mikephil.charting.utils.MPPointF import com.github.mikephil.charting.utils.Transformer import com.github.mikephil.charting.utils.Utils import com.github.mikephil.charting.utils.ViewPortHandler +import com.github.mikephil.charting.utils.calcTextHeight import com.github.mikephil.charting.utils.calcTextWidth import com.github.mikephil.charting.utils.convertDpToPixel import kotlin.math.ceil @@ -188,7 +189,7 @@ open class HorizontalBarChartRenderer( // apply the text-styling defined by the DataSet applyValueTextStyle(dataSet) - val halfTextHeight = Utils.calcTextHeight(paintValues, "10") / 2f + val halfTextHeight = paintValues.calcTextHeight("10") / 2f val formatter = dataSet.valueFormatter diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/LegendRenderer.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/LegendRenderer.kt index a9d4f33eac..a18ccb5d02 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/LegendRenderer.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/LegendRenderer.kt @@ -20,6 +20,7 @@ import com.github.mikephil.charting.interfaces.datasets.IPieDataSet import com.github.mikephil.charting.utils.ColorTemplate import com.github.mikephil.charting.utils.Utils import com.github.mikephil.charting.utils.ViewPortHandler +import com.github.mikephil.charting.utils.calcTextHeight import com.github.mikephil.charting.utils.calcTextWidth import com.github.mikephil.charting.utils.convertDpToPixel import java.util.Collections @@ -212,7 +213,7 @@ open class LegendRenderer( val labelLineHeight = Utils.getLineHeight(labelPaint, legendFontMetrics) val labelLineSpacing = (Utils.getLineSpacing(labelPaint, legendFontMetrics) + legend.yEntrySpace.convertDpToPixel()) - val formYOffset = labelLineHeight - Utils.calcTextHeight(labelPaint, "ABC") / 2f + val formYOffset = labelLineHeight - labelPaint.calcTextHeight("ABC") / 2f val entries = legend.entries diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/PieChartRenderer.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/PieChartRenderer.kt index b3b0fc5e20..430973cf75 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/PieChartRenderer.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/PieChartRenderer.kt @@ -21,6 +21,7 @@ import com.github.mikephil.charting.utils.ColorTemplate import com.github.mikephil.charting.utils.MPPointF import com.github.mikephil.charting.utils.Utils import com.github.mikephil.charting.utils.ViewPortHandler +import com.github.mikephil.charting.utils.calcTextHeight import com.github.mikephil.charting.utils.convertDpToPixel import java.lang.ref.WeakReference import kotlin.math.abs @@ -403,8 +404,7 @@ open class PieChartRenderer( // apply the text-styling defined by the DataSet applyValueTextStyle(dataSet) - val lineHeight = (Utils.calcTextHeight(paintValues, "Q") - + 4f.convertDpToPixel()) + val lineHeight = paintValues.calcTextHeight("Q") + 4f.convertDpToPixel() val formatter = dataSet.valueFormatter diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/XAxisRenderer.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/XAxisRenderer.kt index 7dcd4cd289..a556ac0bfe 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/XAxisRenderer.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/XAxisRenderer.kt @@ -18,6 +18,7 @@ import com.github.mikephil.charting.utils.MPPointF import com.github.mikephil.charting.utils.Transformer import com.github.mikephil.charting.utils.Utils import com.github.mikephil.charting.utils.ViewPortHandler +import com.github.mikephil.charting.utils.calcTextHeight import com.github.mikephil.charting.utils.calcTextWidth import com.github.mikephil.charting.utils.convertDpToPixel import com.github.mikephil.charting.utils.drawXAxisValue @@ -75,7 +76,7 @@ open class XAxisRenderer( val labelSize = Utils.calcTextSize(paintAxisLabels, longest) val labelWidth = labelSize.width - val labelHeight = Utils.calcTextHeight(paintAxisLabels, "Q").toFloat() + val labelHeight = paintAxisLabels.calcTextHeight("Q").toFloat() val labelRotatedSize = Utils.getSizeOfRotatedRectangleByDegrees( labelWidth, @@ -392,7 +393,7 @@ open class XAxisRenderer( when (labelPosition) { LimitLabelPosition.RIGHT_TOP -> { - val labelLineHeight = Utils.calcTextHeight(limitLinePaint, label).toFloat() + val labelLineHeight = limitLinePaint.calcTextHeight(label).toFloat() limitLinePaint.textAlign = Align.LEFT canvas.drawText( label, position[0] + xOffset, viewPortHandler.contentTop() + yOffset + labelLineHeight, @@ -407,7 +408,7 @@ open class XAxisRenderer( LimitLabelPosition.LEFT_TOP -> { limitLinePaint.textAlign = Align.RIGHT - val labelLineHeight = Utils.calcTextHeight(limitLinePaint, label).toFloat() + val labelLineHeight = limitLinePaint.calcTextHeight(label).toFloat() canvas.drawText( label, position[0] - xOffset, viewPortHandler.contentTop() + yOffset + labelLineHeight, limitLinePaint diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/XAxisRendererHorizontalBarChart.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/XAxisRendererHorizontalBarChart.kt index cd29bcee04..524629905a 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/XAxisRendererHorizontalBarChart.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/XAxisRendererHorizontalBarChart.kt @@ -15,6 +15,7 @@ import com.github.mikephil.charting.utils.MPPointF import com.github.mikephil.charting.utils.Transformer import com.github.mikephil.charting.utils.Utils import com.github.mikephil.charting.utils.ViewPortHandler +import com.github.mikephil.charting.utils.calcTextHeight import com.github.mikephil.charting.utils.convertDpToPixel import kotlin.math.roundToInt @@ -250,7 +251,7 @@ open class XAxisRendererHorizontalBarChart( limitLinePaint.strokeWidth = 0.5f limitLinePaint.textSize = limitLine.textSize - val labelLineHeight = Utils.calcTextHeight(limitLinePaint, label).toFloat() + val labelLineHeight = limitLinePaint.calcTextHeight(label).toFloat() val xOffset = 4f.convertDpToPixel() + limitLine.xOffset val yOffset = limitLine.lineWidth + labelLineHeight + limitLine.yOffset diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/YAxisRenderer.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/YAxisRenderer.kt index 5e4ce3cde5..02ddfc750f 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/YAxisRenderer.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/YAxisRenderer.kt @@ -11,10 +11,10 @@ import com.github.mikephil.charting.components.YAxis import com.github.mikephil.charting.components.YAxis.AxisDependency import com.github.mikephil.charting.components.YAxis.YAxisLabelPosition import com.github.mikephil.charting.utils.Transformer -import com.github.mikephil.charting.utils.Utils import com.github.mikephil.charting.utils.ViewPortHandler import androidx.core.graphics.withSave import androidx.core.graphics.withClip +import com.github.mikephil.charting.utils.calcTextHeight import com.github.mikephil.charting.utils.convertDpToPixel open class YAxisRenderer(viewPortHandler: ViewPortHandler, @JvmField protected var yAxis: YAxis, trans: Transformer?) : @@ -61,7 +61,7 @@ open class YAxisRenderer(viewPortHandler: ViewPortHandler, @JvmField protected v paintAxisLabels.textSize = yAxis.textSize paintAxisLabels.color = yAxis.textColor - val yOffset = Utils.calcTextHeight(paintAxisLabels, "A") / 2.5f + yAxis.yOffset + val yOffset = paintAxisLabels.calcTextHeight("A") / 2.5f + yAxis.yOffset val axisDependency = yAxis.axisDependency val labelPosition = yAxis.labelPosition @@ -316,7 +316,7 @@ open class YAxisRenderer(viewPortHandler: ViewPortHandler, @JvmField protected v limitLinePaint.strokeWidth = 0.5f limitLinePaint.textSize = limitLine.textSize - val labelLineHeight = Utils.calcTextHeight(limitLinePaint, label).toFloat() + val labelLineHeight = limitLinePaint.calcTextHeight(label).toFloat() val xOffset = 4f.convertDpToPixel() + limitLine.xOffset val yOffset = limitLine.lineWidth + labelLineHeight + limitLine.yOffset @@ -435,7 +435,7 @@ open class YAxisRenderer(viewPortHandler: ViewPortHandler, @JvmField protected v limitRangePaint.strokeWidth = 0.5f limitRangePaint.textSize = limitRange.textSize - val labelLineHeight = Utils.calcTextHeight(limitRangePaint, label).toFloat() + val labelLineHeight = limitRangePaint.calcTextHeight(label).toFloat() val xOffset = 4f.convertDpToPixel() + limitRange.xOffset val yOffset = limitRange.lineWidth + labelLineHeight + limitRange.yOffset diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/YAxisRendererHorizontalBarChart.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/YAxisRendererHorizontalBarChart.kt index 91ffb7e259..16dc364b35 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/YAxisRendererHorizontalBarChart.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/YAxisRendererHorizontalBarChart.kt @@ -12,8 +12,8 @@ import com.github.mikephil.charting.components.YAxis.AxisDependency import com.github.mikephil.charting.components.YAxis.YAxisLabelPosition import com.github.mikephil.charting.utils.MPPointD import com.github.mikephil.charting.utils.Transformer -import com.github.mikephil.charting.utils.Utils import com.github.mikephil.charting.utils.ViewPortHandler +import com.github.mikephil.charting.utils.calcTextHeight import com.github.mikephil.charting.utils.convertDpToPixel @Suppress("MemberVisibilityCanBePrivate") @@ -75,7 +75,7 @@ open class YAxisRendererHorizontalBarChart( paintAxisLabels.textAlign = Align.CENTER val baseYOffset = 2.5f.convertDpToPixel() - val textHeight = Utils.calcTextHeight(paintAxisLabels, "Q").toFloat() + val textHeight = paintAxisLabels.calcTextHeight("Q").toFloat() val dependency = yAxis.axisDependency val labelPosition = yAxis.labelPosition @@ -280,7 +280,7 @@ open class YAxisRendererHorizontalBarChart( when (position) { LimitLabelPosition.RIGHT_TOP -> { - val labelLineHeight = Utils.calcTextHeight(limitLinePaint, label).toFloat() + val labelLineHeight = limitLinePaint.calcTextHeight(label).toFloat() limitLinePaint.textAlign = Align.LEFT canvas.drawText(label, pts[0] + xOffset, viewPortHandler.contentTop() + yOffset + labelLineHeight, limitLinePaint) } @@ -290,7 +290,7 @@ open class YAxisRendererHorizontalBarChart( } LimitLabelPosition.LEFT_TOP -> { limitLinePaint.textAlign = Align.RIGHT - val labelLineHeight = Utils.calcTextHeight(limitLinePaint, label).toFloat() + val labelLineHeight = limitLinePaint.calcTextHeight(label).toFloat() canvas.drawText(label, pts[0] - xOffset, viewPortHandler.contentTop() + yOffset + labelLineHeight, limitLinePaint) } else -> { diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/CanvasUtils.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/CanvasUtils.kt index 34b66e3cc5..1936d1e66f 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/CanvasUtils.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/CanvasUtils.kt @@ -21,6 +21,17 @@ val FDEG2RAD: Float = (Math.PI.toFloat() / 180f) */ fun Paint.calcTextWidth(demoText: String?) = this.measureText(demoText).toInt() +/** + * calculates the approximate height of a text, depending on a demo text + * avoid repeated calls (e.g. inside drawing methods) + */ +fun Paint.calcTextHeight(demoText: String): Int { + val rect = Rect() + rect.set(0, 0, 0, 0) + this.getTextBounds(demoText, 0, demoText.length, rect) + return rect.height() +} + /** * Utilities class that has some helper methods. Needs to be initialized by * calling Utils.init(...) before usage. Inside the Chart.init() method, this is diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/Utils.java b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/Utils.java index 2b0e1cbf72..5dbbaf2b7d 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/Utils.java +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/Utils.java @@ -47,20 +47,6 @@ public static void init(@NonNull Context context) { maximumFlingVelocity = viewConfiguration.getScaledMaximumFlingVelocity(); } - private static final Rect mCalcTextHeightRect = new Rect(); - - /** - * calculates the approximate height of a text, depending on a demo text - * avoid repeated calls (e.g. inside drawing methods) - */ - public static int calcTextHeight(Paint paint, String demoText) { - - Rect r = mCalcTextHeightRect; - r.set(0, 0, 0, 0); - paint.getTextBounds(demoText, 0, demoText.length(), r); - return r.height(); - } - private static final Paint.FontMetrics mFontMetrics = new Paint.FontMetrics(); public static float getLineHeight(Paint paint) {