Skip to content

Commit db1f18f

Browse files
authored
Merge pull request #50 from AppDevNext/improveYAxisRenderer.renderAxisLabels
Improve y axis renderer.render axis labels
2 parents 34cdd13 + 14ce410 commit db1f18f

File tree

1 file changed

+31
-25
lines changed

1 file changed

+31
-25
lines changed

MPChartLib/src/main/java/com/github/mikephil/charting/renderer/YAxisRenderer.java

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,33 @@ public YAxisRenderer(ViewPortHandler viewPortHandler, YAxis yAxis, Transformer t
4141
}
4242
}
4343

44+
/**
45+
* Return the axis label x position based on axis dependency and label position
46+
* @param dependency
47+
* @param labelPosition
48+
* @return
49+
*/
50+
private float calculateAxisLabelsXPosition(AxisDependency dependency, YAxisLabelPosition labelPosition) {
51+
float viewPortBase = dependency == AxisDependency.LEFT ? mViewPortHandler.offsetLeft() : mViewPortHandler.contentRight();
52+
float xOffset = mYAxis.getXOffset() * (labelPosition == YAxisLabelPosition.OUTSIDE_CHART ? -1 : 1);
53+
54+
return viewPortBase + xOffset;
55+
}
56+
57+
/**
58+
* Return the text align based on axis dependency and label position
59+
* @param dependency
60+
* @param labelPosition
61+
* @return
62+
*/
63+
private Align getAxisLabelTextAlign(AxisDependency dependency, YAxisLabelPosition labelPosition) {
64+
if (dependency == AxisDependency.LEFT ^ labelPosition == YAxisLabelPosition.OUTSIDE_CHART) {
65+
return Align.LEFT;
66+
}
67+
68+
return Align.RIGHT;
69+
}
70+
4471
/**
4572
* draws the y-axis labels to the screen
4673
*/
@@ -56,36 +83,15 @@ public void renderAxisLabels(Canvas c) {
5683
mAxisLabelPaint.setTextSize(mYAxis.getTextSize());
5784
mAxisLabelPaint.setColor(mYAxis.getTextColor());
5885

59-
float xoffset = mYAxis.getXOffset();
60-
float yoffset = Utils.calcTextHeight(mAxisLabelPaint, "A") / 2.5f + mYAxis.getYOffset();
86+
float yOffset = Utils.calcTextHeight(mAxisLabelPaint, "A") / 2.5f + mYAxis.getYOffset();
6187

6288
AxisDependency dependency = mYAxis.getAxisDependency();
6389
YAxisLabelPosition labelPosition = mYAxis.getLabelPosition();
6490

65-
float xPos = 0f;
66-
67-
if (dependency == AxisDependency.LEFT) {
68-
69-
if (labelPosition == YAxisLabelPosition.OUTSIDE_CHART) {
70-
mAxisLabelPaint.setTextAlign(Align.RIGHT);
71-
xPos = mViewPortHandler.offsetLeft() - xoffset;
72-
} else {
73-
mAxisLabelPaint.setTextAlign(Align.LEFT);
74-
xPos = mViewPortHandler.offsetLeft() + xoffset;
75-
}
76-
77-
} else {
78-
79-
if (labelPosition == YAxisLabelPosition.OUTSIDE_CHART) {
80-
mAxisLabelPaint.setTextAlign(Align.LEFT);
81-
xPos = mViewPortHandler.contentRight() + xoffset;
82-
} else {
83-
mAxisLabelPaint.setTextAlign(Align.RIGHT);
84-
xPos = mViewPortHandler.contentRight() - xoffset;
85-
}
86-
}
91+
float xPos = calculateAxisLabelsXPosition(dependency, labelPosition);
92+
mAxisLabelPaint.setTextAlign(getAxisLabelTextAlign(dependency, labelPosition));
8793

88-
drawYLabels(c, xPos, positions, yoffset);
94+
drawYLabels(c, xPos, positions, yOffset);
8995
}
9096

9197
@Override

0 commit comments

Comments
 (0)