]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Fix: applyStack() returned the sum of all values for hidden dataset indices, which...
authorNikolai Iakovlev <deylak@yandex.ru>
Thu, 24 Oct 2024 15:55:58 +0000 (18:55 +0300)
committerGitHub <noreply@github.com>
Thu, 24 Oct 2024 15:55:58 +0000 (11:55 -0400)
src/core/core.datasetController.js
test/specs/controller.bar.tests.js

index 8753cab650db79615a4d4a95f0fbd459de84b673..9b7126a93fd5facfbe79df129d64dd356690e2b1 100644 (file)
@@ -76,9 +76,11 @@ function applyStack(stack, value, dsIndex, options = {}) {
     return;
   }
 
+  let found = false;
   for (i = 0, ilen = keys.length; i < ilen; ++i) {
     datasetIndex = +keys[i];
     if (datasetIndex === dsIndex) {
+      found = true;
       if (options.all) {
         continue;
       }
@@ -89,6 +91,11 @@ function applyStack(stack, value, dsIndex, options = {}) {
       value += otherValue;
     }
   }
+
+  if (!found && !options.all) {
+    return 0;
+  }
+
   return value;
 }
 
index a64af1a878bda3753fcacc2525c2901bdaf3d370..128ef241d0085ab63d8f6e8a8fce7cb1e130a0fe 100644 (file)
@@ -1613,6 +1613,49 @@ describe('Chart.controllers.bar', function() {
     expect(data[0].y).toBeCloseToPixel(512);
   });
 
+  it('should hide bar dataset beneath the chart for correct animations', function() {
+    var chart = window.acquireChart({
+      type: 'bar',
+      data: {
+        datasets: [{
+          data: [1, 2, 3, 4]
+        }, {
+          data: [1, 2, 3, 4]
+        }],
+        labels: ['A', 'B', 'C', 'D']
+      },
+      options: {
+        plugins: {
+          legend: false,
+          title: false
+        },
+        scales: {
+          x: {
+            type: 'category',
+            display: false,
+            stacked: true,
+          },
+          y: {
+            type: 'linear',
+            display: false,
+            stacked: true,
+          }
+        }
+      }
+    });
+
+    var data = chart.getDatasetMeta(0).data;
+    expect(data[0].base).toBeCloseToPixel(512);
+    expect(data[0].y).toBeCloseToPixel(448);
+
+    chart.setDatasetVisibility(0, false);
+    chart.update();
+
+    data = chart.getDatasetMeta(0).data;
+    expect(data[0].base).toBeCloseToPixel(640);
+    expect(data[0].y).toBeCloseToPixel(512);
+  });
+
   describe('Float bar', function() {
     it('Should return correct values from getMinMax', function() {
       var chart = window.acquireChart({