Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow updating dataset types #4586

Merged
merged 1 commit into from Aug 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -693,6 +693,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