我的反应应用只是不显示图表
My react app is just not displaying the graph
我正在尝试使用 react-chartjs-2 制作条形图。每当我 运行 我的 React 应用程序使用以下代码时,它只显示黑页。我在这里做错了什么。
这是我的 Chart.js
代码
import React,{Component} from "react";
import { Bar } from 'react-chartjs-2';
class Chart extends Component{
constructor(props){
super(props);
this.state = {
chartData:{
labels: ['Boston','Worcestor',
'Springfield','Lowell','Cambridge','New bedford'],
datasets: [
{
label: 'Population',
data: [
641712,147895,12479546,1457897,127955,98745
],
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(255, 159, 64, 0.2)',
'rgba(255, 99, 131, 0.2)'
]
}
]
}
}
}
render(){
return(
<div className="chart">
<Bar
data={this.state.chartData}
options={{
maintainAspectRatio: false
}}
/>
</div>
)
}
}
export default Chart;
您需要将此行添加到导入中:import Chart from 'chart.js/auto';
我正在尝试使用 react-chartjs-2 制作条形图。每当我 运行 我的 React 应用程序使用以下代码时,它只显示黑页。我在这里做错了什么。 这是我的 Chart.js
代码import React,{Component} from "react";
import { Bar } from 'react-chartjs-2';
class Chart extends Component{
constructor(props){
super(props);
this.state = {
chartData:{
labels: ['Boston','Worcestor',
'Springfield','Lowell','Cambridge','New bedford'],
datasets: [
{
label: 'Population',
data: [
641712,147895,12479546,1457897,127955,98745
],
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(255, 159, 64, 0.2)',
'rgba(255, 99, 131, 0.2)'
]
}
]
}
}
}
render(){
return(
<div className="chart">
<Bar
data={this.state.chartData}
options={{
maintainAspectRatio: false
}}
/>
</div>
)
}
}
export default Chart;
您需要将此行添加到导入中:import Chart from 'chart.js/auto';