when using jQuery to call webapi to obtain bubble chart data and draw bubble chart with chart.js in the test web page, if you click the display chart button twice, an error of “uncaught error: canvas is already in use. Chart with ID ‘0’ must be destroyed before the canvas can be reused.” will be reported.
from the error prompt, it should be that the canvas used to draw the bubble chart has been occupied, and there will be a conflict when using it to draw the graph again. Find two ways to solve this problem from chart.js official website and online
Call the chart.Destroy() function
The destroy function is introduced in the API help document of chart.js official website. This function is used to destroy the chart instance, clear the saved references in the object and the associated event listener. This function must be used before re using canvas to draw new graphics.
this article adds detection code before creating a new chart instance. If the mybubblechart object is already an instance of chart type, call the destroy function to destroy the instance
if(myBubbleChart instanceof Chart)
{
myBubbleChart.destroy();
}
myBubbleChart = new Chart(bubbleCanvas, {
type: "bubble",
data: data1,
options: []
});
Clear/add canvas with jquery
Another way of learning as like as two peas in 2 is to remove the canvas element from jQuery before you create the chart instance, and then add the same canvas element, which is shown below:
$('#bubbleChart').remove();
$('#chartDiv').append('<canvas id="bubbleChart"></canvas>');
the above two methods support repeatedly clicking the display chart button to generate a bubble chart
Read More:
- [How to Solve] canvas.save(Canvas.ALL_SAVE_FLAG)
- Canvas generates poster todataurl with error [Solved]
- [Solved] AS Warning: String literal in setText can not be translated. Use Android resources instead.
- MOTR compiling error: cannot call member function ‘void std::basic_string<_CharT, _Traits, _Alloc>::_R
- The showdialog() method in thread/threading. Timer/task reported an error: “before ole can be called, the current thread must be set to single thread unit (STA) mode.”
- [Solved] swiper Error: The requested module ‘react’ is expected to be of type CommonJS, which does not support named exports. CommonJS modules can be imported by importing the default export
- jmeter Execute to create Chart Error: An error occurred: Error while processing samples: Mismatch between ……
- It can be opened with localhost, but not with IP address. The request host name is invalid
- [Solved] Markdown Edit Formula Error: ParseError: KaTeX parse error: {align} can be used only in display mode
- An error was reported when Maven package was running the packaged jar package: there is no main list attribute in xxx.jar, which can be solved by configuring Maven plugin
- Hash_map is deprecated and will be REMOVED. Please use unordered_map.
- This limit can be set by changing the [search.max_open_scroll_context] setting
- Driver error 11: System 0.500000: CAN 1 : Message with ID = 630 could not be sent. Driver error 11 in TransmitCANFrame
- How to Solve VMware Workstation Error: This virtual machine appears to be in use.
- Rendering Problems The following classes could not be instantiated: xml layout file could not be loaded
- [Solved] Junit.test use error: log4j:WARN No appenders could be found for logger
- The spring project is normal locally, and the bean cannot be found error is thrown when entering the docker container
- Template cannot be rendered due to the joint query of populate in mongoose: syntax error: unexpected token r in JSON at position 0
- [Solved] RuntimeError: each element in list of batch should be of equal size