简体   繁体   English

使用chart.js时如何解决chart_js.Chart.register问题

[英]How can I solve chart_js.Chart.register problem when using chart.js

Am developing a user dashboard application using Next.js.正在使用 Next.js 开发用户仪表板应用程序。 I query my client from a node and MongoDB server.我从节点和 MongoDB 服务器查询我的客户端。 I need to load my graphs using chart.js.我需要使用 chart.js 加载我的图表。 But when I run the code I experience this error.但是当我运行代码时,我遇到了这个错误。 TypeError: chart_js.Chart.register is not a function. TypeError:chart_js.Chart.register 不是函数。 Here is the code snippet:这是代码片段:

import {Chart as ChartJS} from 'chart.js';
import { Bar } from 'react-chartjs-2';

import React, { useEffect, useReducer } from 'react';
import Layout from '../../components/Layout';
import { getError } from '../../utils/error';

ChartJS.register(
  CategoryScale,
  LinearScale,
  BarElement,
  Title,
  Tooltip,
  Legend
);

2 Things, you are not importing all the elements you are trying to register and since Chart.register is not a function I suspect you are using V2 of Chart.js which is non treeshakable. 2 件事,你没有导入你试图注册的所有元素,因为 Chart.register 不是一个函数,我怀疑你使用的是不可摇动的 Chart.js V2。 So you can't and don't have to use register to show the charts unless you are going to upgrade to V3 and use treeshaking因此,除非您要升级到 V3 并使用 treeshaking,否则您不能也不必使用 register 来显示图表

If you want to register any plugins in V2 you need to register them on the plugins of the chart like so:如果您想在 V2 中注册任何插件,您需要在图表的插件上注册它们,如下所示:

Chart.plugins.register(plugins);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM