I am having a problem understanding how I can get fetched data to be displayed on a chart using chart.js
const [backendData, setBackendData] = useState([{}])
useEffect(() => {
Axios.get('http://localhost:3001/get').then((response) =>{
console.log(response)
setBackendData(response.data)
})
}, [])
backendData.map((val) => {
const total = val.total
})
mondayData.datasets.data = total
// monday chart data
var mondayData = {
labels: ["7 AM", "8 AM", "9 AM", "10 AM", "11 AM", "12 PM", "1 PM", "2 PM", "3 PM"],
datasets: [{
data: [2, 1, 1, 6, 4, 7, 8, 14, 13],
label: "Empty parking spaces",
backgroundColor: "rgb(39, 63, 150, 0.5)",
}]
}
// monday chart options
var mondayOptions = {
}
useEffect(() => {
// var test = document.getElementById("0").getContext('2d');
// test.destroy();
// monday chart
var mondayCtx = document.getElementById('mondayChart').getContext('2d');
let mondayChartStatus = Chart.getChart("mondayChart");
if (mondayChartStatus != undefined) {
mondayChartStatus.destroy();
}
var mondayChart = new Chart(mondayCtx, {
type: 'line',
data: mondayData
});
The backendData variable has one value {total: 22} which is pulled from the database
which is then (I think) getting used to set the const total variable
Right now what I am trying to do is get the 22 to be displayed as the first value on the chart.
I am using the mondayData.datasets[0].data = total to access it but I get an error “TypeError: Cannot read properties of undefined (reading ‘datasets’)”
I just want to use my fetched data from a database to be used as the data for the chart