2

I am working on the following chart. My plan is to use it to display live data every 1 second with a buffer of 600 points. I want it to display initial data (the entire 600 points) fetches data (simulated through setInterval) then puts it and shift. The problem is that it only displays 12 points.

Question 1: Can you help me what am I missing or is it impossible with ChartJS? Question 2: Is this buffer reasonable with ChartJS?

<!DOCTYPE html>
<html>
    <head>
        <title>Hello World!!</title>
    </head>
    <script src="/socket.io/socket.io.js"></script>
    <script src="Chart.min.js"></script>
    <script>
        var socket = io();

        socket.on('pv_changed', function(data) { document.getElementById('pv').innerHTML = data.value });
    </script>
    <style>
    </style>
    <body>
        Hello World!!!<br/>
        <label>PV Value: </lable>
        <label id="pv">hi</lablel>
        <div class="container">
          <div>
            <canvas id="myChart" height=130px></canvas>
            <script>
                var ctx = document.getElementById('myChart').getContext('2d');
                var myChart = new Chart(ctx, {
                    type: 'line',
                    data: {
                        labels: ['M', 'T', 'W', 'T', 'F', 'S', 'S', 'W', 'T', 'F', 'S', 'S'],
                        datasets: [{
                            label: 'Test',
                            data: Array.apply(null, Array(600)).map(Number.prototype.valueOf,0),
                            backgroundColor: "rgba(255,153,0,0.4)",
                            animation: true
                        }]
                    },
                    options: {
                        responsive: true,
                        scales: {
                            yAxes: [{
                                display: true,
                                scaleLabel: {
                                    display: true,
                                    labelString: 'Month'
                                },
                                ticks: {
                                    beginAtZero: true,
                                    min: -5,
                                    max: 25,
                                    stepsize: 5
                                }
                            }],
                            xAxes: [{
                                display: true,
                                scaleLabel: {
                                    display: true,
                                    labelString: 'Hello'
                                },
                                ticks: {
                                    beginAtZero: true,
                                    min: 0,
                                    max: 600,
                                    stepsize: 50
                                }
                            }]
                        }
                    }
                });

                var top = 20;
                var now = 0;
                setInterval(function() {
                    myChart.data.datasets[0].data.shift();
                    myChart.data.datasets[0].data.push(now);
                    myChart.update();
                    now = now + 1;
                    if(now == 20)
                        now = 0;
                }, 1000);
            </script>
          </div>
        </div>
    </body>
</html>

1 Answer 1

0

It seems that you can't update datasets with myChart.update(). try to create temporary object and then assign that object to myChart.data.datasets. you then need to redraw your chart again. I had the same issue with using chart.js in my angular project link. Did you check link?

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

2 Comments

Even without updating it and assign just an array of 600 points as shown will only show 12 points.
Also the chart is updated through .update() but only shows 12 points.

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.