Chartjs-How can have variable data be from a url in javascript

1๐Ÿ‘

โœ…

You need to fetch() the data from the url and then pass that through to the chart:

fetch('http://localhost:80/data') // Get the data from the url
    .then(response => response.json()) // Parse the response into json
    .then(data => {
        // The json from the url, use it to create the chart
        console.log(data);
    })
    .catch(e => {
        // Catch errors
        console.log(e);
    });

If you for example have a function called createChart(), you can pass the data like this:

// Replace the console.log(data) in the then() with the function
.then(createChart)

function createChart(data) {
   // Create chart here with json data
}

1๐Ÿ‘

you can use Fetch

 var barChartData = fetch('localhost:80/data')
        .then(function(response){
            return response.json();
        })
        .catch(err){
    //deal with error
    }
    
    //deal with response you can render it with a for loop

Leave a comment