]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Add format method to time scale to format timestamp using scale options (#11063)
authorstockiNail <stocki.nail@gmail.com>
Wed, 18 Jan 2023 13:27:52 +0000 (14:27 +0100)
committerGitHub <noreply@github.com>
Wed, 18 Jan 2023 13:27:52 +0000 (08:27 -0500)
src/scales/scale.time.js
src/types/index.d.ts
test/specs/scale.time.tests.js

index 6f292beacd2edea2980118cdce9b87f63669df0b..522f5d32c43b5fb570392b83a622d72fa78e6fe7 100644 (file)
@@ -502,6 +502,19 @@ export default class TimeScale extends Scale {
     return adapter.format(value, timeOpts.displayFormats.datetime);
   }
 
+  /**
+        * @param {number} value
+        * @param {string|undefined} format
+        * @return {string}
+        */
+  format(value, format) {
+    const options = this.options;
+    const formats = options.time.displayFormats;
+    const unit = this._unit;
+    const fmt = format || formats[unit];
+    return this._adapter.format(value, fmt);
+  }
+
   /**
         * Function to format an individual tick mark
         * @param {number} time
index c302c34389c8355fc8dd7832f35c40e9b117018e..f312dc4114b4289367a177da66ad377d6c26dcc6 100644 (file)
@@ -3270,6 +3270,7 @@ export type TimeScaleOptions = Omit<CartesianScaleOptions, 'min' | 'max'> & {
 };
 
 export interface TimeScale<O extends TimeScaleOptions = TimeScaleOptions> extends Scale<O> {
+  format(value: number, format?: string): string;
   getDataTimestamps(): number[];
   getLabelTimestamps(): string[];
   normalize(values: number[]): number[];
index abae7359cf39dc3494c8ba9ad0f89cbac38fa660..42817ae15c9cc1273f209e48232ccfba2802c5ba 100644 (file)
@@ -413,6 +413,49 @@ describe('Time scale tests', function() {
     expect(xScale.getLabelForValue(value)).toBe('Jan 1, 2015, 8:00:00 pm');
   });
 
+  it('should get the correct label for a data value by format', function() {
+    var chart = window.acquireChart({
+      type: 'line',
+      data: {
+        datasets: [{
+          xAxisID: 'x',
+          data: [null, 10, 3]
+        }],
+        labels: ['2015-01-01T20:00:00', '2015-01-02T21:00:00', '2015-01-03T22:00:00', '2015-01-05T23:00:00', '2015-01-07T03:00', '2015-01-08T10:00', '2015-01-10T12:00'], // days
+      },
+      options: {
+        scales: {
+          x: {
+            type: 'time',
+            time: {
+              unit: 'day',
+              displayFormats: {
+                day: 'YYYY-MM-DD'
+              }
+            },
+            position: 'bottom',
+            ticks: {
+              source: 'labels',
+              autoSkip: false
+            }
+          }
+        }
+      }
+    });
+
+    var xScale = chart.scales.x;
+    for (const lbl of chart.data.labels) {
+      var dd = xScale._adapter.parse(lbl);
+      var parsed = lbl.split('T');
+      expect(xScale.format(dd)).toBe(parsed[0]);
+    }
+    for (const lbl of chart.data.labels) {
+      var mm = xScale._adapter.parse(lbl);
+      var yearMonth = lbl.substring(0, 7);
+      expect(xScale.format(mm, 'YYYY-MM')).toBe(yearMonth);
+    }
+  });
+
   it('should round to isoWeekday', function() {
     var chart = window.acquireChart({
       type: 'line',