Repository navigation
fix: cover aligned text with the legend hit boxes - #12316
Conversation
kurkle
left a comment
There was a problem hiding this comment.
Thanks. Making the hit box cover aligned text is the right fix for #12067. It is piece P4 of the legend rework designed in openspec/changes/rework-legend/ (#12482), and it lands after #12346. Two things must change before merging:
- In a legend with more than one column, column 0's hit boxes reach into column 1.
getAlignedHitBox(..., this.right)(src/plugins/plugin.legend.js:273) ends every column's text area at the legend's right edge.- Repro: a
linechart with 12 datasets (Item 0…Item 11), legendposition: 'right',labels.textAlign: 'right', on a 1600×200 canvas. That makes two columns, with item 8 first in column 1. _getLegendItemAtat item 8's symbol returnsItem 2. Column 0's boxes are 186 px wide, against 80 on master. With'center'they are 133 px wide, and the result is the same.- The text area of column
cshould end at the next column's left edge minuslabels.padding, which isx + columnSizes[c].width. In the last column it ends atthis.right._draw()(line 438) has the samethis.rightfor every column, so column 0's right-aligned text is drawn on top of column 1 too. One helper used in both places fixes both:
// End of a column's text area: the next column's left edge minus padding, or the legend's right edge. _columnRight(x, col) { const {columnSizes} = this; return col < columnSizes.length - 1 ? x + columnSizes[col].width : this.right; }
- Use
this._columnRight(x, hitbox.col)inadjustHitBoxesandthis._columnRight(x, cursor.line)in_draw. With it, the repro returnsItem 8for both'right'and'center', and every existing legend spec still passes. - Please add that two-column case as a spec. The new generated specs use two datasets, which always fit in one column, so they can't catch this.
- Repro: a
adjustHitBoxesthrows when the label count drops betweenfit()andafterUpdate(line 272).afterUpdatecallsbuildLabels()again. IfgenerateLabelsreturns three items on the first call and one on the second,this.legendItems[i]is undefined, and.textAlignthrowsCannot read properties of undefined (reading 'textAlign'). Master draws this without an error. Read the item defensively:const item = this.legendItems[i]; const textAlign = (item && item.textAlign) || labels.textAlign;.
| hitbox.left += this.left + padding; | ||
| hitbox.left = rtlHelper.leftForLtr(rtlHelper.x(hitbox.left), hitbox.width); | ||
| const x = hitbox.left + this.left + padding; | ||
| const textAlign = this.legendItems[i].textAlign || labels.textAlign; |
There was a problem hiding this comment.
this.legendItems[i] is undefined when generateLabels returns fewer items than at fit time, so this throws. (item && item.textAlign) || labels.textAlign.
| hitbox.left = rtlHelper.leftForLtr(rtlHelper.x(hitbox.left), hitbox.width); | ||
| const x = hitbox.left + this.left + padding; | ||
| const textAlign = this.legendItems[i].textAlign || labels.textAlign; | ||
| const aligned = getAlignedHitBox(x, hitbox.width, boxWidth, fontSize, textAlign, this.right); |
There was a problem hiding this comment.
this.right ends every column's text area at the legend's right edge, so in a two-column legend column 0's boxes cover column 1. Use the column's own right edge (see the review).
2a430f2 to
833f4fc
Compare
✅ Deploy Preview for chartjs-docs ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
|
Size Change: +910 B (+0.25%) Total Size: 365 kB 📦 View Changed
ℹ️ View Unchanged
|
Cover aligned text and symbols while preserving RTL positioning. Add rendered-label regression coverage. Assisted-by: OpenAI Codex
In a vertical legend with more than one column, the text area of every column ended at the legend's right edge. With labels.textAlign 'right' or 'center', the hit boxes of the first column reached into the second one, so clicking an item of the second column returned an item of the first, and the first column's text was drawn over the second. A column's text area now ends at the next column's left edge minus the padding, and the last column's at the legend's right edge. The hit boxes and the drawing use the same helper.
afterUpdate builds the labels again after fit(). When generateLabels returned fewer items the second time, adjustHitBoxes read the text alignment of a missing item and threw "Cannot read properties of undefined (reading 'textAlign')". A hit box without an item now uses labels.textAlign.
The hit box specs clicked 1 px inside the text, so a hit box that was a few pixels too wide, or shifted in a right-to-left legend, still passed. They now check that each hit box runs from the leftmost to the rightmost edge of the drawn symbol and text. The column specs check that right-aligned text ends, and centered text is centered, in its own column's text area, in a two- and a three-column legend. The per-item alignment spec uses the second item, and a new spec covers right-aligned text that is wider than a narrow vertical legend, so the hit box starts left of the symbol.
4216c21 to
8163642
Compare
|
The red |
kurkle
left a comment
There was a problem hiding this comment.
The requested changes are in: each column's text area ends at the next column, and the hit boxes no longer throw when the labels shrink after fit.
Vertical legends can draw a short center- or right-aligned label outside its clickable area. Compute the hitbox from the union of the symbol and the aligned text, honoring per-item alignment and RTL positioning.
Fixes #12067.
Adds regression coverage for every legend position, left/center/right alignment, RTL, and per-item alignment. Eight alignment cases fail on the original code. The tests check the rendered text edges and symbol centers.
Validation on the fork:
controller.polarArea/pointLabels/overlapping.js, reproduced on the unchanged base in the same runner with the identical 293-pixel difference (0.11%, tolerance 0.1%). The fixture and tolerance are unchanged.This change and its tests were developed with OpenAI Codex.
A maintainer finished the requested changes on top of this commit:
labels.padding, and the last column's at the legend's right edge. The hit boxes and the drawing use the same_columnRight()helper. Before this, column 0's hit boxes and right-aligned text reached into column 1 (12 datasets, right legend, 1600×200: clicking item 8's symbol returned item 2).adjustHitBoxesno longer throws whengenerateLabelsreturns fewer items afterfit().textAlign: 'right'and'center', and the shrinking labels.