]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Fix time series scale to have each data point is spread equidistant (#11388)
authorstockiNail <stocki.nail@gmail.com>
Thu, 13 Jul 2023 20:36:08 +0000 (22:36 +0200)
committerGitHub <noreply@github.com>
Thu, 13 Jul 2023 20:36:08 +0000 (16:36 -0400)
* Fix time series scale to have each data point is spread equidistant

* remove tabs

* remove casting and add/update test cases

src/scales/scale.time.js
src/scales/scale.timeseries.js
test/fixtures/scale.timeseries/data-timestamps.js [new file with mode: 0644]
test/fixtures/scale.timeseries/data-timestamps.png [new file with mode: 0644]
test/fixtures/scale.timeseries/financial-daily.js
test/fixtures/scale.timeseries/financial-daily.png
test/fixtures/scale.timeseries/source-auto.png
test/fixtures/scale.timeseries/source-data-offset-min-max.png

index 03a9714b0478bc0965e551e7c4c30b873fd917ac..6bcef6c03e6c8d001d58792abe735a8bb25101e8 100644 (file)
@@ -445,7 +445,7 @@ export default class TimeScale extends Scale {
         * `minor` unit using the given scale time `options`.
         * Important: this method can return ticks outside the min and max range, it's the
         * responsibility of the calling code to clamp values if needed.
-        * @private
+        * @protected
         */
   _generate() {
     const adapter = this._adapter;
@@ -485,7 +485,7 @@ export default class TimeScale extends Scale {
     }
 
     // @ts-ignore
-    return Object.keys(ticks).sort((a, b) => a - b).map(x => +x);
+    return Object.keys(ticks).sort(sorter).map(x => +x);
   }
 
   /**
index f347cd341c1efa7b50e1a905922029416f76a7bd..2be73493407881d2ba7e6011b17dcfc5eeb82499 100644 (file)
@@ -110,6 +110,25 @@ class TimeSeriesScale extends TimeScale {
     return table;
   }
 
+  /**
+    * Generates all timestamps defined in the data.
+    * Important: this method can return ticks outside the min and max range, it's the
+    * responsibility of the calling code to clamp values if needed.
+    * @protected
+    */
+  _generate() {
+    const min = this.min;
+    const max = this.max;
+    let timestamps = super.getDataTimestamps();
+    if (!timestamps.includes(min) || !timestamps.length) {
+      timestamps.splice(0, 0, min);
+    }
+    if (!timestamps.includes(max) || timestamps.length === 1) {
+      timestamps.push(max);
+    }
+    return timestamps.sort((a, b) => a - b);
+  }
+
   /**
         * Returns all timestamps
         * @return {number[]}
diff --git a/test/fixtures/scale.timeseries/data-timestamps.js b/test/fixtures/scale.timeseries/data-timestamps.js
new file mode 100644 (file)
index 0000000..b1df5c0
--- /dev/null
@@ -0,0 +1,46 @@
+module.exports = {
+  threshold: 0.01,
+  tolerance: 0.0015,
+  config: {
+    type: 'line',
+    data: {
+      datasets: [{data: [
+        {x: 1687849697000, y: 904},
+        {x: 1687817063000, y: 905},
+        {x: 1687694268000, y: 913},
+        {x: 1687609438000, y: 914},
+        {x: 1687561387000, y: 916},
+        {x: 1686875127000, y: 918},
+        {x: 1686873138000, y: 920},
+        {x: 1686872777000, y: 928},
+        {x: 1686081641000, y: 915}
+      ], fill: false}, {data: [
+        {x: 1687816803000, y: 1105},
+        {x: 1686869490000, y: 1114},
+        {x: 1686869397000, y: 1103},
+        {x: 1686869225000, y: 1091},
+        {x: 1686556516000, y: 1078}
+      ]}]
+    },
+    options: {
+      scales: {
+        x: {
+          type: 'timeseries',
+          bounds: 'data',
+          time: {
+            unit: 'day'
+          },
+          ticks: {
+            source: 'auto'
+          }
+        },
+        y: {
+          display: false
+        }
+      }
+    }
+  },
+  options: {
+    spriteText: true
+  }
+};
diff --git a/test/fixtures/scale.timeseries/data-timestamps.png b/test/fixtures/scale.timeseries/data-timestamps.png
new file mode 100644 (file)
index 0000000..31d9a3f
Binary files /dev/null and b/test/fixtures/scale.timeseries/data-timestamps.png differ
index 9f13f02d1953fa262fb97b99715bdef3f267a65e..d529be4ff444cee799c1089d936971d4e786bc9b 100644 (file)
@@ -33,12 +33,13 @@ module.exports = {
             autoSkip: true,
             autoSkipPadding: 75,
             maxRotation: 0,
-            sampleSize: 100
+            sampleSize: 100,
+            maxTicksLimit: 3
           },
           // manually set major ticks so that test passes in all time zones with moment adapter
           afterBuildTicks: function(scale) {
-            const major = [0, 12, 24];
             const ticks = scale.ticks;
+            const major = [0, 264, 522];
             for (let i = 0; i < ticks.length; i++) {
               ticks[i].major = major.indexOf(i) >= 0;
             }
index 659c0a1dae273d629a5a893e041a830e6ad2ddf9..1537512dd840e567e9c982a9671ab7511f026fa8 100644 (file)
Binary files a/test/fixtures/scale.timeseries/financial-daily.png and b/test/fixtures/scale.timeseries/financial-daily.png differ
index 532509c394b5d67851caaf086b452749a4978ad7..aa6255b6c83bfea5e61b9e46823a050c1aa8d1e2 100644 (file)
Binary files a/test/fixtures/scale.timeseries/source-auto.png and b/test/fixtures/scale.timeseries/source-auto.png differ
index 824bf36bd513d356edcfd090be9f304213d95d18..fe7165cfef2b4f65cc6dbac8d8d4a731f7a915c1 100644 (file)
Binary files a/test/fixtures/scale.timeseries/source-data-offset-min-max.png and b/test/fixtures/scale.timeseries/source-data-offset-min-max.png differ