user2867085
user2867085

Reputation: 41

Refresh chart.config.data

I have an ajax call that returns some data. The one call returns data for 3 charts. The 1st 2 is easy as i only push a data value into the current data. The 3rd ones data is returned as

{
  labels: ["0RL_201902_19", "0RL_201906_20", "0RL_201909_21", "2NL_201805_36", "2NL_201805_37", "3NL_201805_37", "3NL_201909_51", "3NL_201909_53", "4NL_201909_53", "4NL_201909_52", "4NL_201805_37", "10NL_201907_49"],
  datasets: [{
    label: "%Distribution",
    backgroundColor: "blue",
    borderColor: "rgba(78, 115, 223, 1)",
    data: [52, 22, 26, 22, 78, 21, 52, 27, 35, 48, 17, 100]
  }]
}

I need to replace the current chart.config.data with this result above. This is not working. If i take the result above and add it manual it works, but when assigned in the call is does not work. Any help will be appreciated.

I have tried recreating the chart.

chart = new Chart(ctb, {
  type: 'horizontalBar',
  data: xx,
  options: {}
});

I have tried setting the config

chart.config.data = xx;
chart.update();
$.ajax({
  url: "getstats",
  type: 'GET',
  dataType: 'json',
  success: function(datas) {
    // This ajax call returns data for multiple charts. 

  }
})

Upvotes: 2

Views: 196

Answers (1)

user2867085
user2867085

Reputation: 41

Fixed it.

var varbarlables = datas.map(a => a.labels);
var varbardataset= datas.map(a => a.dataval);
var labels = JSON.parse("[" + varbarlables + "]");

var mdata = JSON.parse("[" + varbardataset + "]");

myBarChart.config.data.labels = labels;
myBarChart.config.data.datasets[0].data = mdata;
myBarChart.update();

Upvotes: 1

Related Questions