0

I've been having some trouble with Chart.js. I'm making a voting system that I want to dynamically update for the user to see. Kind of like strawpoll websites. When the user submits a vote, the results page will automatically update to the new vote count. I've been searching for an answer to this, and I feel like I've gotten halfway. I can get the actual chart to update, but it just duplicates the data and keeps on going forever. I want it to "replace" or just update the number and/or see for new voting questions as well.

Pic of the chart duplicating every second The chart duplicating.

Here's the code I'm running:

<div id="chart-container">
    <canvas id="dataChart"></canvas>
</div>
<script>
    var ctx = $("#dataChart");
    var dataChart = new Chart(ctx, {
        type: 'bar',
        data: {
            labels: [],
            datasets: [{
                label: '<?php echo($row['vote_name']) ?>',
                backgroundColor: '#49e2ff',
                borderColor: '#46d5f1',
                hoverBackgroundColor: '#CCCCCC',
                hoverBorderColor: '#666666',
                data: [],
            }]
        },
        options: {}
    });

    var updateChart = function() {
        $('#dataChart').html('');
        $('#dataChart').html('<canvas id="dataChart"></canvas>');
        $.ajax({
            url: "data.php?form=<?php echo($vote_id) ?>",
            type: "GET",
            dataType: "json",
            success: function(data) {
                console.log(data);
                var name = [];
                var marks = [];
                for (var i in data) {
                    dataChart.data.labels.push(data[i].question);
                    dataChart.data.datasets[0].data.push(data[i].vote_count);
                }
                dataChart.update();
            },
            error: function(data) {
                console.log(data);
            }
        });
    }

    updateChart();
    setInterval(() => {
        updateChart();
    }, 1000);
</script>

Question is: Is there a reason that this isn't working? I can't seem to get it right, no matter what I try. Any help would be appreciated!

2
  • 1
    Why don't you just replace the whole data array passed to Chart? Commented Aug 28, 2020 at 2:51
  • does data has right values which you need to put in chart ? Commented Aug 28, 2020 at 4:00

1 Answer 1

1

Following the suggestion, Tushar made in his comment, the success function could be changed as follows:

success: function(data) {
  dataChart.data.labels = data.map(v => v.question);
  dataChart.data.datasets[0].data = data.map(v => v.vote_count);
  dataChart.update();
},

This solution uses the Array.map() method that creates a new array populated with the results of calling the provided function on every element in the array.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.