From: Ben McCann Date: Wed, 2 Aug 2017 05:29:34 +0000 (-0700) Subject: Allow updating dataset types (#4586) X-Git-Tag: v2.7.0~1^2~28 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=2922dc96cfd3c3448538c19c02b349fe939f16eb;p=thirdparty%2FChart.js.git Allow updating dataset types (#4586) --- diff --git a/src/core/core.controller.js b/src/core/core.controller.js index 5ae591d41..28dd97650 100644 --- a/src/core/core.controller.js +++ b/src/core/core.controller.js @@ -289,9 +289,13 @@ module.exports = function(Chart) { helpers.each(me.data.datasets, function(dataset, datasetIndex) { var meta = me.getDatasetMeta(datasetIndex); - if (!meta.type) { - meta.type = dataset.type || me.config.type; + var type = dataset.type || me.config.type; + + if (meta.type && meta.type !== type) { + me.destroyDatasetMeta(datasetIndex); + meta = me.getDatasetMeta(datasetIndex); } + meta.type = type; types.push(meta.type); @@ -672,20 +676,30 @@ module.exports = function(Chart) { return this.options.legendCallback(this); }, + /** + * @private + */ + destroyDatasetMeta: function(datasetIndex) { + var id = this.id; + var dataset = this.data.datasets[datasetIndex]; + var meta = dataset._meta && dataset._meta[id]; + + if (meta) { + meta.controller.destroy(); + delete dataset._meta[id]; + } + }, + destroy: function() { var me = this; var canvas = me.canvas; - var meta, i, ilen; + var i, ilen; me.stop(); // dataset controllers need to cleanup associated data for (i = 0, ilen = me.data.datasets.length; i < ilen; ++i) { - meta = me.getDatasetMeta(i); - if (meta.controller) { - meta.controller.destroy(); - meta.controller = null; - } + me.destroyDatasetMeta(i); } if (canvas) { diff --git a/test/specs/core.controller.tests.js b/test/specs/core.controller.tests.js index 76740315a..beac21e0d 100644 --- a/test/specs/core.controller.tests.js +++ b/test/specs/core.controller.tests.js @@ -782,6 +782,41 @@ describe('Chart', function() { chart.update(); expect(chart.tooltip._options).toEqual(jasmine.objectContaining(newTooltipConfig)); }); + + it ('should update the metadata', function() { + var cfg = { + data: { + labels: ['A', 'B', 'C', 'D'], + datasets: [{ + type: 'line', + data: [10, 20, 30, 0] + }] + }, + options: { + responsive: true, + scales: { + xAxes: [{ + type: 'time' + }], + yAxes: [{ + scaleLabel: { + display: true, + labelString: 'Value' + } + }] + } + } + }; + var chart = acquireChart(cfg); + var meta = chart.getDatasetMeta(0); + expect(meta.type).toBe('line'); + + // change the dataset to bar and check that meta was updated + chart.config.data.datasets[0].type = 'bar'; + chart.update(); + meta = chart.getDatasetMeta(0); + expect(meta.type).toBe('bar'); + }); }); describe('plugin.extensions', function() {