Skip to content

fix: align inner tick labels inwards on reversed linear axes - #12379

Open
Fardin7798 wants to merge 1 commit into
chartjs:masterfrom
Fardin7798:fix-inner-align-reverse
Open

Fardin7798 wants to merge 1 commit into
chartjs:masterfrom
Fardin7798:fix-inner-align-reverse

Conversation

@Fardin7798

Copy link
Copy Markdown

Closes #12378

With ticks.align: 'inner' and reverse: true on a linear x axis, the first and last tick labels were drawn outside the axis instead of inwards.

The inner alignment code flips left/right when options.reverse is set. That is right for the category scale, which keeps its ticks in label order and only reverses the pixels. Linear, logarithmic and time scales instead reverse the ticks array itself in buildTicks, so on those scales tick 0 is already the leftmost tick and the extra flip pushes both end labels outwards.

Changes in _computeLabelItems (src/core/core.scale.js):

  • Decide which end tick is on the left by comparing getPixelForTick(0) with the pixel of the last tick, instead of reading options.reverse. This works the same for category, linear, logarithmic and time scales.
  • The label backdrop now switches on the resolved per-tick alignment (tickTextAlign) instead of re-deriving inner from the tick index. The old code ignored reverse completely, so a reversed category axis drew the end backdrops on the wrong side of their labels. For non-reversed axes the result is identical to before.

Tests: added ticks.align inner specs in test/specs/core.scale.tests.js that check the leftmost label is left aligned with its backdrop starting at the label, the rightmost is right aligned, and the middle ones are center, for a linear axis, a reversed linear axis and a reversed category axis.

What I ran (local headless Chromium 131, Linux arm64):

  • karma --grep core.scale without the fix: the reversed linear and reversed category specs fail. With the fix: they pass, along with the existing core.scale specs and image fixtures (label-align-inner*, tick-backdrop-alignment-inner).
  • Full karma suite on Chrome, before and after the fix: the same 19 specs fail in both runs (font metric / pixel differences in my local environment, e.g. Linear Scale should fit correctly, Chart.layouts ...). No new failures. Firefox not run locally; left to CI.
  • eslint src/core/core.scale.js test/specs/core.scale.tests.js: no errors.

Linear, logarithmic and time scales reverse their ticks array when
`reverse` is set, but the category scale does not. The `inner` label
alignment assumed the category behaviour, so on a reversed linear axis
the first and last labels were aligned outwards. Pick the alignment
from the tick pixels instead, and position the label backdrop from the
resolved per-tick alignment so it follows the label.

Closes chartjs#12378
@Fardin7798
Fardin7798 marked this pull request as ready for review November 6, 2026 09:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

linear axis: "reverse" breaks tick align 'inner'

1 participant