React Chart.js 时间刻度不适用于 date-fns 适配器

发布于 2025-01-09 16:30:12 字数 1358 浏览 1 评论 0原文

我试图让时间刻度(x 轴)与 date-fns 适配器一起工作,并为 chartjs-adapter-date-fns 获取“错误”,这可能是问题所在:

Could not find a declaration file for module 'chartjs-adapter-date-fns'. 'C:/react/envirodata/node_modules/chartjs-adapter-date-fns/dist/chartjs-adapter-date-fns.js' 
implicitly has an 'any' type.
Try `npm i --save-dev @types/chartjs-adapter-date-fns` if it exists or add a new declaration  (.d.ts) file containing `declare module 'chartjs-adapter-date-fns';`ts(7016)

我的设置是这样的:

import {
  Chart as ChartJS,
  CategoryScale,
  LinearScale,
  PointElement,
  LineElement,
  Title,
  Tooltip,
  Legend,
} from 'chart.js';

import "chartjs-adapter-date-fns";
import {Line} from 'react-chartjs-2';
import zoomPlugin from "chartjs-plugin-zoom";


ChartJS.register(
  CategoryScale,
  LinearScale,
  PointElement,
  LineElement,
  Title,
  Tooltip,
  Legend, 
  zoomPlugin
);

选项是这样的:

plugins: {
  scales: {
    x: {
        type: 'time',  
        adapters: {
          date: {
              locale: en
          }
        ...

我尝试了不同的日期格式的时间刻度。例如:

  1. 标签:[20220224,20220225...]
  2. 数据:[{x:1617253200000,y:1},...]
  3. 数据:[{x:2022-02-24,y:1},... ]

但似乎没有任何作用,日期像字符串一样显示。因此,除了 chartjs-adapter-date-fns 之外,我不确定设置是否有问题。

I am trying to get the time scale (x axis) work with date-fns adapter and getting "error" for chartjs-adapter-date-fns which could be the issue:

Could not find a declaration file for module 'chartjs-adapter-date-fns'. 'C:/react/envirodata/node_modules/chartjs-adapter-date-fns/dist/chartjs-adapter-date-fns.js' 
implicitly has an 'any' type.
Try `npm i --save-dev @types/chartjs-adapter-date-fns` if it exists or add a new declaration  (.d.ts) file containing `declare module 'chartjs-adapter-date-fns';`ts(7016)

My setup is like this:

import {
  Chart as ChartJS,
  CategoryScale,
  LinearScale,
  PointElement,
  LineElement,
  Title,
  Tooltip,
  Legend,
} from 'chart.js';

import "chartjs-adapter-date-fns";
import {Line} from 'react-chartjs-2';
import zoomPlugin from "chartjs-plugin-zoom";


ChartJS.register(
  CategoryScale,
  LinearScale,
  PointElement,
  LineElement,
  Title,
  Tooltip,
  Legend, 
  zoomPlugin
);

and the options are like this:

plugins: {
  scales: {
    x: {
        type: 'time',  
        adapters: {
          date: {
              locale: en
          }
        ...

I have tried different date formats for time scale. For example:

  1. labels : [20220224, 20220225...]
  2. data : [{x: 1617253200000, y: 1}, ...]
  3. data : [{x: 2022-02-24, y: 1}, ...]

But nothing seems to work and dates are displayed like as a string. So apart from the chartjs-adapter-date-fns, I am not sure if there is not an issue with setup.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

缱倦旧时光 2025-01-16 16:30:12

从“chart.js”导入 TimeScale 而不是 CategoryScale

import TimeScale from 'chart.js' instead of CategoryScale

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文