From bbed0ee750e90db0fbd0b8763a80bb309a9c4469 Mon Sep 17 00:00:00 2001 From: kurkle Date: Sun, 17 Nov 2019 01:23:16 +0200 Subject: [PATCH 1/2] Cache sorting order of dataset metas --- src/core/core.controller.js | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/core/core.controller.js b/src/core/core.controller.js index a1325394a51..bb0c0cf189f 100644 --- a/src/core/core.controller.js +++ b/src/core/core.controller.js @@ -418,6 +418,7 @@ helpers.extend(Chart.prototype, /** @lends Chart */ { var me = this; var newControllers = []; var datasets = me.data.datasets; + var sorted = me._sortedMetasets = []; var i, ilen; for (i = 0, ilen = datasets.length; i < ilen; i++) { @@ -446,8 +447,11 @@ helpers.extend(Chart.prototype, /** @lends Chart */ { meta.controller = new ControllerClass(me, i); newControllers.push(meta.controller); } + sorted.push(meta); } + sorted.sort(compare2Level('order', 'index')); + return newControllers; }, @@ -727,18 +731,16 @@ helpers.extend(Chart.prototype, /** @lends Chart */ { */ _getSortedDatasetMetas: function(filterVisible) { var me = this; - var datasets = me.data.datasets || []; + var metasets = me._sortedMetasets; var result = []; var i, ilen; - for (i = 0, ilen = datasets.length; i < ilen; ++i) { + for (i = 0, ilen = metasets.length; i < ilen; ++i) { if (!filterVisible || me.isDatasetVisible(i)) { - result.push(me.getDatasetMeta(i)); + result.push(metasets[i]); } } - result.sort(compare2Level('order', 'index')); - return result; }, From a8b976ef7c6955d3593624b52e93c8aa868bc181 Mon Sep 17 00:00:00 2001 From: kurkle Date: Mon, 18 Nov 2019 00:00:46 +0200 Subject: [PATCH 2/2] Cache dataset visibility --- src/core/core.controller.js | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/src/core/core.controller.js b/src/core/core.controller.js index bb0c0cf189f..bbb30f662e3 100644 --- a/src/core/core.controller.js +++ b/src/core/core.controller.js @@ -434,6 +434,7 @@ helpers.extend(Chart.prototype, /** @lends Chart */ { meta.order = dataset.order || 0; meta.index = i; meta.label = '' + dataset.label; + meta.visible = me.isDatasetVisible(i); if (meta.controller) { meta.controller.updateIndex(i); @@ -707,13 +708,15 @@ helpers.extend(Chart.prototype, /** @lends Chart */ { * @private */ transition: function(easingValue) { - var me = this; + const me = this; var i, ilen; if (!me._animationsDisabled) { - for (i = 0, ilen = (me.data.datasets || []).length; i < ilen; ++i) { - if (me.isDatasetVisible(i)) { - me.getDatasetMeta(i).controller.transition(easingValue); + const metas = me._getSortedDatasetMetas(); + for (i = 0, ilen = metas.length; i < ilen; ++i) { + let meta = metas[i]; + if (meta.visible) { + meta.controller.transition(easingValue); } } } @@ -736,8 +739,9 @@ helpers.extend(Chart.prototype, /** @lends Chart */ { var i, ilen; for (i = 0, ilen = metasets.length; i < ilen; ++i) { - if (!filterVisible || me.isDatasetVisible(i)) { - result.push(metasets[i]); + const meta = metasets[i]; + if (!filterVisible || meta.visible) { + result.push(meta); } } @@ -884,13 +888,7 @@ helpers.extend(Chart.prototype, /** @lends Chart */ { }, getVisibleDatasetCount: function() { - var count = 0; - for (var i = 0, ilen = this.data.datasets.length; i < ilen; ++i) { - if (this.isDatasetVisible(i)) { - count++; - } - } - return count; + return this._getSortedVisibleDatasetMetas().length; }, isDatasetVisible: function(datasetIndex) {