Repository navigation
fix: hidden data is not active, so a hovered dataset fades out when hidden - #12560
Merged
Merged
Conversation
❌ Deploy Preview for chartjs-docs failed.
|
|
Size Change: +1.64 kB (+0.46%) Total Size: 358 kB 📦 View Changed
ℹ️ View Unchanged
|
kurkle
force-pushed
the
rework-legend-p12-hidden-not-active
branch
from
November 27, 2026 18:58
b8ee0e0 to
b0c6d4a
Compare
Hiding a hovered line starts an un-hover or re-hover colour animation that takes over the hide animation, and a pie slice hidden from the legend stays in getActiveElements(). #10882
…idden Hiding an active dataset re-styled it after its hide update: the replay of the last event removed its hover style, or the update re-applied it when the pointer was outside the chart area. Either one started an 'active' colour animation that took over the hide animation, so the line did not fade out. A slice hidden from the legend stayed active. The chart now filters hidden data out of the active elements where they are assigned: in setActiveElements, in the event handler before onHover and onClick, and at the start of update(), before the datasets update, so they resolve their normal options. There they lose their active state without the un-hover animation. A data item is hidden when its controller says so through the new protected _isDataVisible(index): by default when the element is hidden, and in bar, doughnut and polar area charts also by toggleDataVisibility. The API reports record the new method. The HTML legend sample uses hide() and show() for datasets. #10882
…aset A handler that hides the active dataset runs update(), which drops it from the active elements. The event then wrote its own, older list back, so the hidden dataset was active again. The event now filters its list again after the handlers and compares it with what the update left. In hover mode 'dataset', the update ended the dataset element's active state as soon as one of its items was hidden. It now does so only when none of the dataset's items is still active. Adds specs for both cases, for two datasets hidden in one update, and for bar and polar area items hidden with hide(datasetIndex, dataIndex).
- a hovered point whose dataset onClick hides fades out, also when the click moves the active index (fails until the next commit: the point keeps its hover colour through the hide); - a bar dataset active in hover mode 'dataset' can be hidden; - a removed dataset that was active gets no new meta.
Ending the hover state of hidden data before the update only cleared `active`. A line point (or a bar) shares its options, and an update that animates, such as 'hide', animates the shared options and leaves an element's own options alone. So a hovered point kept its hover colour through the hide, while master faded it through the replay's un-hover. The dataset controller's new private _clearHoverStyle(element, index) ends the hover state and gives the element the shared options back, so the update fades it with the others.
kurkle
force-pushed
the
rework-legend-p12-hidden-not-active
branch
from
November 27, 2026 19:09
b0c6d4a to
be90d65
Compare
An active item whose data was removed in the same update as its dataset was hidden still reached getStyle. Its index is past the data, so the context has no data item, and a scriptable option that reads ctx.raw threw. Such an item now only loses its active state.
kurkle
marked this pull request as ready for review
November 27, 2026 19:21
etimberg
approved these changes
Nov 27, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What was wrong
Hiding an active dataset re-styled it after its
'hide'update, and the new colour animation took over the hide animation. Which way depended on where the pointer was:setActiveElements): the update re-applies the hover style to the hidden dataset. The line stays in its hover colour until it vanishes, and is drawn in hover style when it is shown again.The hidden items also stayed active. A pie slice hidden from the legend stayed in
getActiveElements(). Because every controller resolvesactive ? 'active' : mode, a hidden element that was still active also got its hover options (hoverRadius,hoverBorderWidth) in that update.What changed
Hidden data is now never active.
_filterActive(items), leaves out the items whose dataset is not visible, or whose data item the controller does not show. It decides the dataset part once per run of a dataset's items and the data part per item._activeis filtered where it is assigned:setActiveElements, before its "changed" test;onHoverandonClickget the elements, so they no longer receive hidden data inactive. It filters again after them: a handler that hides data updates the chart, and the event then compares with what that update left instead of writing the hidden data back;update(), before the datasets update. The dropped items loseactivethere without an un-hover animation, so the update resolves their normal options and animates them in its own mode. The'hide'fade now keeps its colours. In hover mode'dataset', the dataset element loses its hover state only when none of the dataset's items stays active. A new privateDatasetController._clearHoverStyle(element, index)does the clearing. It gives an element of a controller that shares its options (line points, bars) the shared options back, because an animated update animates those and leaves the element's own hover options alone; otherwise a hovered point would keep its hover colour through the hide.update()keeps its statement count: the replay at its end moved into a helper.DatasetController._isDataVisible(index).hidden, which is whathide(datasetIndex, dataIndex)sets.chart.getDataVisibility(index). They are the controllers that honourtoggleDataVisibility. Custom controllers that extend them inherit it.hide()/show()for datasets, so it animates like the built-in legend.docs/developers/api.mdpoints tohide/showfromsetDatasetVisibility, and says hidden data is never active undersetActiveElements.etc/chart.js.api.mdgains 4 lines andetc/chart.js-helpers.api.md1 line:protected _isDataVisible(index)onDatasetController,BarController,DoughnutControllerandPolarAreaController.openspec/changes/rework-legendrecords piece P12 (tasks 13.1–13.3) and corrects the design's finding, decision 7 and the P12 → P13 contract.Closes #10882
Failing-test evidence
The first commit adds 12 specs in
test/specs/core.controller.spec.js, describe "hidden data is not active". On master, 11 fail in both chromium and firefox:hide(0), which the replay's un-hover already resets when animation is off.The second fix commit adds 7 specs. Three of them fail on the first fix commit:
The other four guard rules that no earlier spec pinned. Each one fails when its rule is broken:
onClickhides fades out (fails when the event compares with its stale previous list: alpha 0.87);hide(0, 1)are not active (each fails when its override drops the element check).A third round adds 3 specs:
'index') whose datasetonClickhides fades out, while the other dataset's point under the click stays active. Before the last fix commit the point stays at its hover alpha (expected 0.5 to be less than 0.5). It also fails (alpha 0.9) when the event's hover styles are diffed against its stale previous list;'dataset'can be hidden without a throw (bars have no dataset element);A fourth round adds one spec: an active point at index 5, whose data shrinks to 5 items in the update that hides its dataset, with a scriptable
pointBackgroundColorthat readsctx.raw.y. Before the last commit, hiding throwsTypeError: Cannot read properties of undefined (reading 'y').With the fixes, all 23 pass in both browsers, in about 0.1 s each.
Timings
Measured on two UMD builds (master and this branch) in Node 26 with
--expose-gc. Each case ran in its own process per build, interleaved, 3 repeats; the table shows medians. Bytes are V8total_allocated_bytesper operation.'dataset', 10k points, pointer switches datasets'dataset', 100k points set by code, all then none'nearest', one item, pointer movesmouseoutwith nothing activesetActiveElements([])update()replaying 10k code-set active elementsupdate()replaying a last event (1000 points)_filterActivealone on 100k items'nearest'allocates about 1.3 KB more (+2.4%).Mode
'dataset'is timed at 10k points rather than 100k because thediffin_updateHoverStylesis quadratic on master. With 100k points, one event that switches datasets takes minutes. That is an existing cost and is not changed here.How to try it
Make a line chart with
interaction: {mode: 'dataset', intersect: false}. Hover a line and click its legend item: the line fades out instead of vanishing at the end of the animation. The HTML legend sample now fades too.Compatibility
Breaking: no. A hidden item is not drawn, so dropping it from the active elements changes nothing visible apart from the bug.
getActiveElements()and theactiveargument ofonHover/onClickno longer include hidden data, as the docs now state._isDataVisibleonly adds to the types.removeHoverStyleoverride no longer runs, and in hover mode'dataset'neither does_removeDatasetHoverStyle. The hover state is ended by_clearHoverStylewithout animating, so the hide keeps its colours. For data that stays visible, both run as before.active. Its options are not resolved, because a scriptable option would get a context without the data item.chart.tooltip.getActiveElements()can still hold an item hidden withhide(datasetIndex, dataIndex)that the interaction finds, whilechart.getActiveElements()leaves it out. The tooltip behaves as on master. A follow-up applies the same filter to the tooltip's elements.