]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Fix category scale invalid data handling (#8668)
authorJukka Kurkela <jukka.kurkela@gmail.com>
Thu, 18 Mar 2021 20:34:08 +0000 (22:34 +0200)
committerGitHub <noreply@github.com>
Thu, 18 Mar 2021 20:34:08 +0000 (16:34 -0400)
* Fix category scale invalid data handling
* Fix NaN

src/scales/scale.category.js
test/fixtures/scale.category/invalid-data.js [new file with mode: 0644]
test/fixtures/scale.category/invalid-data.png [new file with mode: 0644]

index c6efd61b8f4adfe5d82f2383964dbbd55a6264ea..175bd3e8fde280b5a838f63f7f8742dfbc679666 100644 (file)
@@ -1,10 +1,14 @@
 import Scale from '../core/core.scale';
-import {valueOrDefault} from '../helpers';
+import {isNullOrUndef, valueOrDefault} from '../helpers';
+
+const addIfString = (labels, raw, index) => typeof raw === 'string'
+  ? labels.push(raw) - 1
+  : isNaN(raw) ? null : index;
 
 function findOrAddLabel(labels, raw, index) {
   const first = labels.indexOf(raw);
   if (first === -1) {
-    return typeof raw === 'string' ? labels.push(raw) - 1 : index;
+    return addIfString(labels, raw, index);
   }
   const last = labels.lastIndexOf(raw);
   return first !== last ? index : first;
@@ -21,6 +25,9 @@ export default class CategoryScale extends Scale {
   }
 
   parse(raw, index) {
+    if (isNullOrUndef(raw)) {
+      return null;
+    }
     const labels = this.getLabels();
     return isFinite(index) && labels[index] === raw
       ? index : findOrAddLabel(labels, raw, valueOrDefault(index, raw));
@@ -96,7 +103,7 @@ export default class CategoryScale extends Scale {
       value = me.parse(value);
     }
 
-    return me.getPixelForDecimal((value - me._startValue) / me._valueRange);
+    return value === null ? NaN : me.getPixelForDecimal((value - me._startValue) / me._valueRange);
   }
 
   // Must override base implementation because it calls getPixelForValue
diff --git a/test/fixtures/scale.category/invalid-data.js b/test/fixtures/scale.category/invalid-data.js
new file mode 100644 (file)
index 0000000..33d1ee5
--- /dev/null
@@ -0,0 +1,41 @@
+module.exports = {
+  config: {
+    type: 'line',
+    data: {
+      labels: ['a', 'b', 'c', 'd', 'e', 'f', 'g'],
+      datasets: [{
+        data: [
+          {x: 'a', y: 1},
+          {x: null, y: 1},
+          {x: 2, y: 1},
+          {x: undefined, y: 1},
+          {x: 4, y: 1},
+          {x: NaN, y: 1},
+          {x: 6, y: 1}
+        ],
+        backgroundColor: 'red',
+        borderColor: 'red',
+        borderWidth: 5
+      }]
+    },
+    options: {
+      scales: {
+        y: {
+          display: false
+        },
+        x: {
+          grid: {
+            display: false
+          }
+        }
+      }
+    }
+  },
+  options: {
+    spriteText: true,
+    canvas: {
+      width: 256,
+      height: 256
+    }
+  }
+};
diff --git a/test/fixtures/scale.category/invalid-data.png b/test/fixtures/scale.category/invalid-data.png
new file mode 100644 (file)
index 0000000..126c0c8
Binary files /dev/null and b/test/fixtures/scale.category/invalid-data.png differ