【问题标题】:Change locale for Chart.js in React在 React 中更改 Chart.js 的语言环境
【发布时间】:2018-12-13 10:40:52
【问题描述】:

我正在尝试在 React 应用程序中设置 Chart.js 的语言环境。

方法一

Use the bundledChart.js 版本,因为我在应用程序的其余部分不使用moment:

import Chart from 'chart.js/dist/Chart.bundle.min.js';

问题:如何设置语言环境? moment 不能作为全局对象使用,我也找不到它作为 Chart 或其任何实例的属性,也没有用于修改语言环境的 API。

方法二

Use the non bundled 版本并包含moment 我自己。

import * as moment from 'moment';
import 'moment/locale/es';
import Chart from 'chart.js/dist/Chart.min.js';

然后在渲染图表之前的代码中:

moment.locale('es');
chart = new Chart(type, options);

问题:但这确实会引发错误:

Error: Chart.js - Moment.js could not be found! You must include it before Chart.js to use the time scale. Download at https://momentjs.com

我尝试将moment 分配给本地对象为window.moment = moment,但同样的错误。

(方法)Hack 3

为了测试,我将moment.js 和es.js 文件复制到public 文件夹,并将它们作为<script> 标签包含在index.html 中,它有效,但我不想要这样的黑客攻击。

注意事项

我知道一些用于 Chart.js 的 React 库,但不想使用它们。

我读过其他帖子,如果在渲染之前更改了区域设置,他们建议重新加载图表。

[https://stackoverflow.com/questions/38202587/chart-js-moment-locale][https://stackoverflow.com/questions/38202587/chart-js-moment-locale]

https://github.com/apertureless/vue-chartjs/issues/101

https://github.com/chartjs/Chart.js/issues/5284

https://github.com/chartjs/Chart.js/issues/2906

【问题讨论】:

    标签: reactjs chart.js momentjs


    【解决方案1】:

    在 Vue 中我尝试了 whit。

    import Moment from 'moment';
    import 'moment/locale/es';
    import Chart from 'chart.js';
    Moment.locale('es');
    

    它对我有用。

    我正在查看我看到的 chart.js 代码蚂蚁,他们将时刻称为 Moment 而不是 moment

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-07
      • 1970-01-01
      • 2012-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多