From 1e6cdd80d24ec863efb8986f59c491fde27f0143 Mon Sep 17 00:00:00 2001 From: Jan Wedel Date: Wed, 17 Jul 2024 17:47:11 +0200 Subject: [PATCH 1/2] Improves chart update performance for a large number of datasets Fixes #11814 partly --- src/core/core.controller.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/core/core.controller.js b/src/core/core.controller.js index 3c643e8ba3f..9a66d2a795b 100644 --- a/src/core/core.controller.js +++ b/src/core/core.controller.js @@ -852,8 +852,7 @@ class Chart { getDatasetMeta(datasetIndex) { const dataset = this.data.datasets[datasetIndex]; const metasets = this._metasets; - let meta = metasets.filter(x => x && x._dataset === dataset).pop(); - + let meta = metasets[datasetIndex]; if (!meta) { meta = { type: null, @@ -869,7 +868,7 @@ class Chart { _parsed: [], _sorted: false }; - metasets.push(meta); + metasets[datasetIndex] = meta; } return meta; From 1dbef4430fca1555cae03af673b8ac15f13f2682 Mon Sep 17 00:00:00 2001 From: Jan Wedel Date: Thu, 14 Nov 2024 22:06:42 +0100 Subject: [PATCH 2/2] Fixes some broken unit tests for dataset removal and reordering --- src/core/core.controller.js | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/src/core/core.controller.js b/src/core/core.controller.js index 9a66d2a795b..f59143db86c 100644 --- a/src/core/core.controller.js +++ b/src/core/core.controller.js @@ -404,30 +404,31 @@ class Chart { const metasets = this._metasets; const numData = this.data.datasets.length; const numMeta = metasets.length; + const sortedMetasetByIndex = this.data.datasets.map(currentDataset => metasets.find(currentMetaset => currentMetaset._dataset === currentDataset)); - metasets.sort((a, b) => a.index - b.index); if (numMeta > numData) { for (let i = numData; i < numMeta; ++i) { this._destroyDatasetMeta(i); } - metasets.splice(numData, numMeta - numData); + sortedMetasetByIndex.splice(numData, numMeta - numData); } - this._sortedMetasets = metasets.slice(0).sort(compare2Level('order', 'index')); + this._metasets = sortedMetasetByIndex; + this._sortedMetasets = sortedMetasetByIndex.slice(0).sort(compare2Level('order', 'index')); } /** - * @private - */ - _removeUnreferencedMetasets() { + * @private + */ + _rebuildMetasets() { const {_metasets: metasets, data: {datasets}} = this; - if (metasets.length > datasets.length) { - delete this._stacks; - } - metasets.forEach((meta, index) => { - if (datasets.filter(x => x === meta._dataset).length === 0) { - this._destroyDatasetMeta(index); - } + const newMetasets = []; + + datasets.forEach((dataset) => { + const matchingMetaset = metasets.find(metaset => dataset === metaset._dataset); + newMetasets.push(matchingMetaset); }); + + this._metasets = newMetasets; } buildOrUpdateControllers() { @@ -435,11 +436,12 @@ class Chart { const datasets = this.data.datasets; let i, ilen; - this._removeUnreferencedMetasets(); + this._rebuildMetasets(); for (i = 0, ilen = datasets.length; i < ilen; i++) { const dataset = datasets[i]; let meta = this.getDatasetMeta(i); + const type = dataset.type || this.config.type; if (meta.type && meta.type !== type) {