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..d0664d1bb 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.convertDpToPixel +import com.github.mikephil.charting.utils.drawXAxisValue import kotlin.math.roundToInt open class XAxisRenderer( @@ -102,21 +103,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 +227,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() 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/Utils.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/Utils.kt index a90d8c762..59fd3c781 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) /** @@ -231,76 +221,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.