React Chart.js 时间刻度不适用于 date-fns 适配器
我试图让时间刻度(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
}
...
我尝试了不同的日期格式的时间刻度。例如:
- 标签:[20220224,20220225...]
- 数据:[{x:1617253200000,y:1},...]
- 数据:[{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:
- labels : [20220224, 20220225...]
- data : [{x: 1617253200000, y: 1}, ...]
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从“chart.js”导入 TimeScale 而不是 CategoryScale
import TimeScale from 'chart.js' instead of CategoryScale