[Chartjs]-Chartjs & asp.net: Cannot read property 'labels' of undefined

1👍

var chart = document.getElementById(chartID); will return the canvas element, which is not an instance of Chart.js, so all of the subsequent code is invalid.

You need to use myChart from the LoadChart() function as this is a Chart.js instance (returned at instantiation). The simplest way to do this is to move the variable definition to a higher scope so that it is available to your update function.

Without knowing how your code is structured it’s difficult to give a working example, but here’s a simplistic example of what I mean:

var myChart;

$(function () {
    LoadChart();
});

function LoadChart() {
    var ctx = document.getElementById('myChart');
    myChart = new Chart(ctx, {
        type: 'line',
    ...
}

$.connection.redisHub.client.addData = function (chartID, labels, data) {
    myChart.data.labels.push(labels);
    ...
}

Leave a comment