Skip to content

fix: cover aligned text with the legend hit boxes - #12316

Merged
kurkle merged 4 commits into
chartjs:masterfrom
pentaoa:fix/aligned-legend-hitboxes
Nov 26, 2026
Merged

kurkle merged 4 commits into
chartjs:masterfrom
pentaoa:fix/aligned-legend-hitboxes

Conversation

@pentaoa

@pentaoa pentaoa commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

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:

  • Lint, build, type checks, and integration tests passed in this run.
  • Complete browser tests: Chrome 1,728 passed; Firefox 1,727 passed with one existing image-test failure. All legend tests passed in both browsers.
  • The Firefox failure, 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:

  • In a legend with more than one column, a column's text area now ends at the next column's left edge minus 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).
  • adjustHitBoxes no longer throws when generateLabels returns fewer items after fit().
  • New specs: a two-column legend with textAlign: 'right' and 'center', and the shrinking labels.

@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. 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:

  1. 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 line chart with 12 datasets (Item 0 … Item 11), legend position: 'right', labels.textAlign: 'right', on a 1600×200 canvas. That makes two columns, with item 8 first in column 1.
    • _getLegendItemAt at item 8's symbol returns Item 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 c should end at the next column's left edge minus labels.padding, which is x + columnSizes[c].width. In the last column it ends at this.right. _draw() (line 438) has the same this.right for 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) in adjustHitBoxes and this._columnRight(x, cursor.line) in _draw. With it, the repro returns Item 8 for 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.
  2. adjustHitBoxes throws when the label count drops between fit() and afterUpdate (line 272). afterUpdate calls buildLabels() again. If generateLabels returns three items on the first call and one on the second, this.legendItems[i] is undefined, and .textAlign throws Cannot 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;.

Comment thread src/plugins/plugin.legend.js Outdated
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;

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.

this.legendItems[i] is undefined when generateLabels returns fewer items than at fit time, so this throws. (item && item.textAlign) || labels.textAlign.

Comment thread src/plugins/plugin.legend.js Outdated
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);

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.

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

@kurkle
kurkle marked this pull request as draft November 26, 2026 05:59
@kurkle kurkle changed the title Fix hitboxes for aligned vertical legend labels fix: cover aligned text with the legend hit boxes Nov 26, 2026
@kurkle
kurkle force-pushed the fix/aligned-legend-hitboxes branch from 2a430f2 to 833f4fc Compare November 26, 2026 06:00
@netlify

netlify Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for chartjs-docs ready!

Name Link
🔨 Latest commit 8163642
🔍 Latest deploy log https://app.netlify.com/projects/chartjs-docs/deploys/6b082fd7a23c3eec3967fcb4
😎 Deploy Preview https://deploy-preview-12316--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.

@github-actions

github-actions Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Size Change: +910 B (+0.25%)

Total Size: 365 kB

📦 View Changed
Filename Size Change
dist/chart.cjs 82.3 kB +265 B (+0.32%)
dist/chart.js 82.6 kB +272 B (+0.33%)
dist/chart.umd.js 72.7 kB +186 B (+0.26%)
dist/chart.umd.min.js 72.7 kB +187 B (+0.26%)
ℹ️ 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 06:11
@kurkle
kurkle marked this pull request as draft November 26, 2026 13:06
pentaoa and others added 4 commits November 26, 2026 15:08
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.
@kurkle
kurkle force-pushed the fix/aligned-legend-hitboxes branch from 4216c21 to 8163642 Compare November 26, 2026 13:10
@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:33

@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: each column's text area ends at the next column, and the hit boxes no longer throw when the labels shrink after fit.

@kurkle
kurkle merged commit e8ba9b3 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

Development

Successfully merging this pull request may close these issues.

Wrong legend hitbox area when using textAlign right/center

3 participants