Chartjs-Chart.js Y-Axis data not rendering

1👍

The processing of the JSON data in your code looks strange to me and it can be improved.

Since fetch() makes an asynchronous request, also make sure to update the chart only once the data is processed and assigned to dataset.data. Therefore, move myLineChart.update() inside the then() function.

The amended code could look as follows:

fetch('/api/data')
  .then(function(response) {
    return response.json();
  })
  .then(function(json) {
    myLineChart.data.datasets[0].data = json.map(o => ({
      x: parseFloat(o['x']),
      y: parseFloat(o['y'])
    }));
    myLineChart.update();
  });

This may not solve your problem but it is a first step to eliminate possible sources of error.

Leave a comment