Skip to content

refactor: improve YAxisRenderer.renderAxisLabels #5194

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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
Expand Down