Repository navigation
Conversation
kurkle
left a comment
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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.
Fixes #11929
When decimation first runs, the plugin stores the original array in
_databut makes the publicdataset.datagetter return_decimated. Appending through the documenteddataset.data.push(...)path therefore updates only a temporary sampled array, while the next decimation pass still reads stale raw data.This keeps
dataset.databacked by the user-supplied raw array and makes dataset controllers consume_decimatedinternally. Scriptable contexts, segment contexts, and tooltip items now resolverawfrom 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
{x: 9, y: 9}instead of{x: 10, y: 10}{x: 4, y: 4}instead of the rendered tail pointpnpm lint-js— 0 errorspnpm buildwith pnpm 8.13.0