Repository navigation
fix(legend): draw last item when align is end (#12345) - #12346
Conversation
kurkle
left a comment
There was a problem hiding this comment.
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:
- Drop the
1e-6tolerance (src/plugins/plugin.legend.js:407and: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.legendHitBoxesis set to[]in the constructor, sohitboxes &&is never needed either. Readingconst hitbox = hitboxes[i];once per item would make both branches shorter. - 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, thenchart.render()) throwsCannot 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 addCloses #12196and aCo-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.
|
|
||
| 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)) { |
There was a problem hiding this comment.
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.
ac0ac74 to
401becc
Compare
✅ Deploy Preview for chartjs-docs ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
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.
|
Size Change: +39 B (+0.01%) Total Size: 364 kB 📦 View Changed
ℹ️ View Unchanged
|
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.
5658ef6 to
05dd8e2
Compare
|
The red |
Fixes #12345
Context
When a horizontal legend is configured with
align: 'end', the last item of a row ends atthis.right. Because binary floating-point arithmetic can introduce small rounding differences (such as1e-13),x + width + paddingcan evaluate slightly abovethis.right.When this occurred,
_draw()treated the item as overflowing the row and incrementedcursor.linepast the available entries inlineWidths.lineWidths[cursor.line]evaluated toundefined, makingcursor.xNaN. The browser canvas ignores calls withNaNcoordinates, leaving the item unrendered while its hit box remained interactive.Changes
hitboxes[i].row(andhitboxes[i].colfor vertical legends) in_draw()to use the row and column assignments established infit().1e-6) to the fallback overflow check when hit boxes are unavailable.test/specs/plugin.legend.tests.jsverifying that the last legend item renders with valid coordinates whenalign: 'end'.A maintainer finished the requested changes on top of this commit:
1e-6tolerance and the size-based fallback are gone: an item without a hit box (the labels grew betweenfit()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.Cannot read properties of undefined (reading 'height')on master.generateLabelsthat returns more items after the layout draws a top and a right legend without throwing.openspec/changes/rework-legend/design.mdnow says that an item without a hit box is not drawn.Closes #12196