问题描述
我有数据Im正在尝试在chartData
的控制台中显示,但是页面仍未显示实际图表。我不确定为什么。我在控制台中也没有任何错误。图表数据对象具有我在控制台中显示的所有内容。当我检查页面时,画布上有我设置的所有道具。我可能不确定是什么小问题。我收到一个错误,指出“ data
在ChartComponent
中标记为必需,但其值为undefined
。
import React,{ useEffect,useState } from "react";
import "./style.css";
import axios from "axios";
import { Doughnut } from "react-chartjs-2";
// import Chart from 'chart.js';
import Dropdown from "../Dropdown.js";
import { Button } from "react-bootstrap"
function Census() {
const [censusData,setcensusData] = useState([]);
const [county,setCounty] = useState("Williamson County");
const [countyList,setCountyList] = useState([]);
const [chartData,setChartData] = useState();
useEffect(() => {
getCensus();
getCounties();
},[]);
//to get data based on users county selection
function getCensus(e) {
axios.get('/api/census/' + e).then((response) => {
setcensusData(response.data);
censusDataSetter(response.data)
}).catch(err => {
console.log(err);
});
};
//get all counties from db
function getCounties() {
if (countyList.length) { return }
axios.get("/api/census/").then(res => res.data)
.then(response => {
// console.log(response);
setCountyList(response);
})
};
function handleFormSubmit(e) {
e.preventDefault();
getCensus(county)
}
function censusDataSetter(e) {
console.log(e);
console.log(e[0].insured)
setChartData(
{
name: 'Individuals With Health Insurance',data: {
datasets: [{
data: [e[0].insured,e[0].uninsured],backgroundColor: ['#a3a4a5','#d90429']
}],labels: [
"Insured","Uninsured"
]
}
}
)
}
return (
<>
{console.log('censusData'),console.log(chartData)}
<form onSubmit={e => handleFormSubmit(e)}>
<Dropdown countyList={countyList} county={county} setCounty={setCounty} />
<Button type="submit" value="Submit">Search</Button>
</form>
<div className="chart">
{censusData[0] ?
<Doughnut data={chartData}
options={{
responsive: true,maintainAspectRatio: true
}}
// height={500}
// width={700}
/>
:
<p>Select a County</p>
}
</div>
</>
)
}
export default Census;
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)