From 914fcf596ba4e0a4b12501e041b8ca95d3877ac3 Mon Sep 17 00:00:00 2001 From: Hannes Achleitner Date: Fri, 18 Apr 2025 09:04:23 +0200 Subject: [PATCH] Axis code simplify --- .../charting/renderer/AxisRenderer.kt | 54 +++----- .../charting/renderer/XAxisRenderer.kt | 90 ++++++------- .../charting/renderer/YAxisRenderer.kt | 119 +++++++++--------- 3 files changed, 117 insertions(+), 146 deletions(-) diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/AxisRenderer.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/AxisRenderer.kt index 4bd956fdd..e7ef366e9 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/AxisRenderer.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/AxisRenderer.kt @@ -25,22 +25,16 @@ abstract class AxisRenderer( @JvmField protected var axis: AxisBase ) : Renderer(viewPortHandler) { - /** - * Returns the Transformer object used for transforming the axis values. - * - * @return - */ - /** - * Returns the Paint object that is used for drawing the grid-lines of the - * axis. - * - * @return - */ /** * paint object for the grid lines */ - var paintGrid: Paint? = null + var paintGrid = Paint().apply { + color = Color.GRAY + strokeWidth = 1f + style = Paint.Style.STROKE + alpha = 90 + } protected set /** @@ -51,45 +45,25 @@ abstract class AxisRenderer( /** * paint for the x-label values */ - var paintAxisLabels: Paint? = null + var paintAxisLabels = Paint(Paint.ANTI_ALIAS_FLAG) protected set - /** - * Returns the Paint object that is used for drawing the axis-line that goes - * alongside the axis. - * - * @return - */ /** * paint for the line surrounding the chart */ - var paintAxisLine: Paint? = null + var paintAxisLine = Paint().apply { + color = Color.BLACK + strokeWidth = 1f + style = Paint.Style.STROKE + } protected set /** * paint used for the limit lines */ @JvmField - protected var limitLinePaint: Paint - - init { - paintAxisLabels = Paint(Paint.ANTI_ALIAS_FLAG) - - paintGrid = Paint().apply { - color = Color.GRAY - strokeWidth = 1f - style = Paint.Style.STROKE - alpha = 90 - } - - paintAxisLine = Paint().apply { - color = Color.BLACK - strokeWidth = 1f - style = Paint.Style.STROKE - } - limitLinePaint = Paint(Paint.ANTI_ALIAS_FLAG).apply { - style = Paint.Style.STROKE - } + protected var limitLinePaint = Paint(Paint.ANTI_ALIAS_FLAG).apply { + style = Paint.Style.STROKE } /** 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 b7bc5269c..04d9f3d2b 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 @@ -26,9 +26,9 @@ open class XAxisRenderer( ) : AxisRenderer(viewPortHandler, trans, xAxis) { protected fun setupGridPaint() { - paintGrid!!.color = xAxis.gridColor - paintGrid!!.strokeWidth = xAxis.gridLineWidth - paintGrid!!.setPathEffect(xAxis.gridDashPathEffect) + paintGrid.color = xAxis.gridColor + paintGrid.strokeWidth = xAxis.gridLineWidth + paintGrid.setPathEffect(xAxis.gridDashPathEffect) } override fun computeAxis(min: Float, max: Float, inverted: Boolean) { @@ -65,8 +65,8 @@ open class XAxisRenderer( protected open fun computeSize() { val longest = xAxis.longestLabel - paintAxisLabels!!.setTypeface(xAxis.typeface) - paintAxisLabels!!.textSize = xAxis.textSize + paintAxisLabels.setTypeface(xAxis.typeface) + paintAxisLabels.textSize = xAxis.textSize val labelSize = Utils.calcTextSize(paintAxisLabels, longest) @@ -92,7 +92,7 @@ open class XAxisRenderer( val yoffset = xAxis.yOffset - paintAxisLabels!!.color = xAxis.textColor + paintAxisLabels.color = xAxis.textColor val pointF = MPPointF.getInstance(0f, 0f) when (xAxis.position) { @@ -131,15 +131,15 @@ open class XAxisRenderer( override fun renderAxisLine(c: Canvas) { if (!xAxis.isDrawAxisLineEnabled || !xAxis.isEnabled) return - paintAxisLine!!.color = xAxis.axisLineColor - paintAxisLine!!.strokeWidth = xAxis.axisLineWidth - paintAxisLine!!.setPathEffect(xAxis.axisLineDashPathEffect) + paintAxisLine.color = xAxis.axisLineColor + paintAxisLine.strokeWidth = xAxis.axisLineWidth + paintAxisLine.setPathEffect(xAxis.axisLineDashPathEffect) if (xAxis.position == XAxisPosition.TOP || xAxis.position == XAxisPosition.TOP_INSIDE || xAxis.position == XAxisPosition.BOTH_SIDED) { c.drawLine( viewPortHandler.contentLeft(), viewPortHandler.contentTop(), viewPortHandler.contentRight(), - viewPortHandler.contentTop(), paintAxisLine!! + viewPortHandler.contentTop(), paintAxisLine ) } @@ -147,7 +147,7 @@ open class XAxisRenderer( c.drawLine( viewPortHandler.contentLeft(), viewPortHandler.contentBottom(), viewPortHandler.contentRight(), - viewPortHandler.contentBottom(), paintAxisLine!! + viewPortHandler.contentBottom(), paintAxisLine ) } } @@ -223,8 +223,8 @@ open class XAxisRenderer( Utils.drawXAxisValue(c, formattedLabel, x, y, paintAxisLabels, anchor, angleDegrees) } - protected var mRenderGridLinesPath: Path = Path() - protected var mRenderGridLinesBuffer: FloatArray = FloatArray(2) + protected open var mRenderGridLinesPath: Path = Path() + protected open var mRenderGridLinesBuffer: FloatArray = FloatArray(2) override fun renderGridLines(c: Canvas) { if (!xAxis.isDrawGridLinesEnabled || !xAxis.isEnabled) return @@ -280,6 +280,21 @@ open class XAxisRenderer( return mGridClippingRect } + @JvmField + protected var mRenderLimitLinesBuffer: FloatArray = FloatArray(2) + + @JvmField + protected var mLimitLineClippingRect: RectF = RectF() + + var limitLineSegmentsBuffer: FloatArray = FloatArray(4) + private val mLimitLinePath = Path() + + init { + paintAxisLabels.color = Color.BLACK + paintAxisLabels.textAlign = Align.CENTER + paintAxisLabels.textSize = Utils.convertDpToPixel(10f) + } + /** * Draws the grid line at the specified position using the provided path. * @@ -293,16 +308,28 @@ open class XAxisRenderer( gridLinePath.lineTo(x, viewPortHandler.contentTop()) // draw a path because lines don't support dashing on lower android versions - c.drawPath(gridLinePath, paintGrid!!) + c.drawPath(gridLinePath, paintGrid) gridLinePath.reset() } - @JvmField - protected var mRenderLimitLinesBuffer: FloatArray = FloatArray(2) + fun renderLimitLineLine(c: Canvas, limitLine: LimitLine, position: FloatArray) { + limitLineSegmentsBuffer[0] = position[0] + limitLineSegmentsBuffer[1] = viewPortHandler.contentTop() + limitLineSegmentsBuffer[2] = position[0] + limitLineSegmentsBuffer[3] = viewPortHandler.contentBottom() - @JvmField - protected var mLimitLineClippingRect: RectF = RectF() + mLimitLinePath.reset() + mLimitLinePath.moveTo(limitLineSegmentsBuffer[0], limitLineSegmentsBuffer[1]) + mLimitLinePath.lineTo(limitLineSegmentsBuffer[2], limitLineSegmentsBuffer[3]) + + limitLinePaint.style = Paint.Style.STROKE + limitLinePaint.color = limitLine.lineColor + limitLinePaint.strokeWidth = limitLine.lineWidth + limitLinePaint.setPathEffect(limitLine.dashPathEffect) + + c.drawPath(mLimitLinePath, limitLinePaint) + } /** * Draws the LimitLines associated with this axis to the screen. @@ -340,33 +367,6 @@ open class XAxisRenderer( } } - var mLimitLineSegmentsBuffer: FloatArray = FloatArray(4) - private val mLimitLinePath = Path() - - init { - paintAxisLabels!!.color = Color.BLACK - paintAxisLabels!!.textAlign = Align.CENTER - paintAxisLabels!!.textSize = Utils.convertDpToPixel(10f) - } - - fun renderLimitLineLine(c: Canvas, limitLine: LimitLine, position: FloatArray) { - mLimitLineSegmentsBuffer[0] = position[0] - mLimitLineSegmentsBuffer[1] = viewPortHandler.contentTop() - mLimitLineSegmentsBuffer[2] = position[0] - mLimitLineSegmentsBuffer[3] = viewPortHandler.contentBottom() - - mLimitLinePath.reset() - mLimitLinePath.moveTo(mLimitLineSegmentsBuffer[0], mLimitLineSegmentsBuffer[1]) - mLimitLinePath.lineTo(mLimitLineSegmentsBuffer[2], mLimitLineSegmentsBuffer[3]) - - limitLinePaint.style = Paint.Style.STROKE - limitLinePaint.color = limitLine.lineColor - limitLinePaint.strokeWidth = limitLine.lineWidth - limitLinePaint.setPathEffect(limitLine.dashPathEffect) - - c.drawPath(mLimitLinePath, limitLinePaint) - } - fun renderLimitLineLabel(c: Canvas, limitLine: LimitLine, position: FloatArray, yOffset: Float) { val label = limitLine.label 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 5d3a842b8..e6fbfa5a4 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 @@ -17,8 +17,13 @@ import androidx.core.graphics.withSave open class YAxisRenderer(viewPortHandler: ViewPortHandler, @JvmField protected var yAxis: YAxis, trans: Transformer?) : AxisRenderer(viewPortHandler, trans, yAxis) { + @JvmField - protected var zeroLinePaint: Paint? = null + protected var zeroLinePaint = Paint(Paint.ANTI_ALIAS_FLAG).apply { + color = Color.GRAY + strokeWidth = 1f + style = Paint.Style.STROKE + } /** * Return the axis label x position based on axis dependency and label position @@ -49,41 +54,38 @@ open class YAxisRenderer(viewPortHandler: ViewPortHandler, @JvmField protected v val positions = transformedPositions - paintAxisLabels?.let { - it.setTypeface(yAxis.typeface) - it.textSize = yAxis.textSize - it.color = yAxis.textColor + paintAxisLabels.setTypeface(yAxis.typeface) + paintAxisLabels.textSize = yAxis.textSize + paintAxisLabels.color = yAxis.textColor - val yOffset = Utils.calcTextHeight(it, "A") / 2.5f + yAxis.yOffset + val yOffset = Utils.calcTextHeight(paintAxisLabels, "A") / 2.5f + yAxis.yOffset - val dependency = yAxis.axisDependency - val labelPosition = yAxis.labelPosition + val dependency = yAxis.axisDependency + val labelPosition = yAxis.labelPosition - val xPos = calculateAxisLabelsXPosition(dependency, labelPosition) - it.textAlign = getAxisLabelTextAlign(dependency, labelPosition) + val xPos = calculateAxisLabelsXPosition(dependency, labelPosition) + paintAxisLabels.textAlign = getAxisLabelTextAlign(dependency, labelPosition) - drawYLabels(c, xPos, positions, yOffset) - } + drawYLabels(c, xPos, positions, yOffset) } override fun renderAxisLine(c: Canvas) { - if (!yAxis.isEnabled || !yAxis.isDrawAxisLineEnabled) return + if (!yAxis.isEnabled || !yAxis.isDrawAxisLineEnabled) + return - paintAxisLine?.color = yAxis.axisLineColor - paintAxisLine?.strokeWidth = yAxis.axisLineWidth + paintAxisLine.color = yAxis.axisLineColor + paintAxisLine.strokeWidth = yAxis.axisLineWidth - paintAxisLine?.let { - if (yAxis.axisDependency == AxisDependency.LEFT) { - c.drawLine( - viewPortHandler.contentLeft(), viewPortHandler.contentTop(), viewPortHandler.contentLeft(), - viewPortHandler.contentBottom(), it - ) - } else { - c.drawLine( - viewPortHandler.contentRight(), viewPortHandler.contentTop(), viewPortHandler.contentRight(), - viewPortHandler.contentBottom(), it - ) - } + if (yAxis.axisDependency == AxisDependency.LEFT) { + c.drawLine( + viewPortHandler.contentLeft(), viewPortHandler.contentTop(), viewPortHandler.contentLeft(), + viewPortHandler.contentBottom(), paintAxisLine + ) + } else { + c.drawLine( + viewPortHandler.contentRight(), viewPortHandler.contentTop(), viewPortHandler.contentRight(), + viewPortHandler.contentBottom(), paintAxisLine + ) } } @@ -118,14 +120,12 @@ open class YAxisRenderer(viewPortHandler: ViewPortHandler, @JvmField protected v yAxis.getFormattedLabel(i) } - paintAxisLabels?.let { - c.drawText( - text!!, - fixedPosition + xOffset, - positions[i * 2 + 1] + offset, - it - ) - } + c.drawText( + text!!, + fixedPosition + xOffset, + positions[i * 2 + 1] + offset, + paintAxisLabels + ) } } @@ -139,27 +139,25 @@ open class YAxisRenderer(viewPortHandler: ViewPortHandler, @JvmField protected v val positions = transformedPositions - paintGrid?.let { - it.color = yAxis.gridColor - it.strokeWidth = yAxis.gridLineWidth - it.setPathEffect(yAxis.gridDashPathEffect) + paintGrid.color = yAxis.gridColor + paintGrid.strokeWidth = yAxis.gridLineWidth + paintGrid.setPathEffect(yAxis.gridDashPathEffect) - val gridLinePath = renderGridLinesPath - gridLinePath.reset() + val gridLinePath = renderGridLinesPath + gridLinePath.reset() - // draw the grid - var i = 0 - while (i < positions.size) { - // draw a path because lines don't support dashing on lower android versions - c.drawPath(linePath(gridLinePath, i, positions)!!, it) - gridLinePath.reset() - i += 2 - } - c.restoreToCount(clipRestoreCount) + // draw the grid + var i = 0 + while (i < positions.size) { + // draw a path because lines don't support dashing on lower android versions + c.drawPath(linePath(gridLinePath, i, positions)!!, paintGrid) + gridLinePath.reset() + i += 2 } - + c.restoreToCount(clipRestoreCount) } + if (yAxis.isDrawZeroLineEnabled) { drawZeroLine(c) } @@ -236,8 +234,8 @@ open class YAxisRenderer(viewPortHandler: ViewPortHandler, @JvmField protected v // draw zero line val pos = transformer?.getPixelForValues(0f, 0f) pos?.let { - zeroLinePaint!!.color = yAxis.zeroLineColor - zeroLinePaint!!.strokeWidth = yAxis.zeroLineWidth + zeroLinePaint.color = yAxis.zeroLineColor + zeroLinePaint.strokeWidth = yAxis.zeroLineWidth val zeroLinePath = drawZeroLinePath zeroLinePath.reset() @@ -246,7 +244,7 @@ open class YAxisRenderer(viewPortHandler: ViewPortHandler, @JvmField protected v zeroLinePath.lineTo(viewPortHandler.contentRight(), it.y.toFloat()) // draw a path because lines don't support dashing on lower android versions - c.drawPath(zeroLinePath, zeroLinePaint!!) + c.drawPath(zeroLinePath, zeroLinePaint) c.restoreToCount(clipRestoreCount) } @@ -259,14 +257,10 @@ open class YAxisRenderer(viewPortHandler: ViewPortHandler, @JvmField protected v protected var limitLineClippingRect: RectF = RectF() init { - paintAxisLabels?.let { - it.color = Color.BLACK - it.textSize = Utils.convertDpToPixel(10f) + paintAxisLabels.apply { + color = Color.BLACK + textSize = Utils.convertDpToPixel(10f) } - zeroLinePaint = Paint(Paint.ANTI_ALIAS_FLAG) - zeroLinePaint!!.color = Color.GRAY - zeroLinePaint!!.strokeWidth = 1f - zeroLinePaint!!.style = Paint.Style.STROKE } /** @@ -337,6 +331,7 @@ open class YAxisRenderer(viewPortHandler: ViewPortHandler, @JvmField protected v ) } } + LimitLabelPosition.RIGHT_BOTTOM -> { limitLinePaint.let { it.textAlign = Align.RIGHT @@ -347,6 +342,7 @@ open class YAxisRenderer(viewPortHandler: ViewPortHandler, @JvmField protected v ) } } + LimitLabelPosition.LEFT_TOP -> { limitLinePaint.let { it.textAlign = Align.LEFT @@ -357,6 +353,7 @@ open class YAxisRenderer(viewPortHandler: ViewPortHandler, @JvmField protected v ) } } + else -> { limitLinePaint.let { it.textAlign = Align.LEFT