Skip to content

Commit

Permalink
Properly update stacks when datasets index changes (#9425)
Browse files Browse the repository at this point in the history
  • Loading branch information
kurkle committed Jul 16, 2021
1 parent 11045a7 commit 0094a58
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/core/core.datasetController.js
Expand Up @@ -193,6 +193,8 @@ function createDataContext(parent, index, element) {
}

function clearStacks(meta, items) {
// Not using meta.index here, because it might be already updated if the dataset changed location
const datasetIndex = meta.controller.index;
const axis = meta.vScale && meta.vScale.axis;
if (!axis) {
return;
Expand All @@ -201,10 +203,10 @@ function clearStacks(meta, items) {
items = items || meta._parsed;
for (const parsed of items) {
const stacks = parsed._stacks;
if (!stacks || stacks[axis] === undefined || stacks[axis][meta.index] === undefined) {
if (!stacks || stacks[axis] === undefined || stacks[axis][datasetIndex] === undefined) {
return;
}
delete stacks[axis][meta.index];
delete stacks[axis][datasetIndex];
}
}

Expand Down
40 changes: 40 additions & 0 deletions test/fixtures/controller.line/stacking/updates.js
@@ -0,0 +1,40 @@
module.exports = {
description: 'https://github.com/chartjs/Chart.js/issues/9424',
config: {
type: 'line',
data: {
labels: [0, 1, 2],
datasets: [
{
data: [1, 1, 1],
stack: 's1',
borderColor: '#ff0000',
},
{
data: [2, 2, 2],
stack: 's1',
borderColor: '#00ff00',
},
{
data: [3, 3, 3],
stack: 's1',
borderColor: '#0000ff',
}
]
},
options: {
borderWidth: 5,
scales: {
x: {display: false},
y: {display: true, stacked: true}
}
}
},
options: {
spriteText: true,
run(chart) {
chart.data.datasets.splice(1, 0, {data: [1.5, 1.5, 1.5], stack: 's2', borderColor: '#000000'});
chart.update();
}
}
};
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 0094a58

Please sign in to comment.