]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Allow updating dataset types (#4586)
authorBen McCann <benjamin.j.mccann@gmail.com>
Wed, 2 Aug 2017 05:29:34 +0000 (22:29 -0700)
committerSimon Brunel <simonbrunel@users.noreply.github.com>
Wed, 2 Aug 2017 05:29:34 +0000 (07:29 +0200)
src/core/core.controller.js
test/specs/core.controller.tests.js

index 5ae591d41fee059e6b40d0f4e37b420882463011..28dd976509514e27945db192db83e15324ff8693 100644 (file)
@@ -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) {
index 76740315ae172f16522edb1cc4a3ff2774253199..beac21e0daab37a1193e45e30dfe472600443ac5 100644 (file)
@@ -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() {