Chartjs-Displaying data from simple html form to display in chartjs

0πŸ‘

I am not sure your implementation. If you trying to get a value from an input into a chart. Won’t something like this work?

<canvas id="myChart" width="400" height="400"></canvas>
<script>
    var ctx = document.getElementById("myChart").getContext('2d');

    var testOutput = document.getElementById("welcome").value;

    var testValue1 = document.getElementById("test1").value;
    var testValue2 = document.getElementById("test2").value;
    var testValue3 = document.getElementById("test3").value;
    var testValue4 = document.getElementById("test4").value;
    var myChart = new Chart(ctx, {
        type: 'bar',
        data: {
            labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
            datasets: [{
                label: '# of Votes',
                data: [testValue1, testValue2, testValue3, testValue4],
                backgroundColor: [
                    'rgba(255, 99, 132, 0.2)',
                    'rgba(54, 162, 235, 0.2)',
                    'rgba(255, 206, 86, 0.2)',
                    'rgba(75, 192, 192, 0.2)',
                    'rgba(153, 102, 255, 0.2)',
                    'rgba(255, 159, 64, 0.2)'
                ],
                borderColor: [
                    'rgba(255,99,132,1)',
                    'rgba(54, 162, 235, 1)',
                    'rgba(255, 206, 86, 1)',
                    'rgba(75, 192, 192, 1)',
                    'rgba(153, 102, 255, 1)',
                    'rgba(255, 159, 64, 1)'
                ],
                borderWidth: 1
            }]
        },
        options: {
            scales: {
                yAxes: [{
                    ticks: {
                        beginAtZero:true
                    }
                }]
            }
        }
    });
</script>

Reference: http://www.chartjs.org/docs/latest/

0πŸ‘

Maybe you can pass data instead of the empty array

datasets: [{
            label: 'Balance by Year',
            data: [], // Empty Array
            backgroundColor: [
                    'rgba(255, 99, 132, 0.2)',
                    'rgba(54, 162, 235, 0.2)',
                    'rgba(255, 206, 86, 0.2)',
                    'rgba(75, 192, 192, 0.2)',
                    'rgba(153, 102, 255, 0.2)',
                    'rgba(255, 159, 64, 0.2)'
            ],
            borderColor: [
                    'rgba(255,99,132,1)',
                    'rgba(54, 162, 235, 1)',
                    'rgba(255, 206, 86, 1)',
                    'rgba(75, 192, 192, 1)',
                    'rgba(153, 102, 255, 1)',
                    'rgba(255, 159, 64, 1)'
            ],
            borderWidth: 1
    }]

You can first follow the latest Bar chart example and then reproduce it for yourself. http://www.chartjs.org/docs/latest/charts/bar.html

0πŸ‘

Here you go. This works.

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>SimpleAdd</title>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.3/Chart.min.js" charset="utf-8"></script>

</head>

<body>

  <form>
    Test:<br>
    <input id="test1" type="text">
    <input type="button" value="Add data" onclick="adddata()">
  </form>

  <canvas id="myChart"></canvas>
  <script type="text/javascript">


    var ctx = document.getElementById("myChart").getContext('2d');

    function adddata() {
      let test1 = document.getElementById("test1").value;

    //  myChart.data.labels[1] = testName1;
      myChart.data.datasets[0].data[0] = test1;
      myChart.update();
    }

    var myChart = new Chart(ctx, {
      type: 'bar',
      data: {
        labels: ["Red"],
        datasets: [{
          label: '# of Votes',
          data: [test1],
          backgroundColor: [
            'rgba(255, 99, 132, 0.2)'
          ],
          borderColor: [
            'rgba(255,99,132,1)'
          ],
          borderWidth: 1
        }]
      },
      options: {
        scales: {
          yAxes: [{
            ticks: {
              beginAtZero: true
            }
          }]
        }
      }
    });
  </script>
</body>

</html>

Leave a comment