Clear stacks when data is replaced (#8617)

This commit is contained in:
Jukka Kurkela
2021-03-11 23:20:07 +02:00
committed by GitHub
parent e513a90427
commit 1e296ccf4c
3 changed files with 56 additions and 4 deletions

View File

@@ -178,12 +178,13 @@ function createDataContext(parent, index, point, raw, element) {
function clearStacks(meta, items) {
items = items || meta._parsed;
items.forEach((parsed) => {
if (parsed._stacks[meta.vScale.id] === undefined || parsed._stacks[meta.vScale.id][meta.index] === undefined) {
for (const parsed of items) {
const stacks = parsed._stacks;
if (!stacks || stacks[meta.vScale.id] === undefined || stacks[meta.vScale.id][meta.index] === undefined) {
return;
}
delete parsed._stacks[meta.vScale.id][meta.index];
});
delete stacks[meta.vScale.id][meta.index];
}
}
const isDirectUpdateMode = (mode) => mode === 'reset' || mode === 'none';
@@ -311,6 +312,7 @@ export default class DatasetController {
if (me._data) {
// This case happens when the user replaced the data array instance.
unlistenArrayEvents(me._data, me);
clearStacks(me._cachedMeta);
}
if (data && Object.isExtensible(data)) {
listenArrayEvents(data, me);

View File

@@ -0,0 +1,50 @@
var barChartData = {
labels: ['January', 'February', 'March'],
datasets: [
{
label: 'Dataset 1',
backgroundColor: 'red',
data: [5, 5, 5]
},
{
label: 'Dataset 2',
backgroundColor: 'blue',
data: [5, 5, 5]
},
{
label: 'Dataset 3',
backgroundColor: 'green',
data: [5, 5, 5]
}
]
};
module.exports = {
description: 'https://github.com/chartjs/Chart.js/issues/8614',
config: {
type: 'bar',
data: barChartData,
options: {
scales: {
x: {
display: false,
stacked: true
},
y: {
display: false,
stacked: true
}
}
}
},
options: {
run(chart) {
chart.data.datasets[1].data = [
{x: 'January', y: 5},
// Februay missing
{x: 'March', y: 5}
];
chart.update();
}
}
};

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.3 KiB