JQuery Flot: clear graph
Asked Answered
M

2

8

Is there a recommended way to clear a jQuery Flot graph? I couldn't find anything the in the API reference.

Marrano answered 7/10, 2009 at 17:55 Comment(0)
C
15

By "clear" do you mean "nuke the entire graph," or just clear the data?

To nuke the entire graph: $('#canvas_id').empty();

To clear the data, just call the usual $.plot(placeholder, data, options); where data is empty.

Corpora answered 7/10, 2009 at 20:33 Comment(6)
is there any significance with respect to nuking...?Marrano
What is the right way to make sure all event handlers are cleaned up?Seesaw
@Seesaw any sane browser (so, exclude old versions of IE) should take care of this for you. Are you seeing evidence to the contrary?Corpora
This creates generates an error when attempting with the pie plugin.Raybin
@JoshBuchea for what it's worth, this answer is well over eight years old, which is pretty much an eternity in Javascript years.Corpora
@MattBall an eternity indeed. Believe it or not I'm working at a startup that uses Flot charts in production today, so the information is still relevant to some!Raybin
M
1

in my case just clearing container won't help, some flot bindings stil working.

var plot = $.plot(placeholder, data, options);
// some other code
//to completely remove flot chart call
plot.destroy(); // remove graph and all bindings

to clear only graph use Matt Ball ansver.

Merla answered 19/11, 2014 at 11:2 Comment(1)
Thanks for pointing me to destroy! Also helpful to note that destroy will internally call shutdown and remove any registered event handlers.Raybin

© 2022 - 2024 — McMap. All rights reserved.