Charts.js - json output to chart - Javascript Chart.js

Javascript examples for Chart.js:Chart Configuration

Description

Charts.js - json output to chart

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <title>ChartJS Line Chart</title> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <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  .ja  v a 2s. c  o  m*/
var dataJSON = [{
  "day": "2017-11-15",
  "quantity": "72"
}, {
  "day": "2017-11-16",
  "quantity": "157"
}, {
  "day": "2017-11-17",
  "quantity": "130"
}, {
  "day": "2017-11-18",
  "quantity": "91"
}, {
  "day": "2017-11-19",
  "quantity": "96"
}];
var labelsData = [];
var datasetData = [];
dataJSON.forEach(function(packet) {
  labelsData.push(packet.day);
  datasetData.push(parseFloat(packet.quantity));
});
var chartdata = {
  type: 'bar',
  data: {
    labels: labelsData,
    datasets: [{
      label: 'DAYS',
      backgroundColor: 'rgba(200, 200, 200, 0.75)',
      borderColor: 'rgba(200, 200, 200, 0.75)',
      hoverBackgroundColor: 'rgba(200, 200, 200, 1)',
      hoverBorderColor: 'rgba(200, 200, 200, 1)',
      data: datasetData
    }]
  }
}
var ctx = document.getElementById('chartContainer').getContext('2d');
new Chart(ctx, chartdata);
    }

      </script> 
   </head> 
   <body>  
      <canvas id="chartContainer" width="600" height="400"></canvas>   
   </body>
</html>

Related Tutorials