Destroying jQuery Flot Graph
Asked Answered
S

4

8

What is the correct way to destroy a jQuery flot graph in a way that will clean up all event handlers and not result in a memory leak?

It seems flot is leaving behind some zombies (aka Detached Dom Trees)

Spinoza answered 7/4, 2013 at 1:44 Comment(1)
$.plot('#yourFlotDiv', {}, {}); read it somewhere. Don't exactly remember where.Kenley
A
7

If you read the API docs, there is a shutdown method that cleans up for you

shutdown()

Cleans up any event handlers Flot has currently registered. This
is used internally.

eg.

var plot = $.plot($("#yourDiv"), options)

plot.shutdown()
Arie answered 11/4, 2013 at 13:45 Comment(0)
S
4

For people who come here in the future, there is now a destroy function that calls shutdown and removes the canvas element. It's undocumented in the API for some reason, but is available in the code:

var flot = $("#FlotDiv").data('plot')
if (flot) // If it's destroyed, then data('plot') will be undefined
    flot.destroy();
Sensuous answered 28/12, 2014 at 19:43 Comment(0)
P
3

if you want to remove event handlers then try jquery off method.

for to clear flot graph. you can empty the div.

$('#yourFlotDiv').empty();
Podium answered 7/4, 2013 at 1:58 Comment(3)
What arguments should be passed to jQuery.off()?Spinoza
@Spinoza $("#yourFlotDiv").off("yourEventHandlerTORemove"); for more info see this api.jquery.com/offPodium
I am talking about making sure flot cleans up all of its event handlers and deletes all of the DOM elements that it created.Spinoza
W
0

To remove the flot Graph

var placeholder = $("#FlotDiv");
placeholder.unbind(); //Remove a previously-attached event handler from the elements.
placeholder.empty();

Also in case you wish to unbind specific event this is the way to go :

$( "#foo").unbind( "click" );

For more info check this out.

Waterloo answered 23/5, 2016 at 12:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.