From bdf42323adf7199104fcf1634052583f4eac7844 Mon Sep 17 00:00:00 2001 From: Hannes Achleitner Date: Mon, 22 Dec 2025 08:55:34 +0100 Subject: [PATCH] Kotlin Transformer --- .../mikephil/charting/utils/Transformer.java | 414 ------------------ .../mikephil/charting/utils/Transformer.kt | 392 +++++++++++++++++ .../utils/TransformerHorizontalBarChart.java | 33 -- .../utils/TransformerHorizontalBarChart.kt | 26 ++ 4 files changed, 418 insertions(+), 447 deletions(-) delete mode 100644 MPChartLib/src/main/java/com/github/mikephil/charting/utils/Transformer.java create mode 100644 MPChartLib/src/main/java/com/github/mikephil/charting/utils/Transformer.kt delete mode 100644 MPChartLib/src/main/java/com/github/mikephil/charting/utils/TransformerHorizontalBarChart.java create mode 100644 MPChartLib/src/main/java/com/github/mikephil/charting/utils/TransformerHorizontalBarChart.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/Transformer.java b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/Transformer.java deleted file mode 100644 index f42ac6187..000000000 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/Transformer.java +++ /dev/null @@ -1,414 +0,0 @@ - -package com.github.mikephil.charting.utils; - -import android.graphics.Matrix; -import android.graphics.Path; -import android.graphics.RectF; - -import com.github.mikephil.charting.data.CandleEntry; -import com.github.mikephil.charting.data.Entry; -import com.github.mikephil.charting.interfaces.datasets.IBubbleDataSet; -import com.github.mikephil.charting.interfaces.datasets.ICandleDataSet; -import com.github.mikephil.charting.interfaces.datasets.ILineDataSet; -import com.github.mikephil.charting.interfaces.datasets.IScatterDataSet; - -import java.util.List; - -/** - * Transformer class that contains all matrices and is responsible for - * transforming values into pixels on the screen and backwards. - * - * @author Philipp Jahoda - */ -public class Transformer { - - /** - * matrix to map the values to the screen pixels - */ - protected Matrix mMatrixValueToPx = new Matrix(); - - /** - * matrix for handling the different offsets of the chart - */ - protected Matrix mMatrixOffset = new Matrix(); - - protected ViewPortHandler mViewPortHandler; - - public Transformer(ViewPortHandler viewPortHandler) { - this.mViewPortHandler = viewPortHandler; - } - - /** - * Prepares the matrix that transforms values to pixels. Calculates the - * scale factors from the charts size and offsets. - */ - public void prepareMatrixValuePx(float xChartMin, float deltaX, float deltaY, float yChartMin) { - - float scaleX = mViewPortHandler.contentWidth() / deltaX; - float scaleY = mViewPortHandler.contentHeight() / deltaY; - - if (Float.isInfinite(scaleX)) { - scaleX = 0; - } - if (Float.isInfinite(scaleY)) { - scaleY = 0; - } - - // setup all matrices - mMatrixValueToPx.reset(); - mMatrixValueToPx.postTranslate(-xChartMin, -yChartMin); - mMatrixValueToPx.postScale(scaleX, -scaleY); - } - - /** - * Prepares the matrix that contains all offsets. - */ - public void prepareMatrixOffset(boolean inverted) { - - mMatrixOffset.reset(); - - // offset.postTranslate(mOffsetLeft, getHeight() - mOffsetBottom); - - if (!inverted) - mMatrixOffset.postTranslate(mViewPortHandler.offsetLeft(), - mViewPortHandler.getChartHeight() - mViewPortHandler.offsetBottom()); - else { - mMatrixOffset - .setTranslate(mViewPortHandler.offsetLeft(), -mViewPortHandler.offsetTop()); - mMatrixOffset.postScale(1.0f, -1.0f); - } - } - - protected float[] valuePointsForGenerateTransformedValuesScatter = new float[1]; - - /** - * Transforms an List of Entry into a float array containing the x and - * y values transformed with all matrices for the SCATTERCHART. - */ - public float[] generateTransformedValuesScatter(IScatterDataSet data, float phaseX, - float phaseY, int from, int to) { - - final int count = (int) ((to - from) * phaseX + 1) * 2; - - if (valuePointsForGenerateTransformedValuesScatter.length != count) { - valuePointsForGenerateTransformedValuesScatter = new float[count]; - } - float[] valuePoints = valuePointsForGenerateTransformedValuesScatter; - - for (int j = 0; j < count; j += 2) { - - Entry e = data.getEntryForIndex(j / 2 + from); - - if (e != null) { - valuePoints[j] = e.getX(); - valuePoints[j + 1] = e.getY() * phaseY; - } else { - valuePoints[j] = 0; - valuePoints[j + 1] = 0; - } - } - - getValueToPixelMatrix().mapPoints(valuePoints); - - return valuePoints; - } - - protected float[] valuePointsForGenerateTransformedValuesBubble = new float[1]; - - /** - * Transforms an List of Entry into a float array containing the x and - * y values transformed with all matrices for the BUBBLECHART. - */ - public float[] generateTransformedValuesBubble(IBubbleDataSet data, float phaseY, int from, int to) { - - final int count = (to - from + 1) * 2; // (int) Math.ceil((to - from) * phaseX) * 2; - - if (valuePointsForGenerateTransformedValuesBubble.length != count) { - valuePointsForGenerateTransformedValuesBubble = new float[count]; - } - float[] valuePoints = valuePointsForGenerateTransformedValuesBubble; - - for (int j = 0; j < count; j += 2) { - - Entry e = data.getEntryForIndex(j / 2 + from); - - if (e != null) { - valuePoints[j] = e.getX(); - valuePoints[j + 1] = e.getY() * phaseY; - } else { - valuePoints[j] = 0; - valuePoints[j + 1] = 0; - } - } - - getValueToPixelMatrix().mapPoints(valuePoints); - - return valuePoints; - } - - protected float[] valuePointsForGenerateTransformedValuesLine = new float[1]; - - /** - * Transforms an List of Entry into a float array containing the x and - * y values transformed with all matrices for the LINECHART. - */ - public float[] generateTransformedValuesLine(ILineDataSet data, - float phaseX, float phaseY, - int min, int max) { - - int count = ((int) ((max - min) * phaseX) + 1) * 2; - if (count < 0) - count = 0; - - if (valuePointsForGenerateTransformedValuesLine.length != count) { - valuePointsForGenerateTransformedValuesLine = new float[count]; - } - float[] valuePoints = valuePointsForGenerateTransformedValuesLine; - - for (int j = 0; j < count; j += 2) { - - Entry e = data.getEntryForIndex(j / 2 + min); - - if (e != null) { - valuePoints[j] = e.getX(); - valuePoints[j + 1] = e.getY() * phaseY; - } else { - valuePoints[j] = 0; - valuePoints[j + 1] = 0; - } - } - - getValueToPixelMatrix().mapPoints(valuePoints); - - return valuePoints; - } - - protected float[] valuePointsForGenerateTransformedValuesCandle = new float[1]; - - /** - * Transforms an List of Entry into a float array containing the x and - * y values transformed with all matrices for the CANDLESTICKCHART. - */ - public float[] generateTransformedValuesCandle(ICandleDataSet data, - float phaseX, float phaseY, int from, int to) { - - final int count = (int) ((to - from) * phaseX + 1) * 2; - - if (valuePointsForGenerateTransformedValuesCandle.length != count) { - valuePointsForGenerateTransformedValuesCandle = new float[count]; - } - float[] valuePoints = valuePointsForGenerateTransformedValuesCandle; - - for (int j = 0; j < count; j += 2) { - - CandleEntry e = data.getEntryForIndex(j / 2 + from); - - if (e != null) { - valuePoints[j] = e.getX(); - valuePoints[j + 1] = e.getHigh() * phaseY; - } else { - valuePoints[j] = 0; - valuePoints[j + 1] = 0; - } - } - - getValueToPixelMatrix().mapPoints(valuePoints); - - return valuePoints; - } - - /** - * transform a path with all the given matrices VERY IMPORTANT: keep order - * to value-touch-offset - */ - public void pathValueToPixel(Path path) { - - path.transform(mMatrixValueToPx); - path.transform(mViewPortHandler.getMatrixTouch()); - path.transform(mMatrixOffset); - } - - /** - * Transforms multiple paths will all matrices. - */ - public void pathValuesToPixel(List paths) { - - for (int i = 0; i < paths.size(); i++) { - pathValueToPixel(paths.get(i)); - } - } - - /** - * Transform an array of points with all matrices. VERY IMPORTANT: Keep - * matrix order "value-touch-offset" when transforming. - */ - public void pointValuesToPixel(float[] pts) { - - mMatrixValueToPx.mapPoints(pts); - mViewPortHandler.getMatrixTouch().mapPoints(pts); - mMatrixOffset.mapPoints(pts); - } - - /** - * Transform a rectangle with all matrices. - */ - public void rectValueToPixel(RectF r) { - - mMatrixValueToPx.mapRect(r); - mViewPortHandler.getMatrixTouch().mapRect(r); - mMatrixOffset.mapRect(r); - } - - /** - * Transform a rectangle with all matrices with potential animation phases. - */ - public void rectToPixelPhase(RectF r, float phaseY) { - - // multiply the height of the rect with the phase - r.top *= phaseY; - r.bottom *= phaseY; - - mMatrixValueToPx.mapRect(r); - mViewPortHandler.getMatrixTouch().mapRect(r); - mMatrixOffset.mapRect(r); - } - - public void rectToPixelPhaseHorizontal(RectF r, float phaseY) { - - // multiply the height of the rect with the phase - r.left *= phaseY; - r.right *= phaseY; - - mMatrixValueToPx.mapRect(r); - mViewPortHandler.getMatrixTouch().mapRect(r); - mMatrixOffset.mapRect(r); - } - - /** - * Transform a rectangle with all matrices with potential animation phases. - */ - public void rectValueToPixelHorizontal(RectF r) { - - mMatrixValueToPx.mapRect(r); - mViewPortHandler.getMatrixTouch().mapRect(r); - mMatrixOffset.mapRect(r); - } - - /** - * Transform a rectangle with all matrices with potential animation phases. - */ - public void rectValueToPixelHorizontal(RectF r, float phaseY) { - - // multiply the height of the rect with the phase - r.left *= phaseY; - r.right *= phaseY; - - mMatrixValueToPx.mapRect(r); - mViewPortHandler.getMatrixTouch().mapRect(r); - mMatrixOffset.mapRect(r); - } - - /** - * transforms multiple rects with all matrices - */ - public void rectValuesToPixel(List rects) { - - Matrix m = getValueToPixelMatrix(); - - for (int i = 0; i < rects.size(); i++) - m.mapRect(rects.get(i)); - } - - protected Matrix mPixelToValueMatrixBuffer = new Matrix(); - - /** - * Transforms the given array of touch positions (pixels) (x, y, x, y, ...) - * into values on the chart. - */ - public void pixelsToValue(float[] pixels) { - - Matrix tmp = mPixelToValueMatrixBuffer; - tmp.reset(); - - // invert all matrixes to convert back to the original value - mMatrixOffset.invert(tmp); - tmp.mapPoints(pixels); - - mViewPortHandler.getMatrixTouch().invert(tmp); - tmp.mapPoints(pixels); - - mMatrixValueToPx.invert(tmp); - tmp.mapPoints(pixels); - } - - /** - * buffer for performance - */ - float[] ptsBuffer = new float[2]; - - /** - * Returns a recyclable MPPointD instance. - * returns the x and y values in the chart at the given touch point - * (encapsulated in a MPPointD). This method transforms pixel coordinates to - * coordinates / values in the chart. This is the opposite method to - * getPixelForValues(...). - */ - public MPPointD getValuesByTouchPoint(float x, float y) { - - MPPointD result = MPPointD.Companion.getInstance(0, 0); - getValuesByTouchPoint(x, y, result); - return result; - } - - public void getValuesByTouchPoint(float x, float y, MPPointD outputPoint) { - - ptsBuffer[0] = x; - ptsBuffer[1] = y; - - pixelsToValue(ptsBuffer); - - outputPoint.setX(ptsBuffer[0]); - outputPoint.setY(ptsBuffer[1]); - } - - /** - * Returns a recyclable MPPointD instance. - * Returns the x and y coordinates (pixels) for a given x and y value in the chart. - */ - public MPPointD getPixelForValues(float x, float y) { - - ptsBuffer[0] = x; - ptsBuffer[1] = y; - - pointValuesToPixel(ptsBuffer); - - double xPx = ptsBuffer[0]; - double yPx = ptsBuffer[1]; - - return MPPointD.Companion.getInstance(xPx, yPx); - } - - public Matrix getValueMatrix() { - return mMatrixValueToPx; - } - - public Matrix getOffsetMatrix() { - return mMatrixOffset; - } - - private final Matrix mMBuffer1 = new Matrix(); - - public Matrix getValueToPixelMatrix() { - mMBuffer1.set(mMatrixValueToPx); - mMBuffer1.postConcat(mViewPortHandler.getMatrixTouch()); - mMBuffer1.postConcat(mMatrixOffset); - return mMBuffer1; - } - - private final Matrix mMBuffer2 = new Matrix(); - - public Matrix getPixelToValueMatrix() { - getValueToPixelMatrix().invert(mMBuffer2); - return mMBuffer2; - } -} diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/Transformer.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/Transformer.kt new file mode 100644 index 000000000..09c428bf4 --- /dev/null +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/Transformer.kt @@ -0,0 +1,392 @@ +package com.github.mikephil.charting.utils + +import android.graphics.Matrix +import android.graphics.Path +import android.graphics.RectF +import com.github.mikephil.charting.data.Entry +import com.github.mikephil.charting.interfaces.datasets.IBubbleDataSet +import com.github.mikephil.charting.interfaces.datasets.ICandleDataSet +import com.github.mikephil.charting.interfaces.datasets.ILineDataSet +import com.github.mikephil.charting.interfaces.datasets.IScatterDataSet +import com.github.mikephil.charting.utils.MPPointD.Companion.getInstance +import kotlin.Boolean +import kotlin.FloatArray +import kotlin.Int + +/** + * Transformer class that contains all matrices and is responsible for + * transforming values into pixels on the screen and backwards. + */ +open class Transformer(@JvmField protected var viewPortHandler: ViewPortHandler) { + /** + * matrix to map the values to the screen pixels + */ + var valueMatrix: Matrix = Matrix() + protected set + + /** + * matrix for handling the different offsets of the chart + */ + var offsetMatrix: Matrix = Matrix() + protected set + + /** + * Prepares the matrix that transforms values to pixels. Calculates the + * scale factors from the charts size and offsets. + */ + fun prepareMatrixValuePx(xChartMin: Float, deltaX: Float, deltaY: Float, yChartMin: Float) { + var scaleX = viewPortHandler.contentWidth() / deltaX + var scaleY = viewPortHandler.contentHeight() / deltaY + + if (java.lang.Float.isInfinite(scaleX)) { + scaleX = 0f + } + if (java.lang.Float.isInfinite(scaleY)) { + scaleY = 0f + } + + // setup all matrices + valueMatrix.reset() + valueMatrix.postTranslate(-xChartMin, -yChartMin) + valueMatrix.postScale(scaleX, -scaleY) + } + + /** + * Prepares the matrix that contains all offsets. + */ + open fun prepareMatrixOffset(inverted: Boolean) { + offsetMatrix.reset() + + // offset.postTranslate(mOffsetLeft, getHeight() - mOffsetBottom); + if (!inverted) offsetMatrix.postTranslate( + viewPortHandler.offsetLeft(), + viewPortHandler.chartHeight - viewPortHandler.offsetBottom() + ) + else { + offsetMatrix + .setTranslate(viewPortHandler.offsetLeft(), -viewPortHandler.offsetTop()) + offsetMatrix.postScale(1.0f, -1.0f) + } + } + + protected var valuePointsForGenerateTransformedValuesScatter: FloatArray = FloatArray(1) + + /** + * Transforms an List of Entry into a float array containing the x and + * y values transformed with all matrices for the SCATTERCHART. + */ + fun generateTransformedValuesScatter( + data: IScatterDataSet, phaseX: Float, + phaseY: Float, from: Int, to: Int + ): FloatArray { + val count = ((to - from) * phaseX + 1).toInt() * 2 + + if (valuePointsForGenerateTransformedValuesScatter.size != count) { + valuePointsForGenerateTransformedValuesScatter = FloatArray(count) + } + val valuePoints = valuePointsForGenerateTransformedValuesScatter + + var j = 0 + while (j < count) { + val e = data.getEntryForIndex(j / 2 + from) + + if (e != null) { + valuePoints[j] = e.x + valuePoints[j + 1] = e.y * phaseY + } else { + valuePoints[j] = 0f + valuePoints[j + 1] = 0f + } + j += 2 + } + + this.valueToPixelMatrix.mapPoints(valuePoints) + + return valuePoints + } + + protected var valuePointsForGenerateTransformedValuesBubble: FloatArray = FloatArray(1) + + /** + * Transforms an List of Entry into a float array containing the x and + * y values transformed with all matrices for the BUBBLECHART. + */ + fun generateTransformedValuesBubble(data: IBubbleDataSet, phaseY: Float, from: Int, to: Int): FloatArray { + val count = (to - from + 1) * 2 // (int) Math.ceil((to - from) * phaseX) * 2; + + if (valuePointsForGenerateTransformedValuesBubble.size != count) { + valuePointsForGenerateTransformedValuesBubble = FloatArray(count) + } + val valuePoints = valuePointsForGenerateTransformedValuesBubble + + var j = 0 + while (j < count) { + val e: Entry? = data.getEntryForIndex(j / 2 + from) + + if (e != null) { + valuePoints[j] = e.x + valuePoints[j + 1] = e.y * phaseY + } else { + valuePoints[j] = 0f + valuePoints[j + 1] = 0f + } + j += 2 + } + + this.valueToPixelMatrix.mapPoints(valuePoints) + + return valuePoints + } + + protected var valuePointsForGenerateTransformedValuesLine: FloatArray = FloatArray(1) + + /** + * Transforms an List of Entry into a float array containing the x and + * y values transformed with all matrices for the LINECHART. + */ + fun generateTransformedValuesLine( + data: ILineDataSet, + phaseX: Float, phaseY: Float, + min: Int, max: Int + ): FloatArray { + var count = (((max - min) * phaseX).toInt() + 1) * 2 + if (count < 0) count = 0 + + if (valuePointsForGenerateTransformedValuesLine.size != count) { + valuePointsForGenerateTransformedValuesLine = FloatArray(count) + } + val valuePoints = valuePointsForGenerateTransformedValuesLine + + var j = 0 + while (j < count) { + val e = data.getEntryForIndex(j / 2 + min) + + if (e != null) { + valuePoints[j] = e.x + valuePoints[j + 1] = e.y * phaseY + } else { + valuePoints[j] = 0f + valuePoints[j + 1] = 0f + } + j += 2 + } + + this.valueToPixelMatrix.mapPoints(valuePoints) + + return valuePoints + } + + protected var valuePointsForGenerateTransformedValuesCandle: FloatArray = FloatArray(1) + + /** + * Transforms an List of Entry into a float array containing the x and + * y values transformed with all matrices for the CANDLESTICKCHART. + */ + fun generateTransformedValuesCandle( + data: ICandleDataSet, + phaseX: Float, phaseY: Float, from: Int, to: Int + ): FloatArray { + val count = ((to - from) * phaseX + 1).toInt() * 2 + + if (valuePointsForGenerateTransformedValuesCandle.size != count) { + valuePointsForGenerateTransformedValuesCandle = FloatArray(count) + } + val valuePoints = valuePointsForGenerateTransformedValuesCandle + + var j = 0 + while (j < count) { + val e = data.getEntryForIndex(j / 2 + from) + + if (e != null) { + valuePoints[j] = e.x + valuePoints[j + 1] = e.high * phaseY + } else { + valuePoints[j] = 0f + valuePoints[j + 1] = 0f + } + j += 2 + } + + this.valueToPixelMatrix.mapPoints(valuePoints) + + return valuePoints + } + + /** + * transform a path with all the given matrices VERY IMPORTANT: keep order + * to value-touch-offset + */ + fun pathValueToPixel(path: Path) { + path.transform(this.valueMatrix) + path.transform(viewPortHandler.matrixTouch) + path.transform(this.offsetMatrix) + } + + /** + * Transforms multiple paths will all matrices. + */ + fun pathValuesToPixel(paths: MutableList) { + for (i in paths.indices) { + pathValueToPixel(paths.get(i)!!) + } + } + + /** + * Transform an array of points with all matrices. VERY IMPORTANT: Keep + * matrix order "value-touch-offset" when transforming. + */ + fun pointValuesToPixel(pts: FloatArray?) { + valueMatrix.mapPoints(pts) + viewPortHandler.matrixTouch.mapPoints(pts) + offsetMatrix.mapPoints(pts) + } + + /** + * Transform a rectangle with all matrices. + */ + fun rectValueToPixel(r: RectF?) { + valueMatrix.mapRect(r) + viewPortHandler.matrixTouch.mapRect(r) + offsetMatrix.mapRect(r) + } + + /** + * Transform a rectangle with all matrices with potential animation phases. + */ + fun rectToPixelPhase(r: RectF, phaseY: Float) { + // multiply the height of the rect with the phase + + r.top *= phaseY + r.bottom *= phaseY + + valueMatrix.mapRect(r) + viewPortHandler.matrixTouch.mapRect(r) + offsetMatrix.mapRect(r) + } + + fun rectToPixelPhaseHorizontal(r: RectF, phaseY: Float) { + // multiply the height of the rect with the phase + + r.left *= phaseY + r.right *= phaseY + + valueMatrix.mapRect(r) + viewPortHandler.matrixTouch.mapRect(r) + offsetMatrix.mapRect(r) + } + + /** + * Transform a rectangle with all matrices with potential animation phases. + */ + fun rectValueToPixelHorizontal(r: RectF?) { + valueMatrix.mapRect(r) + viewPortHandler.matrixTouch.mapRect(r) + offsetMatrix.mapRect(r) + } + + /** + * Transform a rectangle with all matrices with potential animation phases. + */ + fun rectValueToPixelHorizontal(r: RectF, phaseY: Float) { + // multiply the height of the rect with the phase + + r.left *= phaseY + r.right *= phaseY + + valueMatrix.mapRect(r) + viewPortHandler.matrixTouch.mapRect(r) + offsetMatrix.mapRect(r) + } + + /** + * transforms multiple rects with all matrices + */ + fun rectValuesToPixel(rects: MutableList) { + val m = this.valueToPixelMatrix + + for (i in rects.indices) m.mapRect(rects.get(i)) + } + + protected var mPixelToValueMatrixBuffer: Matrix = Matrix() + + /** + * Transforms the given array of touch positions (pixels) (x, y, x, y, ...) + * into values on the chart. + */ + fun pixelsToValue(pixels: FloatArray?) { + val tmp = mPixelToValueMatrixBuffer + tmp.reset() + + // invert all matrixes to convert back to the original value + offsetMatrix.invert(tmp) + tmp.mapPoints(pixels) + + viewPortHandler.matrixTouch.invert(tmp) + tmp.mapPoints(pixels) + + valueMatrix.invert(tmp) + tmp.mapPoints(pixels) + } + + /** + * buffer for performance + */ + var ptsBuffer: FloatArray = FloatArray(2) + + /** + * Returns a recyclable MPPointD instance. + * returns the x and y values in the chart at the given touch point + * (encapsulated in a MPPointD). This method transforms pixel coordinates to + * coordinates / values in the chart. This is the opposite method to + * getPixelForValues(...). + */ + fun getValuesByTouchPoint(x: Float, y: Float): MPPointD { + val result = getInstance(0.0, 0.0) + getValuesByTouchPoint(x, y, result) + return result + } + + fun getValuesByTouchPoint(x: Float, y: Float, outputPoint: MPPointD) { + ptsBuffer[0] = x + ptsBuffer[1] = y + + pixelsToValue(ptsBuffer) + + outputPoint.x = ptsBuffer[0].toDouble() + outputPoint.y = ptsBuffer[1].toDouble() + } + + /** + * Returns a recyclable MPPointD instance. + * Returns the x and y coordinates (pixels) for a given x and y value in the chart. + */ + fun getPixelForValues(x: Float, y: Float): MPPointD { + ptsBuffer[0] = x + ptsBuffer[1] = y + + pointValuesToPixel(ptsBuffer) + + val xPx = ptsBuffer[0].toDouble() + val yPx = ptsBuffer[1].toDouble() + + return getInstance(xPx, yPx) + } + + private val mMBuffer1 = Matrix() + + val valueToPixelMatrix: Matrix + get() { + mMBuffer1.set(this.valueMatrix) + mMBuffer1.postConcat(viewPortHandler.matrixTouch) + mMBuffer1.postConcat(this.offsetMatrix) + return mMBuffer1 + } + + private val mMBuffer2 = Matrix() + + val pixelToValueMatrix: Matrix + get() { + this.valueToPixelMatrix.invert(mMBuffer2) + return mMBuffer2 + } +} diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/TransformerHorizontalBarChart.java b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/TransformerHorizontalBarChart.java deleted file mode 100644 index a8a0cb600..000000000 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/TransformerHorizontalBarChart.java +++ /dev/null @@ -1,33 +0,0 @@ - -package com.github.mikephil.charting.utils; - -/** - * Transformer class for the HorizontalBarChart. - * - * @author Philipp Jahoda - */ -public class TransformerHorizontalBarChart extends Transformer { - - public TransformerHorizontalBarChart(ViewPortHandler viewPortHandler) { - super(viewPortHandler); - } - - /** - * Prepares the matrix that contains all offsets. - * - */ - public void prepareMatrixOffset(boolean inverted) { - - mMatrixOffset.reset(); - - if (!inverted) { - mMatrixOffset.postTranslate(mViewPortHandler.offsetLeft(), - mViewPortHandler.getChartHeight() - mViewPortHandler.offsetBottom()); - } else { - mMatrixOffset.setTranslate( - -(mViewPortHandler.getChartWidth() - mViewPortHandler.offsetRight()), - mViewPortHandler.getChartHeight() - mViewPortHandler.offsetBottom()); - mMatrixOffset.postScale(-1.0f, 1.0f); - } - } -} diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/TransformerHorizontalBarChart.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/TransformerHorizontalBarChart.kt new file mode 100644 index 000000000..741ab11a8 --- /dev/null +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/TransformerHorizontalBarChart.kt @@ -0,0 +1,26 @@ +package com.github.mikephil.charting.utils + +/** + * Transformer class for the HorizontalBarChart. + */ +class TransformerHorizontalBarChart(viewPortHandler: ViewPortHandler) : Transformer(viewPortHandler) { + /** + * Prepares the matrix that contains all offsets. + */ + override fun prepareMatrixOffset(inverted: Boolean) { + offsetMatrix.reset() + + if (!inverted) { + offsetMatrix.postTranslate( + viewPortHandler.offsetLeft(), + viewPortHandler.chartHeight - viewPortHandler.offsetBottom() + ) + } else { + offsetMatrix.setTranslate( + -(viewPortHandler.chartWidth - viewPortHandler.offsetRight()), + viewPortHandler.chartHeight - viewPortHandler.offsetBottom() + ) + offsetMatrix.postScale(-1.0f, 1.0f) + } + } +}