Support for multiple date adapters
- Dominant language
- JavaScript
- Stars
- 67.7k
- Forks
- 11.9k
- Avg merge
- 7h 39m
- Merged PRs (30d)
- 5
Description
### Feature Proposal
While investigating a bug report regarding date/time handling in a Chart.js plugin, I realized that properly testing the plugin's date/time support would benefit greatly if the test suite could register more than one date/time adapter. As far as I can tell, Chart.js doesn't support this.
This would rarely be of use outside of writing libraries for Chart.js; however, reducing the required use of globals and singletons and increasing flexibility seem like worthwhile goals in general. (For example, I appreciate that Chart.js's plugins system lets you use plugins on a per-chart basis as well as globally enabling them.)
### Possible Implementation
A designed-from-scratch interface might look something like this:
* Each date/time adapter module exports its adapter class as well as automatically registering it as the default Chart.js date adapter when imported.
* Individual charts can import the adapter class and pass it to the chart using a new option - for example:
* Instead of passing adapter options via the [`adapters.date` option](https://www.chartjs.org/docs/latest/axes/cartesian/time.html#time-axis-specific-options), a caller could initialize an adapter instance itself and pass it somehow - maybe via `adapters.date` (with Chart.js checking wither it's an options object or adapter instance via duck typing), maybe via some new `adapters.dateInstance` option or similar.
* As a new top-level chart option, parallel to [`plugins`](https://www.chartjs.org/docs/latest/configuration/#plugins), a caller could pass a new date adapter class (e.g., `{ type, data, options, plugins, adapter: { date } }`).
Given backwards compatibility constraints (in particular, the desire to work with existing third-party adapters), the following approach might be better:
* Existing Chart.js time adapters already provide an `_id` member that's commented as "DEBUG ONLY". (See, e.g., [here](https://github.com/chartjs/chartjs-adapter-date-fns/blob/v3.0.0/src/index.js#L29). Unfortunately, of the [listed adapters](https://github.com/chartjs/awesome?tab=readme-ov-file#adapters), chartjs-adapter-dayjs-4 and chartjs-adapter-spacetime don't provide an `_id`.) Build off of that to allow multiple adapters to be registered and accessed by that ID.
* Whenever an adapter registers itself, core.adapters.ts maintains a registry by `_id` of each adapter as well as registering it as the default.
* Callers may request a particular adapter via a new `adapters.dateId` option or similar.
Since the only listed adapters that provide an `_id` are those within the chartjs GitHub organization, the backwards compatibility design may not be much of an improvement - Chart.js's provided adapters could be updated to export their classes for the designed-from-scratch approach, and most third-party adapters would apparently have to be updated regardless.
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.