chartjs / chartjs-adapter-date-fns

date-fns adapter for Chart.js
MIT License
107 stars 34 forks source link

Adds option to override default formats in adapter #27

Closed kennethormandy closed 3 years ago

kennethormandy commented 3 years ago

I think it’s possible to change time.scale with this adapter, but not possible to change time.displayFormat using this adapter.

For example, you can change time.unit to 'month' to use date-fns to display the month, but you cannot use time.displayFormats to change the display of month from the default of MMM yyyy to your custom string MMMM.

This Pull Request adds an option to pass in formats to modify the default FORMATS object.

If there’s a different way of doing that already, that’s fine too! I intentionally didn’t call it displayFormats as it seems confusing to have time.displayFormats and also adapters.data.displayFormats, but maybe it’s also confusing to have them not match.

This does use Object.assign for convenience, which I think works fine based on Chart.js’ browser support. If that’s an issue, the option could override the whole object instead.

kurkle commented 3 years ago

This already works, at least with @next version: https://codepen.io/kurkle/pen/qBqOxwK What versions are you using?

kennethormandy commented 3 years ago

I wasn’t aware there was a @next version, so I was using v1.0.0 from npm.

Since it looks like that’s already handled, I’ll close this. It might be worth adding a note about the latest version in the README. Thanks for the sample!