Skip to content

Commit

Permalink
Allow updating dataset types (#4586)
Browse files Browse the repository at this point in the history
  • Loading branch information
benmccann authored and simonbrunel committed Aug 2, 2017
1 parent 15934e4 commit 2922dc9
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 8 deletions.
30 changes: 22 additions & 8 deletions src/core/core.controller.js
Expand Up @@ -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);

Expand Down Expand Up @@ -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) {
Expand Down
35 changes: 35 additions & 0 deletions test/specs/core.controller.tests.js
Expand Up @@ -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() {
Expand Down

0 comments on commit 2922dc9

Please sign in to comment.