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, a caller could initialize an adapter instance itself and pass it somehow - maybe viaadapters.date
(with Chart.js checking wither it's an options object or adapter instance via duck typing), maybe via some newadapters.dateInstance
option or similar. - As a new top-level chart option, parallel to
plugins
, a caller could pass a new date adapter class (e.g.,{ type, data, options, plugins, adapter: { date } }
).
- Instead of passing adapter options via the
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. Unfortunately, of the listed 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.