Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: pass some chart options to DateAdapter #10528

Merged
merged 2 commits into from
Jul 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/core/core.adapters.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
* @private
*/

/**
* @typedef { import("../../types/index.esm").ChartOptions } ChartOptions
*/

/**
* @return {*}
*/
Expand All @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions src/scales/scale.time.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 25 additions & 0 deletions test/specs/scale.time.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
7 changes: 7 additions & 0 deletions types/adapters.d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import type { ChartOptions } from './index.esm';

export type TimeUnit = 'millisecond' | 'second' | 'minute' | 'hour' | 'day' | 'week' | 'month' | 'quarter' | 'year';

export interface DateAdapter {
// Override one or multiple of the methods to adjust to the logic of the current date library.
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.
Expand Down