From 96fc3d2d457983466bd699e6c15aab184dd4e078 Mon Sep 17 00:00:00 2001 From: Hannes Achleitner Date: Tue, 16 Dec 2025 14:57:40 +0100 Subject: [PATCH 1/4] Utils cleanup --- .../github/mikephil/charting/utils/Utils.java | 100 ------------------ 1 file changed, 100 deletions(-) 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 97fa39626..fc6603dbb 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 @@ -6,9 +6,6 @@ import android.graphics.Paint; import android.graphics.Rect; import android.graphics.drawable.Drawable; -import android.text.StaticLayout; -import android.text.TextPaint; -import android.util.DisplayMetrics; import android.view.MotionEvent; import android.view.VelocityTracker; import android.view.ViewConfiguration; @@ -155,26 +152,6 @@ public static IValueFormatter getDefaultValueFormatter() { return mDefaultValueFormatter; } - /** - * rounds the given number to the next significant number - * - * @param number - * @return - */ - public static float roundToNextSignificant(double number) { - if (Double.isInfinite(number) || - Double.isNaN(number) || - number == 0.0) { - return 0; - } - - final float d = (float) Math.ceil((float) Math.log10(number < 0 ? -number : number)); - final int pw = 1 - (int) d; - final float magnitude = (float) Math.pow(10, pw); - final long shifted = Math.round(number * magnitude); - return shifted / magnitude; - } - /** * Returns a recyclable MPPointF instance. * Calculates the position around a center point, depending on the distance @@ -327,83 +304,6 @@ public static void drawXAxisValue(Canvas canvas, String text, float x, float y, paint.setTextAlign(originalTextAlign); } - public static void drawMultilineText(Canvas canvas, StaticLayout textLayout, - float x, float y, - TextPaint paint, - MPPointF anchor, float angleDegrees) { - - float drawOffsetX = 0.f; - float drawOffsetY = 0.f; - float drawWidth; - float drawHeight; - - final float lineHeight = paint.getFontMetrics(mFontMetricsBuffer); - - drawWidth = textLayout.getWidth(); - drawHeight = textLayout.getLineCount() * lineHeight; - - // Android sometimes has pre-padding - drawOffsetX -= mDrawTextRectBuffer.left; - - // Android does not snap the bounds to line boundaries, - // and draws from bottom to top. - // And we want to normalize it. - drawOffsetY += drawHeight; - - // To have a consistent point of reference, we always draw left-aligned - Paint.Align originalTextAlign = paint.getTextAlign(); - paint.setTextAlign(Paint.Align.LEFT); - - if (angleDegrees != 0.f) { - - // Move the text drawing rect in a way that it always rotates around its center - drawOffsetX -= drawWidth * 0.5f; - drawOffsetY -= drawHeight * 0.5f; - - float translateX = x; - float translateY = y; - - // Move the "outer" rect relative to the anchor, assuming its centered - if (anchor.x != 0.5f || anchor.y != 0.5f) { - final FSize rotatedSize = getSizeOfRotatedRectangleByDegrees( - drawWidth, - drawHeight, - angleDegrees); - - translateX -= rotatedSize.width * (anchor.x - 0.5f); - translateY -= rotatedSize.height * (anchor.y - 0.5f); - FSize.recycleInstance(rotatedSize); - } - - canvas.save(); - canvas.translate(translateX, translateY); - canvas.rotate(angleDegrees); - - canvas.translate(drawOffsetX, drawOffsetY); - textLayout.draw(canvas); - - canvas.restore(); - } else { - if (anchor.x != 0.f || anchor.y != 0.f) { - - drawOffsetX -= drawWidth * anchor.x; - drawOffsetY -= drawHeight * anchor.y; - } - - drawOffsetX += x; - drawOffsetY += y; - - canvas.save(); - - canvas.translate(drawOffsetX, drawOffsetY); - textLayout.draw(canvas); - - canvas.restore(); - } - - paint.setTextAlign(originalTextAlign); - } - /** * Returns a recyclable FSize instance. * Represents size of a rotated rectangle by degrees. From 7649e5303ca17ec7bd0637527cecf6c3753f2ecb Mon Sep 17 00:00:00 2001 From: Hannes Achleitner Date: Tue, 16 Dec 2025 19:38:51 +0100 Subject: [PATCH 2/4] Kotlin calcTextWidth --- .../mikephil/charting/components/Legend.java | 5 +- .../mikephil/charting/components/YAxis.java | 3 +- .../charting/highlight/RadarHighlighter.java | 6 +- .../renderer/HorizontalBarChartRenderer.kt | 7 +- .../charting/renderer/LegendRenderer.kt | 3 +- .../charting/renderer/RadarChartRenderer.kt | 52 +++++------- .../charting/renderer/XAxisRenderer.kt | 12 +-- .../renderer/XAxisRendererRadarChart.kt | 8 +- .../renderer/YAxisRendererRadarChart.kt | 10 +-- .../mikephil/charting/utils/CanvasUtils.kt | 5 ++ .../github/mikephil/charting/utils/Utils.java | 81 ------------------- .../github/mikephil/charting/utils/UtilsKt.kt | 13 ++- 12 files changed, 67 insertions(+), 138 deletions(-) diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/components/Legend.java b/MPChartLib/src/main/java/com/github/mikephil/charting/components/Legend.java index 85adf9eec..f813d60a1 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/components/Legend.java +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/components/Legend.java @@ -3,6 +3,7 @@ import android.graphics.DashPathEffect; import android.graphics.Paint; +import com.github.mikephil.charting.utils.CanvasUtilsKt; import com.github.mikephil.charting.utils.ColorTemplate; import com.github.mikephil.charting.utils.FSize; import com.github.mikephil.charting.utils.Utils; @@ -199,7 +200,7 @@ public float getMaximumEntryWidth(Paint p) { String label = entry.label; if (label == null) continue; - float length = (float) Utils.calcTextWidth(p, label); + float length = (float) CanvasUtilsKt.calcTextWidth(p, label); if (length > max) max = length; @@ -636,7 +637,7 @@ else if (wasStacked) { wasStacked = false; } - width += Utils.calcTextWidth(labelpaint, label); + width += CanvasUtilsKt.calcTextWidth(labelpaint, label); maxHeight += labelLineHeight + yEntrySpace; } else { diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/components/YAxis.java b/MPChartLib/src/main/java/com/github/mikephil/charting/components/YAxis.java index d8a04ea86..7008d343a 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/components/YAxis.java +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/components/YAxis.java @@ -3,6 +3,7 @@ import android.graphics.Color; import android.graphics.Paint; +import com.github.mikephil.charting.utils.CanvasUtilsKt; import com.github.mikephil.charting.utils.Utils; import com.github.mikephil.charting.utils.UtilsKtKt; @@ -309,7 +310,7 @@ public float getRequiredWidthSpace(Paint p) { p.setTextSize(mTextSize); String label = getLongestLabel(p); - float width = (float) Utils.calcTextWidth(p, label) + getXOffset() * 2f; + float width = (float) CanvasUtilsKt.calcTextWidth(p, label) + getXOffset() * 2f; float minWidth = getMinWidth(); float maxWidth = getMaxWidth(); diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/highlight/RadarHighlighter.java b/MPChartLib/src/main/java/com/github/mikephil/charting/highlight/RadarHighlighter.java index d9c64f667..8a03f21b6 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/highlight/RadarHighlighter.java +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/highlight/RadarHighlighter.java @@ -4,7 +4,7 @@ import com.github.mikephil.charting.data.Entry; import com.github.mikephil.charting.interfaces.datasets.IDataSet; import com.github.mikephil.charting.utils.MPPointF; -import com.github.mikephil.charting.utils.Utils; +import com.github.mikephil.charting.utils.UtilsKtKt; import java.util.List; @@ -65,9 +65,9 @@ protected List getHighlightsAtIndex(int index) { float y = (entry.getY() - mChart.getYChartMin()); - Utils.getPosition( + pOut = UtilsKtKt.getPosition( mChart.getCenterOffsets(), y * factor * phaseY, - sliceangle * index * phaseX + mChart.getRotationAngle(), pOut + sliceangle * index * phaseX + mChart.getRotationAngle() ); mHighlightBuffer.add(new Highlight(index, entry.getY(), pOut.x, pOut.y, i, dataSet.getAxisDependency())); 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 fe0b27469..f55aef987 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 ac0b65448..96ed3c172 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 @@ -19,6 +19,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 @@ -380,7 +381,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 (!wasStacked) { drawLabel(canvas, posX, posY + labelLineHeight, e.label) 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 743b7ea9b..0a6f01410 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, @@ -63,7 +64,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() @@ -74,10 +75,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 @@ -125,8 +125,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() @@ -149,12 +149,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( @@ -172,12 +170,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 @@ -223,14 +219,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) @@ -245,8 +239,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 @@ -209,7 +211,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 } } @@ -221,7 +223,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 eb2a7e072..60c4be3ce 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 3/4] Kotlin calcTextHeight --- .../mikephil/charting/components/Legend.java | 2 +- .../mikephil/charting/components/YAxis.java | 3 +-- .../charting/renderer/BarChartRenderer.kt | 3 ++- .../charting/renderer/BubbleChartRenderer.kt | 3 ++- .../renderer/HorizontalBarChartRenderer.kt | 3 ++- .../charting/renderer/LegendRenderer.kt | 3 ++- .../charting/renderer/PieChartRenderer.kt | 4 ++-- .../charting/renderer/XAxisRenderer.kt | 7 ++++--- .../XAxisRendererHorizontalBarChart.kt | 3 ++- .../charting/renderer/YAxisRenderer.kt | 8 ++++---- .../YAxisRendererHorizontalBarChart.kt | 8 ++++---- .../mikephil/charting/utils/CanvasUtils.kt | 11 +++++++++++ .../github/mikephil/charting/utils/Utils.java | 18 ------------------ 13 files changed, 37 insertions(+), 39 deletions(-) diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/components/Legend.java b/MPChartLib/src/main/java/com/github/mikephil/charting/components/Legend.java index f813d60a1..271b4071f 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/components/Legend.java +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/components/Legend.java @@ -222,7 +222,7 @@ public float getMaximumEntryHeight(Paint p) { String label = entry.label; if (label == null) continue; - float length = (float) Utils.calcTextHeight(p, label); + float length = (float) CanvasUtilsKt.calcTextHeight(p, label); if (length > max) max = length; diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/components/YAxis.java b/MPChartLib/src/main/java/com/github/mikephil/charting/components/YAxis.java index 7008d343a..abe06612a 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/components/YAxis.java +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/components/YAxis.java @@ -4,7 +4,6 @@ import android.graphics.Paint; import com.github.mikephil.charting.utils.CanvasUtilsKt; -import com.github.mikephil.charting.utils.Utils; import com.github.mikephil.charting.utils.UtilsKtKt; /** @@ -334,7 +333,7 @@ public float getRequiredHeightSpace(Paint p) { p.setTextSize(mTextSize); String label = getLongestLabel(p); - return (float) Utils.calcTextHeight(p, label) + getYOffset() * 2f; + return (float) CanvasUtilsKt.calcTextHeight(p, label) + getYOffset() * 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 9f33e863c..756e164f8 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 efecf4f61..384a6a9fc 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 @@ -96,7 +97,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 f55aef987..d5a2db51b 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 96ed3c172..2d5f20db8 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 @@ -19,6 +19,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 @@ -211,7 +212,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 38027684f..6bfbb8724 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 @@ -406,8 +407,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 649e66a38..5bddd88fa 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, @@ -388,7 +389,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, @@ -401,7 +402,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 e47e637cc..118e49373 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 49792a15e..ab2e7ec4a 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 dependency = yAxis.axisDependency val labelPosition = yAxis.labelPosition @@ -314,7 +314,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 @@ -433,7 +433,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 f439de821..e7a71ca67 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 @@ -11,9 +11,9 @@ 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 androidx.core.graphics.withSave +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 7316db392..301996ade 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 e75589fb9..5cf4119cb 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,24 +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) - * - * @param paint - * @param demoText - * @return - */ - 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) { From e375a8bf88e3ee5c7e80662a73846b38a132e6f9 Mon Sep 17 00:00:00 2001 From: Hannes Achleitner Date: Tue, 16 Dec 2025 20:15:44 +0100 Subject: [PATCH 4/4] Kotlin getNormalizedAngle --- .../mikephil/charting/charts/PieChart.java | 3 +- .../charting/charts/PieRadarChartBase.java | 3 +- .../mikephil/charting/charts/RadarChart.java | 3 +- .../mikephil/charting/components/Legend.java | 2 +- .../mikephil/charting/data/BaseDataSet.kt | 2 +- .../charting/renderer/XAxisRenderer.kt | 3 +- .../XAxisRendererHorizontalBarChart.kt | 3 +- .../mikephil/charting/utils/CanvasUtils.kt | 30 +++++++ .../github/mikephil/charting/utils/Utils.java | 79 ------------------- .../github/mikephil/charting/utils/UtilsKt.kt | 18 ++++- 10 files changed, 54 insertions(+), 92 deletions(-) diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/PieChart.java b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/PieChart.java index d240b446e..883dd0881 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/PieChart.java +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/PieChart.java @@ -17,7 +17,6 @@ import com.github.mikephil.charting.interfaces.datasets.IPieDataSet; import com.github.mikephil.charting.renderer.PieChartRenderer; import com.github.mikephil.charting.utils.MPPointF; -import com.github.mikephil.charting.utils.Utils; import com.github.mikephil.charting.utils.UtilsKtKt; import java.util.List; @@ -344,7 +343,7 @@ public XAxis getXAxis() { public int getIndexForAngle(float angle) { // take the current angle of the chart into consideration - float a = Utils.getNormalizedAngle(angle - getRotationAngle()); + float a = UtilsKtKt.getNormalizedAngle(angle - getRotationAngle()); for (int i = 0; i < mAbsoluteAngles.length; i++) { if (mAbsoluteAngles[i] > a) diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/PieRadarChartBase.java b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/PieRadarChartBase.java index bc07f4138..45e8f336e 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/PieRadarChartBase.java +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/PieRadarChartBase.java @@ -19,7 +19,6 @@ import com.github.mikephil.charting.interfaces.datasets.IDataSet; import com.github.mikephil.charting.listener.PieRadarChartTouchListener; import com.github.mikephil.charting.utils.MPPointF; -import com.github.mikephil.charting.utils.Utils; import com.github.mikephil.charting.utils.UtilsKtKt; /** @@ -341,7 +340,7 @@ public float distanceToCenter(float x, float y) { */ public void setRotationAngle(float angle) { mRawRotationAngle = angle; - mRotationAngle = Utils.getNormalizedAngle(mRawRotationAngle); + mRotationAngle = UtilsKtKt.getNormalizedAngle(mRawRotationAngle); } /** diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/RadarChart.java b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/RadarChart.java index 15304dadd..5970841b9 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/RadarChart.java +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/RadarChart.java @@ -14,7 +14,6 @@ import com.github.mikephil.charting.renderer.RadarChartRenderer; import com.github.mikephil.charting.renderer.XAxisRendererRadarChart; import com.github.mikephil.charting.renderer.YAxisRendererRadarChart; -import com.github.mikephil.charting.utils.Utils; import com.github.mikephil.charting.utils.UtilsKtKt; import java.util.List; @@ -207,7 +206,7 @@ public List getLayerColorList() { public int getIndexForAngle(float angle) { // take the current angle of the chart into consideration - float a = Utils.getNormalizedAngle(angle - getRotationAngle()); + float a = UtilsKtKt.getNormalizedAngle(angle - getRotationAngle()); float sliceangle = getSliceAngle(); diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/components/Legend.java b/MPChartLib/src/main/java/com/github/mikephil/charting/components/Legend.java index 271b4071f..12a5946a6 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/components/Legend.java +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/components/Legend.java @@ -694,7 +694,7 @@ else if (wasStacked) { // grouped forms have null labels if (label != null) { - mCalculatedLabelSizes.add(Utils.calcTextSize(labelpaint, label)); + mCalculatedLabelSizes.add(CanvasUtilsKt.calcTextSize(labelpaint, label)); requiredWidth += drawingForm ? formToTextSpace + formSize : 0.f; requiredWidth += mCalculatedLabelSizes.get(i).width; } else { diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/data/BaseDataSet.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/data/BaseDataSet.kt index 5b79ed31d..6061cd003 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/data/BaseDataSet.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/data/BaseDataSet.kt @@ -11,8 +11,8 @@ import com.github.mikephil.charting.formatter.IValueFormatter import com.github.mikephil.charting.interfaces.datasets.IDataSet 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.convertDpToPixel +import com.github.mikephil.charting.utils.getDefaultValueFormatter /** * This is the base dataset of all DataSets. It's purpose is to implement critical methods 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 5bddd88fa..b7ff4fa2e 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 @@ -19,6 +19,7 @@ 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.calcTextSize import com.github.mikephil.charting.utils.calcTextWidth import com.github.mikephil.charting.utils.convertDpToPixel import com.github.mikephil.charting.utils.drawXAxisValue @@ -73,7 +74,7 @@ open class XAxisRenderer( paintAxisLabels.typeface = xAxis.typeface paintAxisLabels.textSize = xAxis.textSize - val labelSize = Utils.calcTextSize(paintAxisLabels, longest) + val labelSize = paintAxisLabels.calcTextSize(longest) val labelWidth = labelSize.width val labelHeight = paintAxisLabels.calcTextHeight("Q").toFloat() 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 118e49373..395fa0f93 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 @@ -16,6 +16,7 @@ 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.calcTextSize import com.github.mikephil.charting.utils.convertDpToPixel import kotlin.math.roundToInt @@ -58,7 +59,7 @@ open class XAxisRendererHorizontalBarChart( val longest = xAxis.longestLabel - val labelSize = Utils.calcTextSize(paintAxisLabels, longest) + val labelSize = paintAxisLabels.calcTextSize(longest) val labelWidth = (labelSize.width + xAxis.xOffset * 3.5f).toInt().toFloat() val labelHeight = labelSize.height 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 301996ade..ba7337a63 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,36 @@ val FDEG2RAD: Float = (Math.PI.toFloat() / 180f) */ fun Paint.calcTextWidth(demoText: String?) = this.measureText(demoText).toInt() +private val calcTextSizeRect = Rect() + +/** + * calculates the approximate size of a text, depending on a demo text + * avoid repeated calls (e.g. inside drawing methods) + * + * @param paint + * @param demoText + * @param outputFSize An output variable, modified by the function. + */ +fun Paint.calcTextSize(demoText: String, outputFSize: FSize) { + val r = calcTextSizeRect + r.set(0, 0, 0, 0) + this.getTextBounds(demoText, 0, demoText.length, r) + outputFSize.width = r.width().toFloat() + outputFSize.height = r.height().toFloat() +} + +/** + * Returns a recyclable FSize instance. + * calculates the approximate size of a text, depending on a demo text + * avoid repeated calls (e.g. inside drawing methods) + * @return A Recyclable FSize instance + */ +fun Paint.calcTextSize(demoText: String): FSize { + val result = FSize.getInstance(0f, 0f) + this.calcTextSize(demoText, result) + return result +} + /** * calculates the approximate height of a text, depending on a demo text * avoid repeated calls (e.g. inside drawing methods) 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 5cf4119cb..7894668eb 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 @@ -67,42 +67,6 @@ public static float getLineSpacing(Paint paint, Paint.FontMetrics fontMetrics) { return fontMetrics.ascent - fontMetrics.top + fontMetrics.bottom; } - /** - * Returns a recyclable FSize instance. - * calculates the approximate size of a text, depending on a demo text - * avoid repeated calls (e.g. inside drawing methods) - * - * @param paint - * @param demoText - * @return A Recyclable FSize instance - */ - public static FSize calcTextSize(Paint paint, String demoText) { - - FSize result = FSize.getInstance(0, 0); - calcTextSize(paint, demoText, result); - return result; - } - - private static final Rect mCalcTextSizeRect = new Rect(); - - /** - * calculates the approximate size of a text, depending on a demo text - * avoid repeated calls (e.g. inside drawing methods) - * - * @param paint - * @param demoText - * @param outputFSize An output variable, modified by the function. - */ - public static void calcTextSize(Paint paint, String demoText, FSize outputFSize) { - - Rect r = mCalcTextSizeRect; - r.set(0, 0, 0, 0); - paint.getTextBounds(demoText, 0, demoText.length(), r); - outputFSize.width = r.width(); - outputFSize.height = r.height(); - - } - /** * Math.pow(...) is very expensive, so avoid calling it and create it * yourself. @@ -111,38 +75,6 @@ public static void calcTextSize(Paint paint, String demoText, FSize outputFSize) 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000 }; - private static final IValueFormatter mDefaultValueFormatter = generateDefaultValueFormatter(); - - private static IValueFormatter generateDefaultValueFormatter() { - return new DefaultValueFormatter(1); - } - - /// - returns: The default value formatter used for all chart components that needs a default - public static IValueFormatter getDefaultValueFormatter() { - return mDefaultValueFormatter; - } - - /** - * Returns a recyclable MPPointF instance. - * Calculates the position around a center point, depending on the distance - * from the center, and the angle of the position around the center. - * - * @param center - * @param dist - * @param angle in degrees, converted to radians internally - * @return - */ - public static MPPointF getPosition(MPPointF center, float dist, float angle) { - MPPointF p = MPPointF.getInstance(0, 0); - getPosition(center, dist, angle, p); - return p; - } - - public static void getPosition(MPPointF center, float dist, float angle, MPPointF outputPoint) { - outputPoint.x = (float) (center.x + dist * Math.cos(Math.toRadians(angle))); - outputPoint.y = (float) (center.y + dist * Math.sin(Math.toRadians(angle))); - } - public static void velocityTrackerPointerUpCleanUpIfNecessary(MotionEvent ev, VelocityTracker tracker) { // Check the dot product of current velocities. @@ -169,17 +101,6 @@ public static void velocityTrackerPointerUpCleanUpIfNecessary(MotionEvent ev, Ve } } - /** - * returns an angle between 0.f < 360.f (not less than zero, less than 360) - */ - public static float getNormalizedAngle(float angle) { - while (angle < 0.f) { - angle += 360.f; - } - - return angle % 360.f; - } - private static final Rect mDrawableBoundsCache = new Rect(); public static void drawImage(Canvas canvas, Drawable drawable, int x, int y) { diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/UtilsKt.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/UtilsKt.kt index 002a393fe..9998d2758 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/UtilsKt.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/UtilsKt.kt @@ -1,12 +1,11 @@ package com.github.mikephil.charting.utils import android.content.Context -import android.graphics.Paint import android.os.Build import android.util.DisplayMetrics import android.util.Log -import android.util.Log.i import android.view.ViewConfiguration +import com.github.mikephil.charting.formatter.DefaultValueFormatter import java.lang.Double import kotlin.Boolean import kotlin.Char @@ -14,6 +13,7 @@ import kotlin.CharArray import kotlin.Float import kotlin.Int import kotlin.String +import kotlin.apply import kotlin.code import kotlin.math.ceil import kotlin.math.cos @@ -21,7 +21,6 @@ import kotlin.math.log10 import kotlin.math.pow import kotlin.math.roundToInt import kotlin.math.sin -import kotlin.time.times var metrics: DisplayMetrics? = null var minimumFlingVelocity = 0 @@ -89,6 +88,8 @@ fun getSDKInt() = Build.VERSION.SDK_INT fun Context.convertDpToPixel(dp: Float) = dp * this.resources.displayMetrics.density +fun getDefaultValueFormatter() = DefaultValueFormatter(1) + fun MPPointF.getPosition(dist: Float, angle: Float) :MPPointF { return MPPointF().apply { x = (this.x + dist * cos(Math.toRadians(angle.toDouble()))).toFloat() @@ -96,6 +97,17 @@ fun MPPointF.getPosition(dist: Float, angle: Float) :MPPointF { } } +/** + * returns an angle between 0.f < 360.f (not less than zero, less than 360) + */ +fun Float.getNormalizedAngle(): Float { + var angle = this + while (angle < 0f) { + angle += 360f + } + return angle % 360f +} + fun Float.formatNumber(digitCount: Int, separateThousands: Boolean, separateChar: Char = '.'): String { var number = this var digitCount = digitCount