简体   繁体   English

图表 Js 未显示动态传递的数据值

[英]Chart Js not showing data value which is passed dynamically

I am trying to create a corona virus tracker using react and chart js.我正在尝试使用 react 和 chart js 创建一个电晕病毒跟踪器。 I have fetched numbers of confirmed, recovered, and deaths from the API and stored them to state variable using useState.我从 API 中获取了确认、恢复和死亡的数量,并使用 useState 将它们存储到 state 变量中。 Then I used the state variable as an array for chart js data properties.然后我使用 state 变量作为图表 js 数据属性的数组。 But the bars of the chart isnt showing.但是图表的条形图没有显示。 Like the value of the data property isnt set properly.就像数据属性的值没有正确设置一样。 Here look at my code.这里看看我的代码。

const [confirmedpeople, setconfirmedpeople] = useState('')
const [recoveredpeople, setrecoveredpeople] = useState('')
const [deathspeople, setdeathspeople] = useState('')

const url= `https://covid19.mathdro.id/api`;
const fetchData= async()=>{
try {
const datas= await axios.get(url)
setconfirmedpeople(datas.data.confirmed.value)
setrecoveredpeople(datas.data.recovered.value)
setdeathspeople(datas.data.recovered.value)
} catch (error) {
console.log(error)
}
}
useEffect(() => {
fetchData()
}, [])


return (
<>
<Bar
data= {{
labels:['confirmed', 'recovered', 'deaths'],
datasets: [{
label: 'CORONA UPDATE',
data: [{confirmedpeople}, {recoveredpeople}, {deathspeople}],
/>

I am trying to create a corona virus tracker using react and chart js.我正在尝试使用 react 和 chart js 创建一个电晕病毒跟踪器。 I have fetched numbers of confirmed, recovered, and deaths from the API and stored them to state variable using useState.我从 API 中获取了确认、恢复和死亡的数量,并使用 useState 将它们存储到 state 变量中。 Then I used the state variable as an array for chart js data properties.然后我使用 state 变量作为图表 js 数据属性的数组。 But the bars of the chart isnt showing.但是图表的条形图没有显示。 Like the value of the data property isnt set properly.就像数据属性的值没有正确设置一样。 Here look at my code.这里看看我的代码。

const [confirmedpeople, setconfirmedpeople] = useState('')
const [recoveredpeople, setrecoveredpeople] = useState('')
const [deathspeople, setdeathspeople] = useState('')

const url= `https://covid19.mathdro.id/api`;
const fetchData= async()=>{
try {
const datas= await axios.get(url)
setconfirmedpeople(datas.data.confirmed.value)
setrecoveredpeople(datas.data.recovered.value)
setdeathspeople(datas.data.recovered.value)
} catch (error) {
console.log(error)
}
}
useEffect(() => {
fetchData()
}, [])


return (
<>
<Bar
data= {{
labels:['confirmed', 'recovered', 'deaths'],
datasets: [{
label: 'CORONA UPDATE',
data: [{confirmedpeople}, {recoveredpeople}, {deathspeople}],
/>

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

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