Chartjs-Chart JS not displaying graph

1👍

It’s not as easy as that. To update the chart you have to access the chart variable and then update its fields. You then call the update() function of the chart. Like this:

$.ajax({
  url: dataUrl,
  type: 'GET',
  success: function (response) {
    popChart.data.datasets[0].data = response.users;
    popChart.update();
  }
});

You can also create functions that do this to make the code more readable. Like in the official documentation.

You have one more problem with your code. In the response you are getting users and docs as objects with several fields representing months. Chart.js wants the data as an array:

"users": [3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

rather than

"users": { "1": 3, "2": 0, "3": 0, "4": 0, "5": 0, "6": 0, "7": 0, "8": 0, "9": 0, "10": 0, "11": 0, "12": 0 }

If you can change the server-side code you should do it there. If not you can convert the users/docs response to arrays at the client-side. See this Q&A.

Leave a comment