Chartjs-Chart Js , loading data on selection but bar graph displaying old values as well on hovering

0👍

The problem is that the old chart is still present in the canvas. Instead of creating a new chart each time new data is available, you should simply assign the new values to labels and the dataset and then update the existing chart.

To do so, you’ll define a global variable chart and change your code as follows:

var chart;
function load_stats(id) {
  $.ajax({
      ...

      if (chart) {
        // Updating Chart
        chart.data.labels = date;
        chart.data.datasets[0].data = throughputs;  
        chart.update();
      } else {
        // Creating Chart
        chart = new Chart('VF_Requests', {
          type: 'bar',
          data: {
            labels: date,
            datasets: [{
              backgroundColor: 'rgb(174,199,232)',
              data: throughputs
            }]
          }
        });
      }
    },
  });
}

Leave a comment