]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
fix: pass timestamp to ticks callback (#10540)
authorDan Onoshko <danon0404@gmail.com>
Fri, 5 Aug 2022 11:59:36 +0000 (18:59 +0700)
committerGitHub <noreply@github.com>
Fri, 5 Aug 2022 11:59:36 +0000 (07:59 -0400)
* fix: pass timestamp to ticks callback
* docs: edit labelling page
* docs: additions to the migration guide

docs/axes/labelling.md
docs/migration/v4-migration.md
src/scales/scale.time.js
test/specs/scale.time.tests.js

index 5f613b6f81b2442fda614f4648cb58bbb7a94f8a..134979ac301f5ab009534e64efc232ad05d4b913 100644 (file)
@@ -22,7 +22,7 @@ To do this, you need to override the `ticks.callback` method in the axis configu
 
 The method receives 3 arguments:
 
-* `value` - the tick value in the **internal data format** of the associated scale.
+* `value` - the tick value in the **internal data format** of the associated scale. For time scale, it is a timestamp.
 * `index` - the tick index in the ticks array.
 * `ticks` - the array containing all of the [tick objects](../api/interfaces/Tick).
 
index 252488ca8cdd47b2b89b0f77c23835ab71ae7c41..8a36b48a02e5df86aa20d9e954cc3f7371ffaa83 100644 (file)
@@ -11,6 +11,7 @@ A number of changes were made to the configuration options passed to the `Chart`
 #### Specific changes
 
 * The radialLinear grid indexable and scriptable options don't decrease the index of the specified grid line anymore.
+* Ticks callback on time scale now receives timestamp instead of a formatted label.
 
 #### Type changes
 * The order of the `ChartMeta` parameters have been changed from `<Element, DatasetElement, Type>` to `<Type, Element, DatasetElement>`
index 04c1ec38ec7dbff4dfc9ad66278979a94e7d2e21..a3940472abdba64924efd4670ce4306d50ae6b90 100644 (file)
@@ -232,6 +232,8 @@ export default class TimeScale extends Scale {
        */
       source: 'auto',
 
+      callback: false,
+
       major: {
         enabled: false
       }
@@ -510,6 +512,12 @@ export default class TimeScale extends Scale {
         */
   _tickFormatFunction(time, index, ticks, format) {
     const options = this.options;
+    const formatter = options.ticks.callback;
+
+    if (formatter) {
+      return call(formatter, [time, index, ticks], this);
+    }
+
     const formats = options.time.displayFormats;
     const unit = this._unit;
     const majorUnit = this._majorUnit;
@@ -517,9 +525,8 @@ export default class TimeScale extends Scale {
     const majorFormat = majorUnit && formats[majorUnit];
     const tick = ticks[index];
     const major = majorUnit && majorFormat && tick && tick.major;
-    const label = this._adapter.format(time, format || (major ? majorFormat : minorFormat));
-    const formatter = options.ticks.callback;
-    return formatter ? call(formatter, [label, index, ticks], this) : label;
+
+    return this._adapter.format(time, format || (major ? majorFormat : minorFormat));
   }
 
   /**
index 70abc9bbced8c04b491fa5bf2caff003e41ee783..3629dc2508a301b686203b20a079c6ab11753f4d 100644 (file)
@@ -73,6 +73,7 @@ describe('Time scale tests', function() {
       },
       ticks: {
         source: 'auto',
+        callback: false,
         major: {
           enabled: false
         }
@@ -353,8 +354,8 @@ describe('Time scale tests', function() {
                 }
               },
               ticks: {
-                callback: function(value) {
-                  return '<' + value + '>';
+                callback: function(_, i) {
+                  return '<' + i + '>';
                 }
               }
             }
@@ -368,21 +369,21 @@ describe('Time scale tests', function() {
       var labels = getLabels(this.scale);
 
       expect(labels.length).toEqual(21);
-      expect(labels[0]).toEqual('<8:00:00>');
-      expect(labels[labels.length - 1]).toEqual('<8:01:00>');
+      expect(labels[0]).toEqual('<0>');
+      expect(labels[labels.length - 1]).toEqual('<60>');
     });
 
     it('should update ticks.callback correctly', function() {
       var chart = this.chart;
-      chart.options.scales.x.ticks.callback = function(value) {
-        return '{' + value + '}';
+      chart.options.scales.x.ticks.callback = function(_, i) {
+        return '{' + i + '}';
       };
       chart.update();
 
       var labels = getLabels(this.scale);
       expect(labels.length).toEqual(21);
-      expect(labels[0]).toEqual('{8:00:00}');
-      expect(labels[labels.length - 1]).toEqual('{8:01:00}');
+      expect(labels[0]).toEqual('{0}');
+      expect(labels[labels.length - 1]).toEqual('{60}');
     });
   });
 
@@ -1260,4 +1261,33 @@ describe('Time scale tests', function() {
 
     expect(chartOptions).toEqual(chart.options);
   });
+
+  it('should pass timestamp to ticks callback', () => {
+    let callbackValue;
+    window.acquireChart({
+      type: 'line',
+      data: {
+        datasets: [{
+          xAxisID: 'x',
+          data: [0, 0]
+        }],
+        labels: ['2015-01-01T20:00:00', '2015-01-01T20:01:00']
+      },
+      options: {
+        scales: {
+          x: {
+            type: 'time',
+            ticks: {
+              callback(value) {
+                callbackValue = value;
+                return value;
+              }
+            }
+          }
+        }
+      }
+    });
+
+    expect(typeof callbackValue).toBe('number');
+  });
 });