Properly update stacks when datasets index changes (#9425)

This commit is contained in:
Jukka Kurkela 2021-07-16 14:15:54 +03:00 committed by GitHub
parent 11045a731a
commit 0094a58ef1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 44 additions and 2 deletions

View File

@ -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;
@ -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];
}
}

View File

@ -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();
}
}
};

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB