Raj*_*ath 7 charts reactjs react-chartjs-2
例子How to display data on top of line chart
实施过程中遇到的问题:
Chartjs 插件“chartjs-plugin-datalabels:1.0.0”导入抛出以下错误
类型错误:无法读取未定义的属性“扩展”。
通过将标签插件包更改为测试版本解决了上述错误2.0.0-rc.1
这里提出了类似的问题,但解决方案无法解决我的问题。
任何形式的帮助将不胜感激。
import React, { Fragment } from "react";
import { Line } from "react-chartjs-2";
import "chartjs-plugin-datalabels";
const styles = {
dangerColor: {
color: "red"
},
geen: {
color: "green"
}
};
const data = {
labels: ["Jan", "Feb", "Mar", "Apr", "May"],
datasets: [
{
label: "Avg interest by month",
data: [0.7, 0.81, 0.71, 0.87, 0.9],
fill: false,
backgroundColor: "transparent",
borderColor: "#06a1e1",
tension: 0.1,
borderWidth: 4
}
]
};
const options = {
maintainAspectRatio: false,
scales: {
x: {
grid: {
display: false
}
},
y: {
display: false,
grid: {
display: false
}
}
},
plugins: {
legend: {
display: false
},
title: {
display: true,
text: "Avg interest by month (days)",
padding: {
bottom: 30
},
weight: "bold",
color: "#00325c",
font: {
size: 13
},
align: "start"
},
datalabels: {
display: true,
color: "white"
}
}
};
const LineChart = () => (
<Fragment>
<div style={{ height: "300px" }}>
<Line data={data} options={options} />
</div>
</Fragment>
);
export default LineChart;
Run Code Online (Sandbox Code Playgroud)
这就是我的 Package.json 依赖项的样子
{
"chart.js": "^3.3.2",
"chartjs-plugin-datalabels": "^2.0.0-rc.1",
"react": "^17.0.2",
"react-chartjs-2": "^3.0.3",
"react-dom": "^17.0.2",
"react-scripts": "^4.0.3",
"web-vitals": "^1.1.2"
}
Run Code Online (Sandbox Code Playgroud)
小智 29
React-chartjs-2 包包含一个可以在组件上设置的插件属性。
更改导入自:
import "chartjs-plugin-datalabels";
Run Code Online (Sandbox Code Playgroud)
到:
import ChartDataLabels from 'chartjs-plugin-datalabels';
Run Code Online (Sandbox Code Playgroud)
在您的组件上,添加保存导入值的插件变量。
从:
<Line data={data} options={options} />
Run Code Online (Sandbox Code Playgroud)
到:
<Line data={data} plugins={[ChartDataLabels]} options={options} />
Run Code Online (Sandbox Code Playgroud)
Raj*_*ath 10
我发现了问题并更新了沙箱
您可以在此处找到链接 https://codesandbox.io/s/quizzical-hooks-zcg91?file=/src/components/LineChart.jsx
问题出在 Chart.js 上
import { Chart } from "chart.js";
import ChartDataLabels from "chartjs-plugin-datalabels";
Chart.register(ChartDataLabels);
Run Code Online (Sandbox Code Playgroud)
chart.js 3.x版本更新了插件注册方法
| 归档时间: |
|
| 查看次数: |
15322 次 |
| 最近记录: |