How to remove previous piechart and add new piechart in js

586 views Asked by At

I developed pie charts as follows chartjs V2.6.0. It works fine whenever I search with new data in same pie chart it shows previous data when on hover. I tried with $('#canvas').remove(); method, but it's not working.

var color = [];
var label = [];
var datas = [];
var odata = 1;

for (i = 0; i < result.length; i++) {
  label.push(result[i].label);
  color.push(result[i].color);
  datas.push(result[i].value1);
}

var config = {
  type: 'pie',
  data: {
    labels: label,
    datasets: [{
      backgroundColor: color,
      data: datas
    }]
  },
  options: {
    responsive: true,
    legend: {
      display: false
    },
    tooltips: {
      enabled: true,
    },
    onClick: graphClickEvent
  }
};

var ctx = document.getElementById("state-area").getContext("2d");
new Chart(ctx, config);

function graphClickEvent(event, array) {
  console.log(event);
  console.log(array);
}
1

There are 1 answers

0
vinodh kumar On

I Used just removed previous canvas and added new canvas. it works fine.

$('#state-area').remove();
 $('#graph-container').append('<canvas id="state-area" height="300" height="300" style="z-index:999999;"><canvas>');