Repository navigation
fix: drop active items whose data was removed before update - #12565
FullStraing wants to merge 7 commits into
Conversation
Refactor _replayActive method to improve handling of active elements and hover styles.
Add test for dropping active elements when data is removed.
❌ Deploy Preview for chartjs-docs failed.
|
kurkle
left a comment
There was a problem hiding this comment.
Thanks, the bug is real and your test reproduces it. master already has the place that should drop these items, though, so the fix can be one line there instead of a second pass over _active.
update() calls _deactivateHidden() after the elements are rebuilt and before the datasets update. It drops every active item that _activeFilter() rejects, clears the dropped element's hover state through _clearHoverStyle() (which already returns early for an index past the end of the data), and in hover mode 'dataset' it also ends the dataset element's hover. A removed item gets through only because DatasetController._isDataVisible() treats a missing element as visible. In src/core/core.datasetController.js:
_isDataVisible(index) {
const element = this._cachedMeta.data[index];
return !!element && !element.hidden;
}With that change and none in core.controller.js, your spec passes, so do pointer hover followed by pop(), replacing the data with a shorter array, and removing the dataset, and the rest of the suite passes too. Could you switch the PR to it? The doc comment on _deactivateHidden() would then say that it drops hidden and removed data.
The filter in _replayActive() misses one case that this covers. In hover mode 'dataset', when the removed item was the only active one, the line keeps its hover style, because nothing sets meta.dataset.active back to false:
it('ends the dataset hover when its only active item is removed', function() {
const chart = acquireChart({
type: 'line',
data: {datasets: [{data: [1, 2, 3, 4, 5, 6].map((y, x) => ({x, y})), borderWidth: 1, hoverBorderWidth: 9}]},
options: {scales: {x: {type: 'linear'}}, animation: false, hover: {mode: 'dataset'}}
});
chart.setActiveElements([{datasetIndex: 0, index: 5}]);
chart.data.datasets[0].data.pop();
chart.update();
const meta = chart.getDatasetMeta(0);
expect(meta.dataset.active).toBe(false);
expect(meta.dataset.options.borderWidth).toBe(1);
});It fails with this PR and passes on master and with the change above, so it is worth adding next to your spec.
The branch as pushed does not parse either: .filter({datasetIndex, index}) => { in _replayActive() is missing the parentheses around the parameter, so lint and the build fail in CI. Switching to the change above removes that code.
…ata in _deactivateHidden docs
|
Thanks for the review — done. The fix is now the one line you suggested in DatasetController._isDataVisible(), and I reverted the _replayActive filter (the missing parens went away with it). I also added your dataset-hover test and updated the _deactivateHidden doc. Local checks: eslint clean, unit suite 289 passed. Let me know if anything else is needed. |
Fixes #12563
What was wrong
When an active element's data point is removed (
data.pop()or a shorter data array), the nextchart.update()still re-applied or removed the hover style for that index.getStylethen resolves options for an index past the end of the data, so a scriptable option gets a dataset context wherectx.rawis undefined (TypeError: Cannot read properties of undefined (reading 'y')). The stale item stayed in_active, so every laterupdate()threw again.What changed
The replay at the end of
update()moved into a private_replayActive()helper, which first drops active items whose index is no longer below the dataset's element count (datasets removed entirely are dropped too). Element counts are current there, the controllers were rebuilt above. This covers both thesetActiveElementspath and the replayed-pointer-event path, since both read the filtered_active.Tests
active elementsblock intest/specs/core.controller.spec.js: set index 5 active,data.pop(),update()must not throw andgetActiveElements()must be empty.TypeErrorand pass with the fix. Full unit suite: 275 passed.eslinton both changed files: clean.