]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Properly update stacks when datasets index changes (#9425)
authorJukka Kurkela <jukka.kurkela@gmail.com>
Fri, 16 Jul 2021 11:15:54 +0000 (14:15 +0300)
committerGitHub <noreply@github.com>
Fri, 16 Jul 2021 11:15:54 +0000 (07:15 -0400)
src/core/core.datasetController.js
test/fixtures/controller.line/stacking/updates.js [new file with mode: 0644]
test/fixtures/controller.line/stacking/updates.png [new file with mode: 0644]

index 5842f1e228f5297992f1ec36f02ba7fa6972dad7..048073a84007f2660fb233499ef38d28c23f1463 100644 (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];
   }
 }
 
diff --git a/test/fixtures/controller.line/stacking/updates.js b/test/fixtures/controller.line/stacking/updates.js
new file mode 100644 (file)
index 0000000..0853f0b
--- /dev/null
@@ -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();
+    }
+  }
+};
diff --git a/test/fixtures/controller.line/stacking/updates.png b/test/fixtures/controller.line/stacking/updates.png
new file mode 100644 (file)
index 0000000..2ca9957
Binary files /dev/null and b/test/fixtures/controller.line/stacking/updates.png differ