Chartjs-How to plot Json data in Chart js

0👍

You can loop through the json return by data.php and set the data on the options object, please check this jsfiddle

$(document).ready(function() {
    var options = {
        type: 'bar',
        data: {
          labels: ["Beta Value", "Charlie Value", "Delta Value"],
          datasets: [{
            label: 'Awesome Dataset',
            data: [ 302, 175, 50],
            backgroundColor: "rgba(75, 192, 192, 1)"
          }]
        }
    };

   var chart = new Chart($("#chart"), options);

   var data = [  
         {  
          "Login_Funnel":"6",
          "Playlist_Funnel_1":"1",
          "Logout_Funnel_2":"0"
       } 
      ];

    options.data.labels = []; //reset the labels
    options.data.datasets = [{
        label: 'Awesome Dataset',
        data: [],
        backgroundColor: "rgba(75, 192, 192, 1)"
    }]; // reset the previous data

    for (var i in data) { // iterate the data array    
        for (var y in data[i]) { //iterate the object
            if (i == 0) {
                options.data.labels.push(y);
            }
            options.data.datasets[i].data.push(data[i][y]);
        }
    }

    chart.update(); // update the chart with the new data
});

Leave a comment