From 844270eb5ec5d14b1786760fe288bfa0351d9d62 Mon Sep 17 00:00:00 2001 From: Dan Onoshko Date: Sat, 30 Jul 2022 22:25:01 +0700 Subject: [PATCH] feat: pass some chart options to DateAdapter (#10528) feat: pass some chart options to DateAdapter --- src/core/core.adapters.js | 11 +++++++++++ src/scales/scale.time.js | 2 ++ test/specs/scale.time.tests.js | 25 +++++++++++++++++++++++++ types/adapters.d.ts | 7 +++++++ 4 files changed, 45 insertions(+) diff --git a/src/core/core.adapters.js b/src/core/core.adapters.js index 254091602fc..064afff809d 100644 --- a/src/core/core.adapters.js +++ b/src/core/core.adapters.js @@ -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. diff --git a/src/scales/scale.time.js b/src/scales/scale.time.js index 6b8078a7514..8f57c1b5974 100644 --- a/src/scales/scale.time.js +++ b/src/scales/scale.time.js @@ -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 diff --git a/test/specs/scale.time.tests.js b/test/specs/scale.time.tests.js index 4daa7d750db..70abc9bbced 100644 --- a/test/specs/scale.time.tests.js +++ b/test/specs/scale.time.tests.js @@ -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); + }); }); diff --git a/types/adapters.d.ts b/types/adapters.d.ts index f06c41b6851..cae40966ba0 100644 --- a/types/adapters.d.ts +++ b/types/adapters.d.ts @@ -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): 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.