]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Reduce code duplication and sort generated ticks (#7747)
authorJukka Kurkela <jukka.kurkela@gmail.com>
Wed, 2 Sep 2020 12:09:39 +0000 (15:09 +0300)
committerGitHub <noreply@github.com>
Wed, 2 Sep 2020 12:09:39 +0000 (08:09 -0400)
* Reduce code duplication and sort generated ticks
* Add test

src/scales/scale.time.js
test/fixtures/scale.time/negative-times.js [new file with mode: 0644]
test/fixtures/scale.time/negative-times.png [new file with mode: 0644]

index 99f5b9412526acfce0db871473f9c59eaee18889..2df0618838408be0cb3923e7bcda1b1070e59b01 100644 (file)
@@ -135,17 +135,18 @@ function determineMajorUnit(unit) {
 }
 
 /**
- * @param {number[]} timestamps
  * @param {object} ticks
  * @param {number} time
+ * @param {number[]} [timestamps] - if defined, snap to these timestamps
  */
-function addTick(timestamps, ticks, time) {
-       if (!timestamps.length) {
-               return;
+function addTick(ticks, time, timestamps) {
+       if (!timestamps) {
+               ticks[time] = true;
+       } else if (timestamps.length) {
+               const {lo, hi} = _lookup(timestamps, time);
+               const timestamp = timestamps[lo] >= time ? timestamps[lo] : timestamps[hi];
+               ticks[timestamp] = true;
        }
-       const {lo, hi} = _lookup(timestamps, time);
-       const timestamp = timestamps[lo] >= time ? timestamps[lo] : timestamps[hi];
-       ticks[timestamp] = true;
 }
 
 /**
@@ -416,28 +417,17 @@ export default class TimeScale extends Scale {
                        throw new Error(min + ' and ' + max + ' are too far apart with stepSize of ' + stepSize + ' ' + minor);
                }
 
-               if (me.options.ticks.source === 'data') {
-                       // need to make sure ticks are in data in this case
-                       const timestamps = me.getDataTimestamps();
-
-                       for (time = first; time < max; time = +adapter.add(time, stepSize, minor)) {
-                               addTick(timestamps, ticks, time);
-                       }
-
-                       if (time === max || options.bounds === 'ticks') {
-                               addTick(timestamps, ticks, time);
-                       }
-               } else {
-                       for (time = first; time < max; time = +adapter.add(time, stepSize, minor)) {
-                               ticks[time] = true;
-                       }
+               const timestamps = options.ticks.source === 'data' && me.getDataTimestamps();
+               for (time = first; time < max; time = +adapter.add(time, stepSize, minor)) {
+                       addTick(ticks, time, timestamps);
+               }
 
-                       if (time === max || options.bounds === 'ticks') {
-                               ticks[time] = true;
-                       }
+               if (time === max || options.bounds === 'ticks') {
+                       addTick(ticks, time, timestamps);
                }
 
-               return Object.keys(ticks).map(x => +x);
+               // @ts-ignore
+               return Object.keys(ticks).sort((a, b) => a - b).map(x => +x);
        }
 
        /**
diff --git a/test/fixtures/scale.time/negative-times.js b/test/fixtures/scale.time/negative-times.js
new file mode 100644 (file)
index 0000000..a186333
--- /dev/null
@@ -0,0 +1,35 @@
+module.exports = {
+       config: {
+               type: 'line',
+               data: {
+                       datasets: [{
+                               data: [
+                                       {x: -1000000, y: 1},
+                                       {x: 1000000000, y: 2}
+                               ]
+                       }]
+               },
+               options: {
+                       scales: {
+                               x: {
+                                       type: 'time',
+                                       time: {
+                                               unit: 'day'
+                                       },
+                                       ticks: {
+                                               display: false
+                                       }
+                               },
+                               y: {
+                                       ticks: {
+                                               display: false
+                                       }
+                               }
+                       },
+                       legend: false
+               }
+       },
+       options: {
+               canvas: {width: 1000, height: 200}
+       }
+};
diff --git a/test/fixtures/scale.time/negative-times.png b/test/fixtures/scale.time/negative-times.png
new file mode 100644 (file)
index 0000000..b745393
Binary files /dev/null and b/test/fixtures/scale.time/negative-times.png differ