]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
`null` or `undefined` should skip grid lines in the time scale (#9252)
authorEvert Timberg <evert.timberg+github@gmail.com>
Sat, 12 Jun 2021 14:47:14 +0000 (10:47 -0400)
committerGitHub <noreply@github.com>
Sat, 12 Jun 2021 14:47:14 +0000 (10:47 -0400)
* `null` or `undefined` should skip grid lines in the time scale

* Refactor implementation per code review

src/core/core.scale.js
test/fixtures/scale.time/skip-null-gridlines.js [new file with mode: 0644]
test/fixtures/scale.time/skip-null-gridlines.png [new file with mode: 0644]
test/fixtures/scale.time/skip-undefined-gridlines.js [new file with mode: 0644]
test/fixtures/scale.time/skip-undefined-gridlines.png [new file with mode: 0644]

index 8850ad759026b463c00123afca9d8eeca5577771..a640d332a428fb74377ef883980874c46440baa5 100644 (file)
@@ -552,14 +552,6 @@ export default class Scale extends Element {
       tick = ticks[i];
       tick.label = call(tickOpts.callback, [tick.value, i, ticks], me);
     }
-    // Ticks should be skipped when callback returns null or undef, so lets remove those.
-    for (i = 0; i < ilen; i++) {
-      if (isNullOrUndef(ticks[i].label)) {
-        ticks.splice(i, 1);
-        ilen--;
-        i--;
-      }
-    }
   }
   afterTickToLabelConversion() {
     call(this.options.afterTickToLabelConversion, [this]);
@@ -769,6 +761,16 @@ export default class Scale extends Element {
 
     me.generateTickLabels(ticks);
 
+    // Ticks should be skipped when callback returns null or undef, so lets remove those.
+    let i, ilen;
+    for (i = 0, ilen = ticks.length; i < ilen; i++) {
+      if (isNullOrUndef(ticks[i].label)) {
+        ticks.splice(i, 1);
+        ilen--;
+        i--;
+      }
+    }
+
     me.afterTickToLabelConversion();
   }
 
diff --git a/test/fixtures/scale.time/skip-null-gridlines.js b/test/fixtures/scale.time/skip-null-gridlines.js
new file mode 100644 (file)
index 0000000..f8ded03
--- /dev/null
@@ -0,0 +1,32 @@
+module.exports = {
+  threshold: 0.01,
+  tolerance: 0.0025,
+  config: {
+    type: 'line',
+    data: {
+      labels: ['2017', '2018', '2019', '2020', '2025'],
+      datasets: [{data: [0, 1, 2, 3, 4], fill: false}]
+    },
+    options: {
+      scales: {
+        x: {
+          type: 'time',
+          time: {
+            parser: 'YYYY',
+            unit: 'year'
+          },
+          ticks: {
+            source: 'auto',
+            callback: (tick, index) => index % 2 === 0 ? null : tick,
+          }
+        },
+        y: {
+          display: false
+        }
+      }
+    }
+  },
+  options: {
+    spriteText: true
+  }
+};
diff --git a/test/fixtures/scale.time/skip-null-gridlines.png b/test/fixtures/scale.time/skip-null-gridlines.png
new file mode 100644 (file)
index 0000000..d0609a8
Binary files /dev/null and b/test/fixtures/scale.time/skip-null-gridlines.png differ
diff --git a/test/fixtures/scale.time/skip-undefined-gridlines.js b/test/fixtures/scale.time/skip-undefined-gridlines.js
new file mode 100644 (file)
index 0000000..2871a81
--- /dev/null
@@ -0,0 +1,32 @@
+module.exports = {
+  threshold: 0.01,
+  tolerance: 0.0025,
+  config: {
+    type: 'line',
+    data: {
+      labels: ['2017', '2018', '2019', '2020', '2025'],
+      datasets: [{data: [0, 1, 2, 3, 4], fill: false}]
+    },
+    options: {
+      scales: {
+        x: {
+          type: 'time',
+          time: {
+            parser: 'YYYY',
+            unit: 'year'
+          },
+          ticks: {
+            source: 'auto',
+            callback: (tick, index) => index % 2 === 0 ? undefined : tick,
+          }
+        },
+        y: {
+          display: false
+        }
+      }
+    }
+  },
+  options: {
+    spriteText: true
+  }
+};
diff --git a/test/fixtures/scale.time/skip-undefined-gridlines.png b/test/fixtures/scale.time/skip-undefined-gridlines.png
new file mode 100644 (file)
index 0000000..d0609a8
Binary files /dev/null and b/test/fixtures/scale.time/skip-undefined-gridlines.png differ