Skip to content

Commit 94ac8e0

Browse files
committed
fix: wrong y offset for x labels in some cases
1 parent 3e03d98 commit 94ac8e0

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

Diff for: src/charting/renderer/XAxisRenderer.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export class XAxisRenderer extends AxisRenderer {
6161
const longest = axis.getLongestLabel();
6262
const labelSize = Utils.calcTextSize(this.mAxisLabelPaint, longest);
6363
const labelWidth = labelSize.width;
64-
const labelHeight = Utils.calcTextHeight(this.mAxisLabelPaint, 'Q') + axis.getYOffset() + 2;
64+
const labelHeight = Utils.calcTextHeight(this.mAxisLabelPaint, 'Q') + 2;
6565
const labelRotatedSize = Utils.getSizeOfRotatedRectangleByDegrees(labelWidth, labelHeight, axis.getLabelRotationAngle());
6666

6767
axis.mLabelWidth = Math.round(labelWidth);
@@ -88,6 +88,12 @@ export class XAxisRenderer extends AxisRenderer {
8888
paint.setColor(axis.getTextColor());
8989
// const align = this.mAxisLabelPaint.getTextAlign();
9090
// this.mAxisLabelPaint.setTextAlign(Align.CENTER);
91+
92+
// TODO: fix this the right way.
93+
// for now Utils.drawXAxisValue needs the font ascent
94+
// but it is not calculated all the time (in the lightest of cases)
95+
// we call this next line to ensure it is
96+
const labelLineHeight = Utils.getLineHeight(paint);
9197
const rect = this.mAxis.isIgnoringOffsets() ? this.mViewPortHandler.getChartRect() : this.mViewPortHandler.getContentRect();
9298
const pointF = { x: 0, y: 0 };
9399
if (axis.getPosition() === XAxisPosition.TOP) {

Diff for: src/charting/utils/Utils.ts

+1-6
Original file line numberDiff line numberDiff line change
@@ -491,10 +491,6 @@ export namespace Utils {
491491
// And we want to normalize it.
492492
drawOffsetY += -mFontMetricsBuffer.ascent;
493493

494-
// To have a consistent point of reference, we always draw left-aligned
495-
// const originalTextAlign = paint.getTextAlign();
496-
// paint.setTextAlign(Align.LEFT);
497-
498494
if (angleDegrees !== 0) {
499495
const lineHeight = paint.getFontMetrics(mFontMetricsBuffer);
500496
paint.getTextBounds(text, 0, text.length, mDrawTextRectBuffer);
@@ -521,11 +517,10 @@ export namespace Utils {
521517

522518
c.restore();
523519
} else {
524-
if (anchor.x !== 0 || anchor.y !== 0) {
520+
if (anchor.y !== 0) {
525521
const lineHeight = paint.getFontMetrics(mFontMetricsBuffer);
526522
drawOffsetY -= lineHeight * anchor.y;
527523
}
528-
529524
drawOffsetX += x;
530525
drawOffsetY += y;
531526
c.drawText(text, drawOffsetX, drawOffsetY, paint);

0 commit comments

Comments
 (0)