How to plot Json data in Chart js - Javascript Chart.js

Javascript examples for Chart.js:Chart Json Data

Description

How to plot Json data in Chart js

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript" src="https://code.jquery.com/jquery-3.2.1.min.js"></script> 
      <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.3.0/Chart.js"></script> 
      <script type="text/javascript">
    window.onload=function(){//  w  w w. j a  v  a2 s  .c o m
$(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 = [];
    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();
});
    }

      </script> 
   </head> 
   <body> 
      <canvas id="chart"></canvas>  
   </body>
</html>

Related Tutorials