1

I'm trying to learn chart.js and have the following code that displays a graph.

However, it only loads the data once. If I change the values in the textbox inputs and click the button again, it doesn't change the chart at all.

I tried playing with the info here: https://www.chartjs.org/docs/latest/developers/updates.html but just can't get it to work.

function graphGraph() {

    let R = document.getElementById("RedVal").value;
    let B = document.getElementById("BlueVal").value;
    let Y = document.getElementById("YellowVal").value;
    let G = document.getElementById("GreenVal").value;
    let P = document.getElementById("PurpleVal").value;
    let O = document.getElementById("OrangeVal").value;

    let ctx = document.getElementById('myChart').getContext('2d');
    let myChart = new Chart(ctx, {
        type: 'bar',
        data: {
            labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'],
            datasets: [{
                label: '# of Votes',
                //data: [12, 19, 3, 5, 2, 3],
                data: [R, B, Y, G, P, O],
                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: {
                y: {
                    beginAtZero: true
                }
            }
        }
    });
}
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<div>
    <h3>Example Graph:</h3>
    <br />
    <table style="width:70%;" border="1" align="center">
        <tr>
            <th>Colour</th>
            <th>Value</th>
        </tr>
        <tr>
            <td>Red =</td>
            <td><input id="RedVal" min="0" type="text" /></td>
        </tr>
        <tr>
            <td>Blue =</td>
            <td><input id="BlueVal" min="0" type="text" /></td>
        </tr>
        <tr>
            <td>Yellow =</td>
            <td><input id="YellowVal" min="0" type="text" /></td>
        </tr>
        <tr>
            <td>Green =</td>
            <td><input id="GreenVal" min="0" type="text" /></td>
        </tr>
        <tr>
            <td>Purple =</td>
            <td><input id="PurpleVal" min="0" type="text" /></td>
        </tr>
        <tr>
            <td>Orange =</td>
            <td><input id="OrangeVal" min="0" type="text" /></td>
        </tr>
    </table>
</div>

<div>
    <input id="graphGraph" type="button" value="graphGraph" onclick="graphGraph()" />
    <canvas id="myChart"></canvas>
</div>

1 Answer 1

2

As the error states the canvas is already in use so you would need to update it instead like so:

let myChart = undefined;

function graphGraph() {

  let R = document.getElementById("RedVal").value;
  let B = document.getElementById("BlueVal").value;
  let Y = document.getElementById("YellowVal").value;
  let G = document.getElementById("GreenVal").value;
  let P = document.getElementById("PurpleVal").value;
  let O = document.getElementById("OrangeVal").value;

  if (Chart.getChart(myChart) === undefined) {
    let ctx = document.getElementById('myChart').getContext('2d');
    myChart = new Chart(ctx, {
      type: 'bar',
      data: {
        labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'],
        datasets: [{
          label: '# of Votes',
          //data: [12, 19, 3, 5, 2, 3],
          data: [R, B, Y, G, P, O],
          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: {
          y: {
            beginAtZero: true
          }
        }
      }
    });
  } else {
    myChart.data.datasets[0].data = [R, B, Y, G, P, O];
    myChart.update();
  }
}
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<div>
  <h3>Example Graph:</h3>
  <br />
  <table style="width:70%;" border="1" align="center">
    <tr>
      <th>Colour</th>
      <th>Value</th>
    </tr>
    <tr>
      <td>Red =</td>
      <td><input id="RedVal" min="0" type="text" /></td>
    </tr>
    <tr>
      <td>Blue =</td>
      <td><input id="BlueVal" min="0" type="text" /></td>
    </tr>
    <tr>
      <td>Yellow =</td>
      <td><input id="YellowVal" min="0" type="text" /></td>
    </tr>
    <tr>
      <td>Green =</td>
      <td><input id="GreenVal" min="0" type="text" /></td>
    </tr>
    <tr>
      <td>Purple =</td>
      <td><input id="PurpleVal" min="0" type="text" /></td>
    </tr>
    <tr>
      <td>Orange =</td>
      <td><input id="OrangeVal" min="0" type="text" /></td>
    </tr>
  </table>
</div>

<div>
  <input id="graphGraph" type="button" value="graphGraph" onclick="graphGraph()" />
  <canvas id="myChart"></canvas>
</div>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.