mirror of
https://github.com/chartjs/Chart.js.git
synced 2025-12-08 20:36:08 +00:00
Allow updating dataset types (#4586)
This commit is contained in:
parent
15934e49c5
commit
2922dc96cf
@ -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) {
|
||||
|
||||
@ -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() {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user