Adding data to dynamically from tables - Javascript Chart.js

Javascript examples for Chart.js:Chart Data

Description

Adding data to dynamically from tables

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.6.0/Chart.min.js"></script> 
      <script type="text/javascript">
    window.onload=function(){//from  ww w.  j  a  va  2  s  . c  o  m
function results(answers,resultsArray){
    return answers.map( (a) => {
        var nb = 0;
        resultsArray.forEach( (r) => { if(a === r)nb++ } );
        return nb;
    })
}
var answers = ["Answer1","Answer2","Answer3","Answer4"];
var resultsArray =["Answer1","Answer1","Answer1","Answer1","Answer1","Answer2","Answer2","Answer2"];
var ctx = document.getElementById("myChart").getContext('2d');
var myChart = new Chart(ctx, {
    type: 'bar',
    data: {
        labels: answers,
        datasets: [{
            label: 'tab',
            data: results(answers,resultsArray)
        }]
    }
});
    }

      </script> 
   </head> 
   <body> 
      <canvas id="myChart" width="400" height="400"></canvas> 
      <script>

      </script>  
   </body>
</html>

Related Tutorials