From 81ec94d61bd42a9392da12e4ed8978bf2e784208 Mon Sep 17 00:00:00 2001 From: Duong Tran Date: Tue, 21 Sep 2021 00:14:50 +1000 Subject: [PATCH] refactor: improve YAxisRenderer.renderAxisLabels --- .../charting/renderer/YAxisRenderer.java | 56 ++++++++++--------- 1 file changed, 31 insertions(+), 25 deletions(-) diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/YAxisRenderer.java b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/YAxisRenderer.java index 53cca7ee03..67567a6270 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/YAxisRenderer.java +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/YAxisRenderer.java @@ -41,6 +41,33 @@ public YAxisRenderer(ViewPortHandler viewPortHandler, YAxis yAxis, Transformer t } } + /** + * Return the axis label x position based on axis dependency and label position + * @param dependency + * @param labelPosition + * @return + */ + private float calculateAxisLabelsXPosition(AxisDependency dependency, YAxisLabelPosition labelPosition) { + float viewPortBase = dependency == AxisDependency.LEFT ? mViewPortHandler.offsetLeft() : mViewPortHandler.contentRight(); + float xOffset = mYAxis.getXOffset() * (labelPosition == YAxisLabelPosition.OUTSIDE_CHART ? -1 : 1); + + return viewPortBase + xOffset; + } + + /** + * Return the text align based on axis dependency and label position + * @param dependency + * @param labelPosition + * @return + */ + private Align getAxisLabelTextAlign(AxisDependency dependency, YAxisLabelPosition labelPosition) { + if (dependency == AxisDependency.LEFT ^ labelPosition == YAxisLabelPosition.OUTSIDE_CHART) { + return Align.LEFT; + } + + return Align.RIGHT; + } + /** * draws the y-axis labels to the screen */ @@ -56,36 +83,15 @@ public void renderAxisLabels(Canvas c) { mAxisLabelPaint.setTextSize(mYAxis.getTextSize()); mAxisLabelPaint.setColor(mYAxis.getTextColor()); - float xoffset = mYAxis.getXOffset(); - float yoffset = Utils.calcTextHeight(mAxisLabelPaint, "A") / 2.5f + mYAxis.getYOffset(); + float yOffset = Utils.calcTextHeight(mAxisLabelPaint, "A") / 2.5f + mYAxis.getYOffset(); AxisDependency dependency = mYAxis.getAxisDependency(); YAxisLabelPosition labelPosition = mYAxis.getLabelPosition(); - float xPos = 0f; - - if (dependency == AxisDependency.LEFT) { - - if (labelPosition == YAxisLabelPosition.OUTSIDE_CHART) { - mAxisLabelPaint.setTextAlign(Align.RIGHT); - xPos = mViewPortHandler.offsetLeft() - xoffset; - } else { - mAxisLabelPaint.setTextAlign(Align.LEFT); - xPos = mViewPortHandler.offsetLeft() + xoffset; - } - - } else { - - if (labelPosition == YAxisLabelPosition.OUTSIDE_CHART) { - mAxisLabelPaint.setTextAlign(Align.LEFT); - xPos = mViewPortHandler.contentRight() + xoffset; - } else { - mAxisLabelPaint.setTextAlign(Align.RIGHT); - xPos = mViewPortHandler.contentRight() - xoffset; - } - } + float xPos = calculateAxisLabelsXPosition(dependency, labelPosition); + mAxisLabelPaint.setTextAlign(getAxisLabelTextAlign(dependency, labelPosition)); - drawYLabels(c, xPos, positions, yoffset); + drawYLabels(c, xPos, positions, yOffset); } @Override