]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Fix skipNull for subsequent datasets (#8972)
authorJukka Kurkela <jukka.kurkela@gmail.com>
Sat, 24 Apr 2021 19:20:52 +0000 (22:20 +0300)
committerGitHub <noreply@github.com>
Sat, 24 Apr 2021 19:20:52 +0000 (15:20 -0400)
src/controllers/controller.bar.js
test/fixtures/controller.bar/skipNull/bar-skip-null-object-data.js [moved from test/fixtures/controller.bar/bar-skip-null-object-data.js with 100% similarity]
test/fixtures/controller.bar/skipNull/bar-skip-null-object-data.png [moved from test/fixtures/controller.bar/bar-skip-null-object-data.png with 100% similarity]
test/fixtures/controller.bar/skipNull/bar-skip-null.js [moved from test/fixtures/controller.bar/bar-skip-null.js with 100% similarity]
test/fixtures/controller.bar/skipNull/bar-skip-null.png [moved from test/fixtures/controller.bar/bar-skip-null.png with 100% similarity]
test/fixtures/controller.bar/skipNull/combinations.js [new file with mode: 0644]
test/fixtures/controller.bar/skipNull/combinations.png [new file with mode: 0644]

index 0ed99b7ec8bee9eb72ff1c7e54b1e267f7cf0ecc..fa107586718be9827be6ed0af46f7c2976f6f1d2 100644 (file)
@@ -354,11 +354,12 @@ export default class BarController extends DatasetController {
         * Returns the stack index for the given dataset based on groups and bar visibility.
         * @param {number} [datasetIndex] - The dataset index
         * @param {string} [name] - The stack name to find
+   * @param {number} [dataIndex]
         * @returns {number} The stack index
         * @private
         */
-  _getStackIndex(datasetIndex, name) {
-    const stacks = this._getStacks(datasetIndex);
+  _getStackIndex(datasetIndex, name, dataIndex) {
+    const stacks = this._getStacks(datasetIndex, dataIndex);
     const index = (name !== undefined)
       ? stacks.indexOf(name)
       : -1; // indexOf returns -1 if element is not present
@@ -477,15 +478,16 @@ export default class BarController extends DatasetController {
     const me = this;
     const scale = ruler.scale;
     const options = me.options;
+    const skipNull = options.skipNull;
     const maxBarThickness = valueOrDefault(options.maxBarThickness, Infinity);
     let center, size;
     if (ruler.grouped) {
-      const stackCount = options.skipNull ? me._getStackCount(index) : ruler.stackCount;
+      const stackCount = skipNull ? me._getStackCount(index) : ruler.stackCount;
       const range = options.barThickness === 'flex'
         ? computeFlexCategoryTraits(index, ruler, options, stackCount)
         : computeFitCategoryTraits(index, ruler, options, stackCount);
 
-      const stackIndex = me._getStackIndex(me.index, me._cachedMeta.stack);
+      const stackIndex = me._getStackIndex(me.index, me._cachedMeta.stack, skipNull ? index : undefined);
       center = range.start + (range.chunk * stackIndex) + (range.chunk / 2);
       size = Math.min(maxBarThickness, range.chunk * range.ratio);
     } else {
diff --git a/test/fixtures/controller.bar/skipNull/combinations.js b/test/fixtures/controller.bar/skipNull/combinations.js
new file mode 100644 (file)
index 0000000..8dd3876
--- /dev/null
@@ -0,0 +1,38 @@
+module.exports = {
+  config: {
+    type: 'bar',
+    data: {
+      labels: ['0', '1', '2', '3', '4', '5', '6', '7'],
+      datasets: [
+        {
+          data: [null, 1000, null, 1000, null, 1000, null, 1000],
+          backgroundColor: '#00ff00',
+          borderColor: '#ff0000'
+        },
+        {
+          data: [null, null, 1000, 1000, null, null, 1000, 1000],
+          backgroundColor: '#ff0000',
+          borderColor: '#ff0000'
+        },
+        {
+          data: [null, null, null, null, 1000, 1000, 1000, 1000],
+          backgroundColor: '#0000ff',
+          borderColor: '#0000ff'
+        }
+      ]
+    },
+    options: {
+      skipNull: true,
+      scales: {
+        x: {display: false},
+        y: {display: false}
+      }
+    }
+  },
+  options: {
+    canvas: {
+      height: 256,
+      width: 512
+    }
+  }
+};
diff --git a/test/fixtures/controller.bar/skipNull/combinations.png b/test/fixtures/controller.bar/skipNull/combinations.png
new file mode 100644 (file)
index 0000000..6d26ea3
Binary files /dev/null and b/test/fixtures/controller.bar/skipNull/combinations.png differ