9

我升级到最新的 Chart.JS 版本 3.0.2。我正在尝试获取要呈现的时间序列图。这是我的配置:

{
  type: 'line',
  data: {
    datasets: [{
      data: dataForChart
    }]
  },
  options: {
    scales: {
      x: {
        type: 'time'
      }
    }
  }
} 

我已经像这样导入了模块:

import ChartJS from 'chart.js/auto';

我得到的错误是:

Error: This method is not implemented: either no adapter can be found or an incomplete integration was provided.

关于我可能做错的任何提示?

这是一个存在该问题的代码沙箱:https ://codesandbox.io/s/throbbing-cdn-j6q2u?file=/src/App.js

4

4 回答 4

9

您需要安装和导入适配器,在您的情况下,它是时间序列的时刻适配器

npm install moment chartjs-adapter-moment --save

然后将其导入您的组件中: import 'chartjs-adapter-moment';

有关适配器的更多信息,请查看

于 2021-07-16T12:31:07.680 回答
6

您需要一个如上所述的适配器。看这里:https ://github.com/chartjs/chartjs-adapter-date-fns

一种选择是添加以下 CDN 链接。

<script src="https://cdn.jsdelivr.net/npm/chart.js/dist/chart.min.js"></script>
   
<script src="https://cdn.jsdelivr.net/npm/chartjs-adapter-date-fns/dist/chartjs-adapter-date-fns.bundle.min.js"></script>
于 2021-11-24T09:13:02.870 回答
3

如您的错误和文档中所述,您需要一个适配器将日期转换为日期对象,请参阅文档:https ://www.chartjs.org/docs/latest/axes/cartesian/time.html#date-adapters

于 2021-04-09T09:30:55.180 回答
0

我认为您应该像下面的代码一样将列表放入 x 中。

scales: {
  x: [{
    type: 'time'
  }]
}
于 2021-11-04T01:14:49.500 回答