Skip to content

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

Merged
kurkle merged 5 commits into
chartjs:masterfrom
Fire162:fix/legend-align-end-rounding
Nov 26, 2026
Merged

kurkle merged 5 commits into
chartjs:masterfrom
Fire162:fix/legend-align-end-rounding

Conversation

@Fire162

@Fire162 Fire162 commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

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'.

A maintainer finished the requested changes on top of this commit:

  • Drawing reads each item's hit box once and follows its row or column. The 1e-6 tolerance and the size-based fallback are gone: an item without a hit box (the labels grew between fit() and drawing) is not drawn, so drawing does not throw. Master's size comparison as a fallback still threw for a vertical legend past its last column.
  • Fix legend crash when defaults font changes between fit and draw #12205's spec ("should not crash when defaults font changes before a render") moved in, with credit to its author. It throws Cannot read properties of undefined (reading 'height') on master.
  • New spec: a generateLabels that returns more items after the layout draws a top and a right legend without throwing.
  • The legend design in openspec/changes/rework-legend/design.md now says that an item without a hit box is not drawn.

Closes #12196

@kurkle kurkle left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this. It is the right fix, and it is the first piece of a larger rework of the legend that is now designed in openspec/changes/rework-legend/ (#12482). In that design, drawing reads each item's row or column from the hit boxes that fit() computed, which is what this PR does. That also fixes #12196, so #12205 is superseded by this PR. Two things are needed before it can merge:

  1. Drop the 1e-6 tolerance (src/plugins/plugin.legend.js:407 and :412). The tolerance is only used when an item has no hit box, and it isn't in the comparison on master. Please use master's comparison unchanged as the fallback. legendHitBoxes is set to [] in the constructor, so hitboxes && is never needed either. Reading const hitbox = hitboxes[i]; once per item would make both branches shorter.
  2. Bring in #12205's spec, with credit to its author. #12205's test ("should not crash when defaults font changes before a render": a right legend with 8 datasets on a 256×180 canvas, set Chart.defaults.font.size = 42, then chart.render()) throws Cannot read properties of undefined (reading 'height') on master and passes with this PR. So this PR is what fixes #12196, and the spec should guard it. Please add Closes #12196 and a Co-authored-by: trailer for the #12205 author on the commit.

This PR is the first in the series. The other legend fixes (#12330, #12187, #12316) and the later pieces build on it. If it hasn't moved in about two weeks, a maintainer will finish it on top of your commits and keep your authorship.

Comment thread src/plugins/plugin.legend.js Outdated

if (isHorizontal) {
if (i > 0 && x + width + padding > this.right) {
if (i > 0 && (hitboxes && hitboxes[i] ? hitboxes[i].row !== cursor.line : x + width + padding > this.right + 1e-6)) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please drop the 1e-6 here and on line 412, and use master's comparison unchanged as the fallback. hitboxes is always an array, so hitboxes && can go too.

@netlify

netlify Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for chartjs-docs ready!

Name Link
🔨 Latest commit 05dd8e2
🔍 Latest deploy log https://app.netlify.com/projects/chartjs-docs/deploys/6b082f5727b8ae14fdd3b698
😎 Deploy Preview https://deploy-preview-12346--chartjs-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

kurkle added a commit to Fire162/Chart.js that referenced this pull request Nov 26, 2026
The legend design still said that chartjs#12346's size-based fallback stays
for an item without a hit box. That fallback throws for a vertical
legend once it wraps past the last column, so the legend does not draw
such an item. The design's decision, its P1 row and its review notes
now say so.
@github-actions

github-actions Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Size Change: +39 B (+0.01%)

Total Size: 364 kB

📦 View Changed
Filename Size Change
dist/chart.cjs 82 kB +11 B (+0.01%)
dist/chart.js 82.3 kB +11 B (+0.01%)
dist/chart.umd.js 72.5 kB +8 B (+0.01%)
dist/chart.umd.min.js 72.5 kB +9 B (+0.01%)
ℹ️ View Unchanged
Filename Size
dist/chunks/helpers.dataset.cjs 25.8 kB
dist/chunks/helpers.dataset.js 25.6 kB
dist/helpers.cjs 1.52 kB
dist/helpers.js 1.33 kB

compressed-size-action

@kurkle
kurkle marked this pull request as ready for review November 26, 2026 07:41
etimberg
etimberg previously approved these changes Nov 26, 2026
@kurkle
kurkle marked this pull request as draft November 26, 2026 13:06
Fire162 and others added 5 commits November 26, 2026 15:06
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.
Changing Chart.defaults.font after an update and drawing again before
the next update made the size-based wrapping in _draw() leave the
columns that fit() computed, and drawing threw "Cannot read properties
of undefined (reading 'height')". Drawing now follows the hit boxes, so
this spec guards it.

The spec comes from chartjs#12205.

Co-authored-by: dhruv7539 <dhruvbhanderi7@gmail.com>
Drawing now follows each item's hit box without a size-based fallback
and without the 1e-6 tolerance. An item that has no hit box was added
by generateLabels after fit() (afterUpdate builds the labels again), so
fit() left no room for it: it is not drawn. Falling back to the size
comparison wrapped such an item past the last column of a vertical
legend and threw "Cannot read properties of undefined (reading
'height')".

legendHitBoxes is always an array, so the guard on it goes.
The legend design still said that chartjs#12346's size-based fallback stays
for an item without a hit box. That fallback throws for a vertical
legend once it wraps past the last column, so the legend does not draw
such an item. The design's decision, its P1 row and its review notes
now say so.
The count-change spec now checks that only the items with a hit box
are drawn. Its top variant passed on master, because a horizontal item
without a hit box is drawn at NaN without throwing.

The align-end spec now checks that Beta is drawn on Alpha's row and
inside the legend, not only at a finite x.

Also drop a stray blank line and the extra newline at the end of the
spec file.
@kurkle
kurkle force-pushed the fix/legend-align-end-rounding branch from 5658ef6 to 05dd8e2 Compare November 26, 2026 13:08
@kurkle
kurkle marked this pull request as ready for review November 26, 2026 13:16
@kurkle

kurkle commented Nov 26, 2026

Copy link
Copy Markdown
Member

The red Coveralls - ubuntu-latest-chrome and Coveralls - ubuntu-latest-firefox checks are not from this PR. They measure only the specs that still run under Karma, and master fails them too (5eb416f: "Coverage decreased (-6.5%) to 79.929%") since the specs moved to Vitest. The Vitest coverage, the combined coverage and both required builds pass.

@kurkle
kurkle requested a review from etimberg November 26, 2026 13:32

@kurkle kurkle left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The requested changes are in: the tolerance is gone, an item without a hit box is not drawn, and #12205's spec moved in with credit.

@kurkle
kurkle merged commit 1f5381a into chartjs:master Nov 26, 2026
10 of 12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

3 participants