0
votes

I'm attempting to call Chart.js's addData method from a signalR callback in order to dynamically add data to a chart based on server inputs. However, when the callback is triggered, the addData method on Chart.js is throwing an exception:

Uncaught TypeError: window.myChart.addData is not a function

Javascript:

var ctx = $("#myChart");
    window.myChart = new Chart(ctx, {
        type: 'line',
        data: {
            labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
            datasets: [{
                label: '# of Votes',
                data: [12, 19, 3, 5, 2, 3],
                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
                    }
                }]
            }
        },
        responsive: true
    });


    $(function () {
        var chart = $.connection.chartHub;

        chart.client.addPointToChart = function () {
            window.myChart.addData([20], "Magenta");
        };
        $.connection.hub.start().done(function () {
            chart.server.start();
        });
    });

C# (hub code)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.AspNet.SignalR;

namespace dvvWeb.Hubs
{
public class ChartHub : Hub
{
    public void Start()
    {
        while (true)
        {
            Clients.All.addPointToChart();
            System.Threading.Thread.Sleep(10000);
        }
    }
}
}
1

1 Answers

3
votes

According to the documentation you should change your dataset directly and call update:

.update(duration, lazy) function to update your datas

// duration is the time for the animation of the redraw in miliseconds
// lazy is a boolean. if true, the animation can be interupted by other animations
myLineChart.data.datasets[0].data[2] = 50; // Would update the first dataset's value of 'March' to be 50
myLineChart.update(); // Calling update now animates the position of March from 90 to 50.

I did a little test function on this fiddle:

setInterval(function(e) {
console.log(myDoughnutChart.data.datasets[0]);
  myDoughnutChart.data.datasets[0].data.push(15);
  myDoughnutChart.update();
}, 1000);

https://jsfiddle.net/Tintin37/weLoqyby/

Opened issue : https://github.com/chartjs/Chart.js/issues/1997

EDIT

For real time chart (I'm using signalr too, I use visjs)

http://visjs.org/examples/graph2d/15_streaming_data.html

Have a great day !