]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
feat: pass some chart options to DateAdapter (#10528)
authorDan Onoshko <danon0404@gmail.com>
Sat, 30 Jul 2022 15:25:01 +0000 (22:25 +0700)
committerGitHub <noreply@github.com>
Sat, 30 Jul 2022 15:25:01 +0000 (11:25 -0400)
feat: pass some chart options to DateAdapter

src/core/core.adapters.js
src/scales/scale.time.js
test/specs/scale.time.tests.js
types/adapters.d.ts

index 254091602fc405e99aa0bbf8b0d1a28c00ed9b06..064afff809d7dff0b4da985514da1bd77f14b1dc 100644 (file)
@@ -4,6 +4,10 @@
  * @private
  */
 
+/**
+ * @typedef { import("../../types/index.esm").ChartOptions } ChartOptions
+ */
+
 /**
  * @return {*}
  */
@@ -30,6 +34,13 @@ export class DateAdapter {
     this.options = options || {};
   }
 
+  /**
+   * Will called with chart options after adapter creation.
+   * @param {ChartOptions} chartOptions
+   */
+  // eslint-disable-next-line no-unused-vars
+  init(chartOptions) {}
+
   /**
         * Returns a map of time formats for the supported formatting units defined
         * in Unit as well as 'datetime' representing a detailed date/time string.
index 6b8078a7514af0087f3927ea7cca24c5f2b81e9a..8f57c1b5974f2e7aa3017955770460bbae38358f 100644 (file)
@@ -223,6 +223,8 @@ export default class TimeScale extends Scale {
     const time = scaleOpts.time || (scaleOpts.time = {});
     const adapter = this._adapter = new adapters._date(scaleOpts.adapters.date);
 
+    adapter.init(opts);
+
     // Backward compatibility: before introducing adapter, `displayFormats` was
     // supposed to contain *all* unit/string pairs but this can't be resolved
     // when loading the scale (adapters are loaded afterward), so let's populate
index 4daa7d750dbae9e7904aaa7900b4e49a7527b740..70abc9bbced8c04b491fa5bf2caff003e41ee783 100644 (file)
@@ -1235,4 +1235,29 @@ describe('Time scale tests', function() {
       });
     });
   });
+
+  it('should pass chart options to date adapter', function() {
+    let chartOptions;
+
+    Chart._adapters._date.override({
+      init(options) {
+        chartOptions = options;
+      }
+    });
+
+    var chart = window.acquireChart({
+      type: 'line',
+      data: {},
+      options: {
+        locale: 'es',
+        scales: {
+          x: {
+            type: 'time'
+          },
+        }
+      }
+    });
+
+    expect(chartOptions).toEqual(chart.options);
+  });
 });
index f06c41b6851c01dc70b1b6f621c9a784ecea3bc4..cae40966ba06989764a5481b0ebc9af9b7439af2 100644 (file)
@@ -1,3 +1,5 @@
+import type { ChartOptions } from './index.esm';
+
 export type TimeUnit = 'millisecond' | 'second' | 'minute' | 'hour' | 'day' | 'week' | 'month' | 'quarter' | 'year';
 
 export interface DateAdapter {
@@ -5,6 +7,11 @@ export interface DateAdapter {
   override(members: Partial<DateAdapter>): void;
   readonly options: unknown;
 
+  /**
+   * Will called with chart options after adapter creation.
+   * @param {ChartOptions} chartOptions
+   */
+  init(chartOptions: ChartOptions): void;
   /**
    * Returns a map of time formats for the supported formatting units defined
    * in Unit as well as 'datetime' representing a detailed date/time string.