Create chart from form value - Javascript Chart.js

Javascript examples for Chart.js:Chart Configuration

Description

Create chart from form value

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> 
      <style id="compiled-css" type="text/css">

canvas { background-color : #eee;
}


      </style> 
   </head> 
   <body> 
      <form>
          a: //from   w  w w  .  ja  v  a2 s .co m
         <input type="number" id="a">
         <br>
          b: 
         <input type="number" id="b">
         <br>
          c: 
         <input type="number" id="c">
         <br>
          d: 
         <input type="number" id="d">
         <br> 
         <button onclick="display(a,b,c,d)">display</button> 
      </form> 
      <canvas id="myChart"></canvas> 
      <script>
  var ctx = document.getElementById("myChart").getContext('2d');
  var myChart;
  function display(a, b, c, d) {
     event.preventDefault();
    myChart = new Chart(ctx, {
      type: 'bar',
      data: {
        labels: ["a", "b", "c", "d"],
        datasets: [{
          label: 'data',
          data: [a.value, b.value, c.value, d.value],
          backgroundColor: [
            'rgba(255, 99, 132, 0.5)',
            'rgba(54, 162, 235, 0.5)',
            'rgba(255, 206, 86, 0.5)',
            'rgba(75, 192, 192, 0.5)',
          ],
          borderColor: [
            'rgba(255,99,132,1)',
            'rgba(54, 162, 235, 1)',
            'rgba(255, 206, 86, 1)',
            'rgba(75, 192, 192, 1)',
          ],
        }]
      },
      options: {}
    });
  }

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

Related Tutorials