Skip to content

fix: skip missing active elements when setting hover - #12411

Merged
etimberg merged 2 commits into
chartjs:masterfrom
azizu06:fm/chartjs-12042
Nov 23, 2026
Merged

etimberg merged 2 commits into
chartjs:masterfrom
azizu06:fm/chartjs-12042

Conversation

@azizu06

@azizu06 azizu06 commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

Summary

Calling setActiveElements after adding a dataset but before calling update() can store an undefined element. The next update throws when it reapplies hover styles.

setActiveElements
  resolve each requested point
  keep the points with chart elements

Fixes #12042

Evidence

  • Before chart.update() throws Cannot set properties of undefined (setting 'active').
  • After The update completes and the existing point keeps its hover style. The regression covers both points in the same selection.

The controller suite passed in Chrome and Firefox. Lint, type checks and integration tests passed. The full browser suite had the same 13 image fixture failures on this branch and on master on macOS.

Merge Danger

Door Two-way. Blast radius Hover.

@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, this is the right fix for the chart side. I ran it on current master: the new spec fails without the change, and these all throw Cannot set properties of undefined (setting 'active') on master and work with it:

  • an index past the end of the data, e.g. chart.setActiveElements([{datasetIndex: 0, index: 99}]) (throws right away on master, not only on the next update)
  • the same together with a valid point, followed by update()
  • hover.mode: 'dataset' with an invalid first item

Two requests before this goes in:

1. The tooltip has the same gap. Tooltip.setActiveElements() builds its list the same way and keeps the entries without an element, so this still throws after this change:

chart.tooltip.setActiveElements([{datasetIndex: 0, index: 99}], {x: 0, y: 0});
chart.update();
// TypeError: Cannot read properties of undefined

The docs sample calls both methods with the same list, so an invalid index would still take the chart down through the tooltip. Could you apply the same filter there in this PR, with a spec?

2. A spec for the out-of-range index. The current spec covers a dataset that hasn't been built yet. The more common case is an index that doesn't exist in an existing dataset (for example when hover is synced from another chart with more data). That one throws inside setActiveElements itself on master, so it is worth pinning separately.

One thing to be aware of, not a request: a point in a dataset that is added but not yet updated is now dropped for good, so it is not active after the update() either. That is better than the crash, and fine for this PR.

@netlify

netlify Bot commented Nov 23, 2026

Copy link
Copy Markdown

👷 Deploy request for chartjs-docs pending review.

Visit the deploys page to approve it

Name Link
🔨 Latest commit ddda718

@azizu06

azizu06 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for catching the tooltip case. chart.tooltip.setActiveElements() now skips entries with no element. The new specs call each setter with index 99 and a valid point, then check that the valid point stays active after chart.update(). The tooltip spec also checks that passing index 99 alone clears the active list.

@kurkle kurkle added this to the Version 4.6.2 milestone Nov 23, 2026
@etimberg
etimberg merged commit 4507317 into chartjs:master Nov 23, 2026
7 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.

Fix Bug setting active hover to undefined

3 participants