-
Notifications
You must be signed in to change notification settings - Fork 11.9k
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
Implement adapter to abstract date/time features #5960
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor notice on deprecated functionality.
I believe we need to move |
Correct, but since we need to generate |
That could also break some integrations because they will now need to explicitly depend on |
Yes, you're right, it would need to be added to both It is true that users will need to explicitly depend on
If we don't make that change I fear it removes most of the benefit of this PR, which is that people have been asking to switch out moment for smaller libraries (#4303). Without updating the package.json we don't allow them to do that, but only allow them to include multiple date libraries, which would only increase the size even more |
Does it work as expected with the luxon adapter?
You right, the ultimate goal is to allow CJS/ES users to skip moment from their builds but I think this is another fight, too complicated to be part of this PR. Note that users of the UMD build as global (
I just want to make sure we don't break CI of our users and unfortunately, that's just a warning: the build will success and will be deployed without If everyone agree with the approach of that PR, we could investigate the |
Use `adapter.startOf(ts, 'day') !== ts` instead of introducing a new adapter API to detect when the timestamp is midnight in the current timezone.
d352066
to
52a7734
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just wanted to say how happy I am about this PR. I had problems replacing moment
with dayjs
and this new adapter architecture would help a lot. Can't wait to see it merged 🥇
Also I'm very curious on how this would get adopted for the coming v3 release. I expect something like this:
import chartjsAdapterMoment from 'chartjs-adapter-moment';
var chart1 = new Chart(ctx, {
dateTimeAdapter: chartjsAdapterMoment //default
});
In 2.8+, you will not be able to use This work great when imported globally ( <script src="https://cdn.jsdelivr.net/npm/luxon@1.8.2/build/global/luxon.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chart.js@2.8.0/dist/Chart.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chartjs-adapter-luxon@1.0.0"></script> And for CJS / ESM projects, it "should" work like: import Chart from 'chart.js';
import 'chartjs-adapter-luxon'; // or 'chartjs-adapter-dayjs'
var chart1 = new Chart(ctx, {
// ...
}); The adapter is imported for "side effects" (implicitly registered). Though, I'm not totally convinced it should be implicit, we may want to prefer the following approach, however it's a bit weird since the adapter is a singleton. import Chart from 'chart.js';
import ChartAdapterLuxon from 'chartjs-adapter-luxon'; // or 'chartjs-adapter-dayjs'
// NOT THE CURRENT IMPLEMENTATION!
Chart.adapters.date = ChartAdapterLuxon;
var chart1 = new Chart(ctx, {
// ...
}); |
b76d6b4
👍 Thinking out loud:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks again @simonbrunel !
🎉 Thanks! 🏅 |
Edit: #6016 adds support for adapter options
In short, what are the main differences with #5522
In details
How adapters work
Chart._adapters._date
(private).chart._adapters._date
methods to manipulate timestamp.For example, the Luxon adapter (chartjs-adapter-luxon)
How it works
Timezone, locales and extra time features
When using adapters, the user have full control over the date lib and so can configure it globally. If at some point we get requests to have these configurations per charts, we will introduce adapter specific options that will be passed to the adapter parse / format methods (e.g.
scale.options.adapter.date.*
or whatever name). It's not part of this PR because we may not need this granularity, so let's wait until it's explicitly requested. Anyway, the time scale shouldn't have to deal directly with such features, neither try to abstract them.Closes #5522