Chartjs-Loading data into chart on html page

1👍

You need to pass the data to charts.js as an array instead of just as a JSON file. I’ve included an example below. It uses the fs library, which i think is included in node by default.

const fs = require("fs");
let myChart = document.getElementById('myChart').getContext('2d');

// read file sample.json file
fs.readFile('./file.json',
    // callback function that is called when reading file is done
    function(err, data) { 
        // json data
        var jsonData = data;
 
        // parse json
        var jsonParsed = JSON.parse(jsonData);
    }
);

let massPopChart = new Chart(myChart, {
    type:'bar',
    data: {
        labels:[ 'Boston', 'Worcester', 'Springfield', 'Lowell', 'Dick' ],
        datasets:[{
            label: 'population',
            data: jsonParsed
        }]
    },
    options: {}
}); 

Leave a comment