简体   繁体   中英

Destroying chart.js is not working when chart created inside function - chart.destroy() is not a function

I am using a function to create a chart which uses chart.js. Unfortunately, whenever I am trying to call chart.destroy() I get the error:

"chart.destroy() is not a function". My code looks as following:

  var chart = {};
  createChart(x_axis_data, y_axis_data, chart); //Function creating chart

The chart itself is created inside the function with following:

chart = new Chart(document.getElementById(id),{....

The chart is created successfully. However, I cannot delte the chart by calling: chart.destroy. I have already tried to use

window.chart.destroy()

without any success.

Set window.chart = rather than relying on the implicit function of chart = . If you were using strict mode with your code, it should throw a warning.

Take a look at this: https://github.com/chartjs/Chart.js/issues/1007

Change:

var myNewChart1 = new Chart(ctx1).Line(barChartData1, {animation: false});

To:

window.myNewChart1 = new Chart(ctx1).Line(barChartData1, {animation: false});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM