Search code examples
javascriptchartshtml5-canvaschart.js

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.


Solution

  • 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.