]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Logarithmic: handle null/NaN values (#8793)
authorJukka Kurkela <jukka.kurkela@gmail.com>
Sat, 3 Apr 2021 12:01:48 +0000 (15:01 +0300)
committerJukka Kurkela <jukka.kurkela@gmail.com>
Sun, 4 Apr 2021 11:47:16 +0000 (14:47 +0300)
src/scales/scale.logarithmic.js
test/fixtures/scale.logarithmic/null-values.js [new file with mode: 0644]
test/fixtures/scale.logarithmic/null-values.png [new file with mode: 0644]
test/specs/scale.logarithmic.tests.js

index 2223f1cff88892a62b9352284857c5493d3ab749..99e83241485e8afd8ee3eaa757af3fc5b2448d3f 100644 (file)
@@ -170,6 +170,9 @@ export default class LogarithmicScale extends Scale {
     if (value === undefined || value === 0) {
       value = me.min;
     }
+    if (value === null || isNaN(value)) {
+      return NaN;
+    }
     return me.getPixelForDecimal(value === me.min
       ? 0
       : (log10(value) - me._startValue) / me._valueRange);
diff --git a/test/fixtures/scale.logarithmic/null-values.js b/test/fixtures/scale.logarithmic/null-values.js
new file mode 100644 (file)
index 0000000..a9450f0
--- /dev/null
@@ -0,0 +1,49 @@
+module.exports = {
+  config: {
+    type: 'line',
+    data: {
+      labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
+      datasets: [{
+        backgroundColor: 'red',
+        borderColor: 'red',
+        fill: false,
+        data: [
+          150,
+          null,
+          1500,
+          200,
+          9000,
+          3000,
+          8888
+        ],
+        spanGaps: true
+      }, {
+        backgroundColor: 'blue',
+        borderColor: 'blue',
+        fill: false,
+        data: [
+          1000,
+          5500,
+          800,
+          7777,
+          null,
+          6666,
+          5555
+        ],
+        spanGaps: false
+      }]
+    },
+    options: {
+      responsive: true,
+      scales: {
+        x: {
+          display: false,
+        },
+        y: {
+          display: false,
+          type: 'logarithmic',
+        }
+      }
+    }
+  }
+};
diff --git a/test/fixtures/scale.logarithmic/null-values.png b/test/fixtures/scale.logarithmic/null-values.png
new file mode 100644 (file)
index 0000000..d759296
Binary files /dev/null and b/test/fixtures/scale.logarithmic/null-values.png differ
index 221152b66b594fa08ad5a2c6e5455554228e72cd..b68ef305e6df09ef9b15da9e8eb5e12486cd63d5 100644 (file)
@@ -3,6 +3,8 @@ function getLabels(scale) {
 }
 
 describe('Logarithmic Scale tests', function() {
+  describe('auto', jasmine.fixture.specs('scale.logarithmic'));
+
   it('should register', function() {
     var Constructor = Chart.registry.getScale('logarithmic');
     expect(Constructor).not.toBe(undefined);