我正在尝试这个reactjs流式图表的例子https://nagix.github.io/chartjs-plugin-streaming/latest/tutorials/react/app.html
但我似乎无法使它工作,它编译时没有问题,但在浏览器中没有显示任何内容,当我检查浏览器控制台时,它有一条错误消息,上面写着:
App.js:7 Uncaught TypeError: react_chartjs_2__WEBPACK_IMPORTED_MODULE_5__.Chart.register is not a function
at Module../src/App.js (App.js:7:1)
at Module.options.factory (react refresh:6:1)
at __webpack_require__ (bootstrap:24:1)
at fn (hot module replacement:61:1)
at Module../src/index.js (App.js:46:1)
at Module.options.factory (react refresh:6:1)
at __webpack_require__ (bootstrap:24:1)
at startup:7:1
at startup:7:1
./src/App.js @ App.js:7
options.factory @ react refresh:6
__webpack_require__ @ bootstrap:24
fn @ hot module replacement:61
./src/index.js @ App.js:46
options.factory @ react refresh:6
__webpack_require__ @ bootstrap:24
(anonymous) @ startup:7
(anonymous) @ startup:7
我尝试了Chart.plugins.register(StreamingPlugin(,然后错误消息是:
App.js:7 Uncaught TypeError: Cannot read properties of undefined (reading 'register')
at Module../src/App.js (App.js:7:1)
at Module.options.factory (react refresh:6:1)
at __webpack_require__ (bootstrap:24:1)
at fn (hot module replacement:61:1)
at Module../src/index.js (App.js:46:1)
at Module.options.factory (react refresh:6:1)
at __webpack_require__ (bootstrap:24:1)
at startup:7:1
at startup:7:1
./src/App.js @ App.js:7
options.factory @ react refresh:6
__webpack_require__ @ bootstrap:24
fn @ hot module replacement:61
./src/index.js @ App.js:46
options.factory @ react refresh:6
__webpack_require__ @ bootstrap:24
(anonymous) @ startup:7
(anonymous) @ startup:7
您需要从Chart.js而不是从Reacht-chartjs-2导入Chart来注册它,如下所示:
import {Chart} from 'chart.js';
import {Chart as ReactChart} from 'react-chartjs-2';
import StreamingPlugin from 'chartjs-plugin-streaming';
Chart.register(StreamingPlugin);
从导入react-chattjs-2中删除图表并从Chart.js/auto导入Chart,这样您的导入就会像这样。
import { Line } from "react-chartjs-2";
import "chartjs-adapter-luxon";
import StreamingPlugin from "chartjs-plugin-streaming";
import "./App.css";
import Chart from "chart.js/auto";
Chart.register(StreamingPlugin);```
Hope this helped.
对我有效的是使用chart.js@3.8.0
、chartjs-chart-geo@3.7.2
和react-chartjs-2@4.1.0
。就像在这个代码笔里。