I am attempting to create a webpage that creates a simple chart using chart.js. I want to store all data related to the chart into a separate file ./file.json. When I attempt to load the html page while attempting to load data from ./file.json into chartdata, the chart fails to appear, but when I insert the data directly into script.js, it works.
Here is my faulty code within the script:
const chartdata = require('./file.json');
let myChart = document.getElementById('myChart').getContext('2d');
let massPopChart = new Chart(myChart, {
type:'bar',
data: {
labels:[ 'Boston', 'Worcester', 'Springfield', 'Lowell', 'Dick' ],
datasets:[{
label: 'population',
data: chartdata
}]
},
options: {}
});
The chart successfully appears in the html page when I insert the data directly:
let myChart = document.getElementById('myChart').getContext('2d');
let massPopChart = new Chart(myChart, {
type:'bar',
data: {
labels:[ 'Boston', 'Worcester', 'Springfield', 'Lowell', 'Dick' ],
datasets:[{
label: 'population',
data: [135850, 52122, 148825, 16939, 9763]
}]
},
options: {}
});
Any suggestions?