Skip to content

Fix appending data while decimation is active - #12284

Open
tianrking wants to merge 1 commit into
chartjs:masterfrom
tianrking:codex/decimation-live-data-appends
Open

tianrking wants to merge 1 commit into
chartjs:masterfrom
tianrking:codex/decimation-live-data-appends

Conversation

@tianrking

Copy link
Copy Markdown

Fixes #11929

When decimation first runs, the plugin stores the original array in _data but makes the public dataset.data getter return _decimated. Appending through the documented dataset.data.push(...) path therefore updates only a temporary sampled array, while the next decimation pass still reads stale raw data.

This keeps dataset.data backed by the user-supplied raw array and makes dataset controllers consume _decimated internally. Scriptable contexts, segment contexts, and tooltip items now resolve raw from the controller data so their indices continue to match the rendered sample. Existing decimation assertions use the rendered metadata length, and a regression test appends through the public data array and verifies the raw length, parsed tail, controller context, and tooltip item.

Tests

  • Test-first regression: raw length remained 10 instead of 11; parsed tail remained {x: 9, y: 9} instead of {x: 10, y: 10}
  • Tooltip test-first check: old lookup returned {x: 4, y: 4} instead of the rendered tail point
  • Targeted Karma suites: decimation 6/6, tooltip 40/40, dataset controller 44/44, line 102/102, scatter 9/9
  • Full Karma suite: 1703/1703
  • pnpm lint-js — 0 errors
  • pnpm build with pnpm 8.13.0

@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 for picking this up, and for the regression spec. The bug is real, but this approach fixes it by breaking reads. Once dataset.data returns the raw array, it no longer lines up with the drawn points while decimation is active. With 10 points, threshold: 7 and samples: 5, a scriptable option that reads ctx.dataset.data[ctx.dataIndex] gets the wrong point for 5 of its 10 calls (on master: 0). That is the pattern of the scriptable example in docs/general/options.md, and chartjs-plugin-datalabels reads its label values the same way. dataset.data.length changes from the drawn count to the raw count too. These all fail silently, so this can't go in as it is.

The fix can keep dataset.data as it is today and instead make the plugin pass changes at either end of the sample on to the raw data. Then no controller or tooltip change is needed; your new spec passes with only the plugin change (see the inline comment).

Please also update requirement 5 in docs/configuration/decimation.md. It should say that while decimation is active, dataset.data is the drawn sample, and that push, pop, shift and unshift on it reach the full data, while other changes (splice, assigning data[i]) need dataset.data set to a new array.

CI hasn't run on this branch: both runs failed at startup, because the branch carries the workflow files from before they were reworked. A rebase onto current master fixes that. (#12341 changes the min-max algorithm in the same file; the two don't conflict.)

enumerable: true,
get: function() {
return this._decimated;
return this._data;

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.

Instead of returning _data here (which makes ctx.dataset.data[ctx.dataIndex] and dataset.data.length disagree with the drawn points), the getter can keep returning _decimated, and the plugin can listen to the sample's array methods the way the dataset controller already does. Calls at either end are then made on the raw data as well, and the next update rebuilds the sample from it:

import {listenArrayEvents} from '../helpers/helpers.collection.js';

/**
 * While decimation is active, `dataset.data` returns the decimated sample. Changes made to it at
 * either end are also made to the raw data, so they are kept when the sample is rebuilt.
 */
function forwardToRawData(dataset) {
  return {
    _onDataPush: (...items) => dataset._data.push(...items),
    _onDataPop: () => dataset._data.pop(),
    _onDataShift: () => dataset._data.shift(),
    _onDataUnshift: (...items) => dataset._data.unshift(...items),
  };
}

and where the sample is stored:

      listenArrayEvents(decimated, forwardToRawData(dataset));
      dataset._decimated = decimated;

With only this on master (getter unchanged, no controller or tooltip changes) and your spec file, plugin.decimation (7/7), plugin.tooltip, core.datasetController, controller.line and controller.scatter all pass. push + update() draws the new tail, 190 pushes with an update after each keep all 200 points, pop/shift/unshift reach the raw data, and ctx.dataset.data[ctx.dataIndex] matches the drawn point for all 10 points. Update time with 100k points is unchanged (about 1.4 ms per update before and after).

splice and data[i] = ... are not forwarded: an index into the sample doesn't say which raw point is meant. Your spec could add a pop/unshift case and one that checks ctx.dataset.data[ctx.dataIndex] against ctx.parsed.

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.

Chart with live data source + decimation + zoom plugins not drawing new points when samples exceed decimation threshold

2 participants