Chart.js Bar chart load data based on Label - Javascript Chart.js

Javascript examples for Chart.js:Bar Chart

Description

Chart.js Bar chart load data based on Label

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.1.1.js"></script> 
      <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.4.0/Chart.min.js"></script> 
      <script type="text/javascript">
    var VanillaRunOnDomReady = function() {
 var monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
   "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
 ];/*from  w w w.j a  v a  2s .co  m*/
 var startmonth = ["Dec", "Nov", "Oct", "Sep", "Aug", "Jul",
   "Jun", "May", "Apr", "Mar", "Feb", "Jan"
 ];
 var itemMonths = [],
   itemCountList = [];
 var start;
 var end = 11;
 var month;
 var year;
 var date = new Date();
 month = date.getMonth();
 year = parseInt(2016);
 start = 0;
 var Result = [];
 Result.push({
   Months: "Feb",
   Years: 2016,
   Counts: 6
 });
 Result.push({
   Months: "Mar",
   Years: 2016,
   Counts: 1
 });
 Result.push({
   Months: "Apr",
   Years: 2016,
   Counts: 1
 });
 for (var i = 0; i < 12; i++) {
   var months = monthNames[start];
   var monthValue = 0;
   itemMonths.push(months + year);
   start = start + 1;
   if (start == 12) {
     start = 0;
     year = year + 1;
   }
   var dataObj = $.grep(Result, function(a) {
     return a.Months == months
   })[0];
   var monthValue = dataObj !== undefined ? dataObj.Counts : 0;
   itemCountList.push(monthValue)
 }
 console.log(itemCountList);
 var mybarChart = null;
 var ctx = document.getElementById("mybarChart");
 mybarChart = new Chart(ctx, {
   type: 'bar',
   data: {
     labels: itemMonths,
     datasets: [{
       label: 'Total Count',
       backgroundColor: "#26B99A",
       data: itemCountList
     }]
   },
   options: {
     scales: {
       yAxes: [{
         ticks: {
           beginAtZero: true,
           steps: 10,
           stepValue: 5,
           max: 100
         }
       }],
       xAxes: [{
         steps: 10,
         stepValue: 5,
         max: 12
       }]
     }
   }
 });
    }
var alreadyrunflag = 0;
if (document.addEventListener)
    document.addEventListener("DOMContentLoaded", function(){
        alreadyrunflag=1;
        VanillaRunOnDomReady();
    }, false);
else if (document.all && !window.opera) {
    document.write('<script type="text/javascript" id="contentloadtag" defer="defer" src="javascript:void(0)"><\/script>');
    var contentloadtag = document.getElementById("contentloadtag")
    contentloadtag.onreadystatechange=function(){
        if (this.readyState=="complete"){
            alreadyrunflag=1;
            VanillaRunOnDomReady();
        }
    }
}
window.onload = function(){
  setTimeout("if (!alreadyrunflag){VanillaRunOnDomReady}", 0);
}

      </script> 
   </head> 
   <body> 
      <form method="POST"> 
         <div> 
            <canvas id="mybarChart"></canvas> 
         </div> 
      </form>  
   </body>
</html>

Related Tutorials