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 2305d9ea4..d86c2e64a 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.convertDpToPixel import java.lang.Float import kotlin.Array @@ -224,7 +225,7 @@ class Legend() : ComponentBase() { val label = entry.label if (label == null) 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 1093f46b8..fe9d1ded6 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.calcTextHeight 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. @@ -236,7 +237,7 @@ 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 @@ -246,7 +247,7 @@ class YAxis : AxisBase { 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)) @@ -257,24 +258,20 @@ 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 + return p.calcTextHeight(label).toFloat() + yOffset * 2f } /** * Returns true if this axis needs horizontal offset, false if no offset is needed. */ fun needsOffset(): Boolean { - return if (isEnabled && isDrawLabelsEnabled && this.labelPosition == YAxisLabelPosition.OUTSIDE_CHART) - true - else - 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/BarChartRenderer.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/BarChartRenderer.kt index 6152528ec..fa00974f6 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 @@ -278,7 +279,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 2213186d3..c8634368b 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 @@ -98,7 +99,7 @@ open class BubbleChartRenderer( if (isDrawingValuesAllowed(dataProvider)) { val dataSets = bubbleData.dataSets - val lineHeight = Utils.calcTextHeight(paintValues, "1").toFloat() + val lineHeight = paintValues.calcTextHeight("1").toFloat() dataSets?.let { for (i in it.indices) { 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 5c38011ef..eda363638 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.convertDpToPixel import kotlin.math.ceil import kotlin.math.min @@ -186,7 +187,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 8b9f7c6c4..f9df39137 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.convertDpToPixel import java.util.Collections import kotlin.math.min @@ -209,7 +210,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 f0960bece..8e0a98fad 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 @@ -405,7 +406,7 @@ open class PieChartRenderer( // apply the text-styling defined by the DataSet applyValueTextStyle(dataSet) - val lineHeight = (Utils.calcTextHeight(paintValues, "Q") + 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 b1238eb03..350cbe876 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,7 +18,9 @@ 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 com.github.mikephil.charting.utils.drawXAxisValue import kotlin.math.roundToInt open class XAxisRenderer( @@ -72,7 +74,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, @@ -102,21 +104,25 @@ open class XAxisRenderer( pointF.y = 1.0f drawLabels(canvas, viewPortHandler.contentTop() - yOffset, pointF) } + XAxisPosition.TOP_INSIDE -> { pointF.x = 0.5f pointF.y = 1.0f drawLabels(canvas, viewPortHandler.contentTop() + yOffset + xAxis.mLabelHeight, pointF) } + XAxisPosition.BOTTOM -> { pointF.x = 0.5f pointF.y = 0.0f drawLabels(canvas, viewPortHandler.contentBottom() + yOffset, pointF) } + XAxisPosition.BOTTOM_INSIDE -> { pointF.x = 0.5f pointF.y = 0.0f drawLabels(canvas, viewPortHandler.contentBottom() - yOffset - xAxis.mLabelHeight, pointF) } + else -> { // BOTH SIDED pointF.x = 0.5f pointF.y = 1.0f @@ -222,7 +228,7 @@ open class XAxisRenderer( } protected fun drawLabel(canvas: Canvas, formattedLabel: String?, x: Float, y: Float, anchor: MPPointF, angleDegrees: Float) { - formattedLabel?.let { Utils.drawXAxisValue(canvas, it, x, y, paintAxisLabels, anchor, angleDegrees) } + formattedLabel?.let { canvas.drawXAxisValue(it, x, y, paintAxisLabels, anchor, angleDegrees) } } protected open var renderGridLinesPath: Path = Path() @@ -384,7 +390,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, @@ -399,7 +405,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 b6f60b3ea..27f925ab3 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 @@ -253,7 +254,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 80e202f40..70041fed7 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 @@ -317,7 +317,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 @@ -436,7 +436,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 ef610f97e..1f0465664 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") @@ -76,7 +76,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 @@ -283,7 +283,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) } @@ -293,7 +293,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 982c806c3..b8d9c05ee 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 @@ -5,16 +5,12 @@ import android.graphics.Paint import android.graphics.Paint.Align import android.graphics.Rect import android.graphics.drawable.Drawable -import android.text.StaticLayout -import android.text.TextPaint import com.github.mikephil.charting.utils.Utils.FDEG2RAD import kotlin.math.abs import kotlin.math.cos import kotlin.math.sin private val mDrawableBoundsCache = Rect() -val DEG2RAD: Double = (Math.PI / 180.0) -val FDEG2RAD: Float = (Math.PI.toFloat() / 180f) /** * Utilities class that has some helper methods. Needs to be initialized by @@ -117,84 +113,6 @@ fun Canvas.drawXAxisValue( paint.textAlign = originalTextAlign } -fun Canvas.drawMultilineText( - textLayout: StaticLayout, - x: Float, y: Float, - paint: TextPaint, - anchor: MPPointF, angleDegrees: Float -) { - var drawOffsetX = 0f - var drawOffsetY = 0f - val drawWidth: Float - val drawHeight: Float - - val lineHeight = paint.getFontMetrics(mFontMetricsBuffer) - - drawWidth = textLayout.width.toFloat() - drawHeight = textLayout.lineCount * lineHeight - - // Android sometimes has pre-padding - drawOffsetX -= mDrawTextRectBuffer.left.toFloat() - - // 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 - val originalTextAlign = paint.textAlign - paint.textAlign = Align.LEFT - - if (angleDegrees != 0f) { - // Move the text drawing rect in a way that it always rotates around its center - - drawOffsetX -= drawWidth * 0.5f - drawOffsetY -= drawHeight * 0.5f - - var translateX = x - var translateY = y - - // Move the "outer" rect relative to the anchor, assuming its centered - if (anchor.x != 0.5f || anchor.y != 0.5f) { - val rotatedSize = getSizeOfRotatedRectangleByDegrees( - drawWidth, - drawHeight, - angleDegrees - ) - - translateX -= rotatedSize.width * (anchor.x - 0.5f) - translateY -= rotatedSize.height * (anchor.y - 0.5f) - FSize.recycleInstance(rotatedSize) - } - - this.save() - this.translate(translateX, translateY) - this.rotate(angleDegrees) - - this.translate(drawOffsetX, drawOffsetY) - textLayout.draw(this) - - this.restore() - } else { - if (anchor.x != 0f || anchor.y != 0f) { - drawOffsetX -= drawWidth * anchor.x - drawOffsetY -= drawHeight * anchor.y - } - - drawOffsetX += x - drawOffsetY += y - - this.save() - - this.translate(drawOffsetX, drawOffsetY) - textLayout.draw(this) - - this.restore() - } - - paint.textAlign = originalTextAlign -} - /** * Returns a recyclable FSize instance. * Represents size of a rotated rectangle by degrees. diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/SaveUtils.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/SaveUtils.kt index 607019e0a..e8ce18097 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/SaveUtils.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/SaveUtils.kt @@ -56,7 +56,7 @@ object SaveUtils { // restrain quality var fileName = fileName var quality = quality - if (quality < 0 || quality > 100) { + if (quality !in 0..100) { quality = 50 } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/Utils.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/Utils.kt index a90d8c762..07b211871 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/Utils.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/Utils.kt @@ -3,7 +3,6 @@ package com.github.mikephil.charting.utils import android.content.Context import android.graphics.Canvas import android.graphics.Paint -import android.graphics.Paint.Align import android.graphics.Rect import android.graphics.drawable.Drawable import android.view.MotionEvent @@ -11,9 +10,7 @@ import android.view.VelocityTracker import android.view.ViewConfiguration import com.github.mikephil.charting.formatter.DefaultValueFormatter import com.github.mikephil.charting.formatter.IValueFormatter -import com.github.mikephil.charting.utils.FSize.Companion.recycleInstance import com.github.mikephil.charting.utils.MPPointF.Companion.instance -import java.lang.Double import java.lang.Float import kotlin.Int import kotlin.IntArray @@ -27,21 +24,14 @@ import kotlin.math.sin /** * Utilities class that has some helper methods. Needs to be initialized by * calling Utils.init(...) before usage. Inside the Chart.init() method, this is - * done, if the Utils are used before that, Utils.init(...) needs to be called - * manually. - * - * @author Philipp Jahoda + * done, if the Utils are used before that, Utils.init(...) needs to be called manually. */ object Utils { var minimumFlingVelocity: Int = 50 var maximumFlingVelocity: Int = 8000 - val DEG2RAD: kotlin.Double = (Math.PI / 180.0) - val FDEG2RAD: kotlin.Float = (Math.PI.toFloat() / 180f) + const val DEG2RAD: Double = (Math.PI / 180.0) + const val FDEG2RAD: kotlin.Float = (Math.PI.toFloat() / 180f) - @Suppress("unused") - val DOUBLE_EPSILON: kotlin.Double = Double.longBitsToDouble(1) - - @Suppress("unused") val FLOAT_EPSILON: kotlin.Float = Float.intBitsToFloat(1) /** @@ -62,19 +52,6 @@ object Utils { return paint.measureText(demoText).toInt() } - private val mCalcTextHeightRect = Rect() - - /** - * calculates the approximate height of a text, depending on a demo text - * avoid repeated calls (e.g. inside drawing methods) - */ - fun calcTextHeight(paint: Paint, demoText: String): Int { - val r = mCalcTextHeightRect - r.set(0, 0, 0, 0) - paint.getTextBounds(demoText, 0, demoText.length, r) - return r.height() - } - private val mFontMetrics = Paint.FontMetrics() fun getLineHeight(paint: Paint): kotlin.Float { @@ -231,76 +208,6 @@ object Utils { canvas.restoreToCount(saveId) } - private val mDrawTextRectBuffer = Rect() - private val mFontMetricsBuffer = Paint.FontMetrics() - - fun drawXAxisValue( - canvas: Canvas, text: String, x: kotlin.Float, y: kotlin.Float, - paint: Paint, - anchor: MPPointF, angleDegrees: kotlin.Float - ) { - var drawOffsetX = 0f - var drawOffsetY = 0f - - val lineHeight = paint.getFontMetrics(mFontMetricsBuffer) - paint.getTextBounds(text, 0, text.length, mDrawTextRectBuffer) - - // Android sometimes has pre-padding - drawOffsetX -= mDrawTextRectBuffer.left.toFloat() - - // Android does not snap the bounds to line boundaries, - // and draws from bottom to top. - // And we want to normalize it. - drawOffsetY -= mFontMetricsBuffer.ascent - - // To have a consistent point of reference, we always draw left-aligned - val originalTextAlign = paint.textAlign - paint.textAlign = Align.LEFT - - if (angleDegrees != 0f) { - // Move the text drawing rect in a way that it always rotates around its center - - drawOffsetX -= mDrawTextRectBuffer.width() * 0.5f - drawOffsetY -= lineHeight * 0.5f - - var translateX = x - var translateY = y - - // Move the "outer" rect relative to the anchor, assuming its centered - if (anchor.x != 0.5f || anchor.y != 0.5f) { - val rotatedSize = getSizeOfRotatedRectangleByDegrees( - mDrawTextRectBuffer.width().toFloat(), - lineHeight, - angleDegrees - ) - - translateX -= rotatedSize.width * (anchor.x - 0.5f) - translateY -= rotatedSize.height * (anchor.y - 0.5f) - recycleInstance(rotatedSize) - } - - canvas.save() - canvas.translate(translateX, translateY) - canvas.rotate(angleDegrees) - - canvas.drawText(text, drawOffsetX, drawOffsetY, paint) - - canvas.restore() - } else { - if (anchor.x != 0f || anchor.y != 0f) { - drawOffsetX -= mDrawTextRectBuffer.width() * anchor.x - drawOffsetY -= lineHeight * anchor.y - } - - drawOffsetX += x - drawOffsetY += y - - canvas.drawText(text, drawOffsetX, drawOffsetY, paint) - } - - paint.textAlign = originalTextAlign - } - /** * Returns a recyclable FSize instance. * Represents size of a rotated rectangle by degrees. 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 cf3dd707f..494c403d5 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,6 +1,8 @@ package com.github.mikephil.charting.utils import android.content.Context +import android.graphics.Paint +import android.graphics.Rect import android.os.Build import android.util.DisplayMetrics import android.view.ViewConfiguration @@ -153,3 +155,16 @@ fun Float.formatNumber(digitCount: Int, separateThousands: Boolean, separateChar // use this instead of "new String(...)" because of issue < Android 4.0 return String(out, start, out.size - start) } + +private val mCalcTextHeightRect = Rect() + +/** + * 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 r = mCalcTextHeightRect + r.set(0, 0, 0, 0) + this.getTextBounds(demoText, 0, demoText.length, r) + return r.height() +}