Skip to content

Commit

Permalink
feat: pass some chart options to DateAdapter (#10528)
Browse files Browse the repository at this point in the history
feat: pass some chart options to DateAdapter
  • Loading branch information
dangreen committed Jul 30, 2022
1 parent a26ca0a commit 844270e
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/core/core.adapters.js
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
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
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
@@ -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

0 comments on commit 844270e

Please sign in to comment.