问题
I'm building an Angular 2 app and use Chart.js 2.0 to display 'time' data. Unfortunately I am not able to change the locale of moment.js.
I npm installed moment and import moment.js and pl.js using SystemJS, but Chart.js complains that moment.js has to be loaded before Chart.js (not sure how to accomplish that, in every source file moment is declared before chartjs).
Then I tried the Chart.bundle.min.js (with moment included), which works but only in english. I am not able to change the locale to 'pl'.
The following line has no effect with Chart.bundle.min.js and dates are still displayed in english.
moment.locale('pl');
Does Chart.js have an own function to change locale? The docs don't specify anything about hat.
回答1:
Chart.js won't do date formatting for you - the amount of possibilities and locales are so vast it fits well beyond the scope of the project - in face there are libraries just for different formatting of javascript dates. One good example could be something like http://momentjs.com/.
For plotting data you should use seconds/minutes/hours as most appropriate for your data. You can customise how you format this in your interpolated JS string for labels - scaleLabel. For chart labels, you should just use formatted strings as you see most fit.
source: https://github.com/chartjs/Chart.js/issues/79
回答2:
You must include your moment locale file on your page:
Ex.: You can find your locale using CDN https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.14.1/locale/pl.js
Ex.: Bower
<script src="/bower_components/moment/moment.js"></script>
<script src="/bower_components/moment/locale/de.js"></script>
回答3:
Moment's locale must be set before the chart load else the chart must be updated with update(). https://github.com/chartjs/Chart.js/issues/3108
来源:https://stackoverflow.com/questions/38202587/chart-js-moment-locale