How to get a plot object from an initialized placeholder/container

970 views
Skip to first unread message

Robert McLeod

unread,
Oct 13, 2014, 12:56:25 AM10/13/14
to flot-...@googlegroups.com
I have a long running webapp.

I want to be able to drop the plot variable after I initialize the flot on a placeholder to allow the java-script garbage collector to do it's thing.

Then I want to be able to pick it up again later so I can update the value, by giving the placeholder id as an argument.

Is there anyway to do this?  Calling $("#placeholder").plot() again seems to just try to initialize the chart again, emptying it of anything that was in it.

Thanks,
Robert

Ced

unread,
Oct 13, 2014, 11:46:16 AM10/13/14
to flot-...@googlegroups.com
There's really very little benefit in doing this.  At best, you'd just be garbage collecting a reference to the plot, which is essentially a pointer or 4 bytes.  Then each time you replot you'll incur the cost of traversing the DOM to locate the #placeholder div.  It's a tradeoff between performance and memory.  In this case I would personally just keep the reference since it's a tiny amount of memory.

To answer your question, the plot object is attached to the #placeholder element in the "data" field with the key "plot."
// Excerpt from jquery.flot.js:
placeholder.data("plot", plot);

Refer to the jQuery documentation here: http://api.jquery.com/jquery.data/

You can retrieve the plot object with the following:
plot = $.data($("#placeholder"), "plot");

Robert McLeod

unread,
Oct 13, 2014, 5:01:01 PM10/13/14
to flot-...@googlegroups.com
Awesome, thank you.
Reply all
Reply to author
Forward
0 new messages