Skip to content

fix(legend): draw last item when align is end (#12345) - #12346

Open
Fire162 wants to merge 1 commit into
chartjs:masterfrom
Fire162:fix/legend-align-end-rounding
Open

Fire162 wants to merge 1 commit into
chartjs:masterfrom
Fire162:fix/legend-align-end-rounding

Conversation

@Fire162

@Fire162 Fire162 commented Oct 15, 2026

Copy link
Copy Markdown

Fixes #12345

Context

When a horizontal legend is configured with align: 'end', the last item of a row ends at this.right. Because binary floating-point arithmetic can introduce small rounding differences (such as 1e-13), x + width + padding can evaluate slightly above this.right.

When this occurred, _draw() treated the item as overflowing the row and incremented cursor.line past the available entries in lineWidths. lineWidths[cursor.line] evaluated to undefined, making cursor.x NaN. The browser canvas ignores calls with NaN coordinates, leaving the item unrendered while its hit box remained interactive.

Changes

  • Check hitboxes[i].row (and hitboxes[i].col for vertical legends) in _draw() to use the row and column assignments established in fit().
  • Add a small tolerance (1e-6) to the fallback overflow check when hit boxes are unavailable.
  • Add regression test in test/specs/plugin.legend.tests.js verifying that the last legend item renders with valid coordinates when align: 'end'.

With a horizontal legend configured with align: 'end', the last item
on a line ends at this.right. Binary floating-point rounding can cause
x + width + padding to evaluate slightly higher than this.right (for
example by 1e-13). When this happened, _draw() treated the item as
wrapping to the next line, incrementing cursor.line beyond lineWidths.
Because lineWidths[cursor.line] was undefined, cursor.x became NaN
and the item was not painted even though its hit box remained clickable.

Use hitboxes[i].row and hitboxes[i].col in _draw() to match the
line and column boundaries established during fit(), with a small
tolerance fallback when hit boxes are not available.
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.

Horizontal legend with align: 'end' sometimes drops the last item (still clickable)

1 participant