Data with pair X and Y values - Javascript Chart.js

Javascript examples for Chart.js:Chart Data

Description

Data with pair X and Y values

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://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.2.1/Chart.min.js"></script> 
      <script type="text/javascript">
    window.onload=function(){//  ww  w.  j  a  v a 2 s .  c  o m
var APIarray = [
    ['08/09/2016', 12],
    ['09/09/2016', 19]
];
var ctx = document.getElementById("myChart");
var data = {
   labels: [],
    datasets: [{
        label: "My dataset",
        backgroundColor: "rgba(75,192,192,0.4)",
        borderColor: "rgba(75,192,192,1)",
        data: []
    }]
};
Chart.pluginService.register({
    beforeInit: function(chart) {
        var data = chart.config.data;
        for (var i = 0; i < APIarray.length; i++) {
           data.labels.push(APIarray[i][0]);
            data.datasets[0].data.push(APIarray[i][1]);
        }
    }
});
var myBarChart = new Chart(ctx, {
    type: 'bar',
    data: data,
    options: {
        scales: {
            yAxes: [{
                ticks: {
                    beginAtZero:true
                }
            }]
        }
    }
});
    }

      </script> 
   </head> 
   <body> 
      <canvas id="myChart" height="200"></canvas>  
   </body>
</html>

Related Tutorials