Skip to content

fix: hidden data is not active, so a hovered dataset fades out when hidden - #12560

Merged
kurkle merged 6 commits into
masterfrom
rework-legend-p12-hidden-not-active
Nov 27, 2026
Merged

kurkle merged 6 commits into
masterfrom
rework-legend-p12-hidden-not-active

Conversation

@kurkle

@kurkle kurkle commented Nov 27, 2026 •

Copy link
Copy Markdown
Member

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:

  • Pointer over the chart: the update replays the last event. It no longer finds the hidden dataset, so it removes its hover style. The un-hover animation then retargets the fade towards the normal colour.
  • Pointer outside the chart area (a legend click, or 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 resolves active ? '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.

  • The filter. A private chart method, _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.
  • Where it runs. _active is filtered where it is assigned:
    • in setActiveElements, before its "changed" test;
    • in the event handler, before onHover and onClick get the elements, so they no longer receive hidden data in active. 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;
    • at the start of update(), before the datasets update. The dropped items lose active there 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 private DatasetController._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.
  • The data part. It is a new protected controller method, DatasetController._isDataVisible(index).
    • By default it returns whether the element is not hidden, which is what hide(datasetIndex, dataIndex) sets.
    • Bar, doughnut (so pie too) and polarArea override it to also ask chart.getDataVisibility(index). They are the controllers that honour toggleDataVisibility. Custom controllers that extend them inherit it.
    • A line point at a toggled index is still drawn, so it can still be active.
  • Docs.
    • The HTML legend sample uses hide()/show() for datasets, so it animates like the built-in legend.
    • docs/developers/api.md points to hide/show from setDatasetVisibility, and says hidden data is never active under setActiveElements.
  • API reports. etc/chart.js.api.md gains 4 lines and etc/chart.js-helpers.api.md 1 line: protected _isDataVisible(index) on DatasetController, BarController, DoughnutController and PolarAreaController.
  • Design. openspec/changes/rework-legend records 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:

fade a hovered line out when it is hidden           expected 0.87 to be less than 0.5
fade a line set active by code ... from the legend  expected 0.5 to be less than 0.5
line shown again not in hover style                 expected true to be false
pie slice hidden from the legend                    expected [ {…} ] to deeply equal []
hover options, code-set point, hide(0)              expected true to be false
hover options, bar, toggleDataVisibility            expected true to be false
hover options, doughnut arc, toggleDataVisibility   expected true to be false
hover options, polarArea arc, toggleDataVisibility  expected true to be false
item hidden by hide(0, 1) stays active              expected [ {…}, {…} ] to deeply equal [ {…} ]
bar + line, toggleDataVisibility(1)                 expected [ {ds0}, {ds1} ] to deeply equal [ {ds1} ]
hidden bar the 'index' mode finds, onHover          expected [ {…} ] to deeply equal []
  • The twelfth spec passes on master and is kept as a guard: it checks the hover radius of a hovered point after 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:

should not set a dataset that onClick hides active   expected [ {…}, {…}, {…} ] to deeply equal []
should not set a dataset that onHover hides active   expected [ {…}, {…}, {…} ] to deeply equal []
dataset hover style kept when one item is hidden     expected false to be true  (dataset element active during the update)

The other four guard rules that no earlier spec pinned. Each one fails when its rule is broken:

  • a hovered line that onClick hides fades out (fails when the event compares with its stale previous list: alpha 0.87);
  • two datasets hidden in one update both end their hover state;
  • bar and polarArea items hidden with hide(0, 1) are not active (each fails when its override drops the element check).

A third round adds 3 specs:

  • a hovered point (mode 'index') whose dataset onClick hides 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;
  • a bar dataset active in hover mode 'dataset' can be hidden without a throw (bars have no dataset element);
  • a removed dataset that was active gets no new meta.

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 pointBackgroundColor that reads ctx.raw.y. Before the last commit, hiding throws TypeError: Cannot read properties of undefined (reading 'y').

With the fixes, all 23 pass in both browsers, in about 0.1 s each.

  • The line specs use a hover colour (alpha 0.5) that is less opaque than the normal one (0.9), so an un-hover that takes over the fade raises the alpha. They sample the alpha half way through the hide.

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 V8 total_allocated_bytes per operation.

case master µs branch µs master bytes branch bytes
mode 'dataset', 10k points, pointer switches datasets 405392 410510 85.68 M 85.79 M
mode 'dataset', 100k points set by code, all then none 226769 224970 436.87 M 438.35 M
mode 'nearest', one item, pointer moves 30.34 30.78 53823 55137
empty: mouseout with nothing active 0.04 0.04 30 29
empty: setActiveElements([]) 0.04 0.04 221 224
update() replaying 10k code-set active elements 169961 168756 82.25 M 82.28 M
update() replaying a last event (1000 points) 1247 1202 850269 851163
_filterActive alone on 100k items – 1557 – 2.79 M
  • Times are within noise in every case.
  • The pointer event in mode 'nearest' allocates about 1.3 KB more (+2.4%).
  • Setting 100k elements by code allocates about 1.5 MB more (+0.3%), the filtered copy.
  • These numbers were taken before the last simplification. That change removed a partition in the hover-style update, so it can only lower them.

Mode 'dataset' is timed at 10k points rather than 100k because the diff in _updateHoverStyles is 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 the active argument of onHover/onClick no longer include hidden data, as the docs now state.
  • The new protected _isDataVisible only adds to the types.
  • For data that is hidden, a custom controller's removeHoverStyle override no longer runs, and in hover mode 'dataset' neither does _removeDatasetHoverStyle. The hover state is ended by _clearHoverStyle without animating, so the hide keeps its colours. For data that stays visible, both run as before.
  • An active item whose data is removed in the same update that hides its dataset only loses active. Its options are not resolved, because a scriptable option would get a context without the data item.
  • The tooltip keeps its own active elements and is not changed here. chart.tooltip.getActiveElements() can still hold an item hidden with hide(datasetIndex, dataIndex) that the interaction finds, while chart.getActiveElements() leaves it out. The tooltip behaves as on master. A follow-up applies the same filter to the tooltip's elements.

@netlify

netlify Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

❌ Deploy Preview for chartjs-docs failed.

Name Link
🔨 Latest commit 46927ed
🔍 Latest deploy log https://app.netlify.com/projects/chartjs-docs/deploys/6b09d6f0330b5332fdad9a5a

@github-actions

github-actions Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

Size Change: +1.64 kB (+0.46%)

Total Size: 358 kB

📦 View Changed
Filename Size Change
dist/chart.cjs 82.3 kB +487 B (+0.6%)
dist/chart.js 82.7 kB +469 B (+0.57%)
dist/chart.umd.js 74.1 kB +340 B (+0.46%)
dist/chart.umd.min.js 74.1 kB +341 B (+0.46%)
ℹ️ View Unchanged
Filename Size
dist/chunks/helpers.dataset.cjs 21 kB
dist/chunks/helpers.dataset.js 20.8 kB
dist/helpers.cjs 1.54 kB
dist/helpers.js 1.34 kB

compressed-size-action

@kurkle
kurkle force-pushed the rework-legend-p12-hidden-not-active branch from b8ee0e0 to b0c6d4a Compare November 27, 2026 18:58
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
kurkle force-pushed the rework-legend-p12-hidden-not-active branch from b0c6d4a to be90d65 Compare November 27, 2026 19:09
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
kurkle merged commit 8602ae8 into master Nov 27, 2026
6 of 10 checks passed
@kurkle
kurkle deleted the rework-legend-p12-hidden-not-active branch November 27, 2026 21:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

In line chart when interaction mode is dataset, fade animation is missing when legend item is clicked

2 participants