Home Forums Chart Support Not able to get chart to update from JSON

Not able to get chart to update from JSON

Viewing 3 posts - 1 through 3 (of 3 total)
  • #42345

    I’m sure I must be fumbling something easy here, but not sure what I’ve done wrong.

    I have a JSON file that updates it’s data every 5 minutes, and the chart is reading and displaying that data fine, at least on the initial load, however I can’t get the chart to update the data as new data comes into the JSON.

    I stripped down a basic version of what I’m doing here in this fiddle: https://jsfiddle.net/elitriona/pagkd024/51/

    Currently the setInterval is trying to update the chart every 1 second, although it does not need too since the JSON data itself only gets updated every 5 minutes. But no matter how long I wait… 5 minutes…10 minutes…the chart does not update when new JSON becomes available. I can confirm by looking at the JSON data directly that new data is available, but I can’t get the chart to show it.

    Any insight as to what I’ve done incorrectly?

    Thank you!

    #42354

    @elitriona,

    It seems like you are not rendering the chart after updating the datapoints on ajax request. Calling chart.render() after updating the datapoints will work fine in your case. Please take a look the code snippet below for the same.

    $.getJSON('https://trionacgm.fly.dev/api/v1/entries.json?count=12', function(data) {
      chart.options.data[0].dataPoints = [];
      $.each(data, function(key, value) {
        chart.options.data[0].dataPoints.push({
          x: (value['date']),
          y: Number(value['sgv'])
        });
      });
      chart.render();
    });

    Also, check out this JSFiddle for complete working code.

    Updating CanvasJS Chart from JSON API

    —-
    Manoj Mohan
    Team CanvasJS

    #42355

    Outstanding! Thank you, this solved it perfectly :)

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.