Permalink
Browse files

Refactor replot behaviour so Flot tries to reuse the existing canvas,

adding shutdown() methods to the plot (based on patch by Ryley
Breiddal, issue 269). This prevents a memory leak in Chrome and
hopefully makes replotting faster for those who are using $.plot
instead of .setData()/.draw(). Also update jQuery to 1.5.1 to prevent
IE leaks fixed some time ago in jQuery.


git-svn-id: https://flot.googlecode.com/svn/trunk@317 1e0a6537-2640-0410-bfb7-f154510ff394
  • Loading branch information...
olau@iola.dk
olau@iola.dk committed Mar 17, 2011
1 parent e47b431 commit 4e02781692e4bc58dc2d8585db1570e7cae748d1
Showing with 7,542 additions and 3,424 deletions.
  1. +26 −2 API.txt
  2. +8 −2 NEWS.txt
  3. +50 −18 PLUGINS.txt
  4. +31 −23 jquery.flot.crosshair.js
  5. +104 −32 jquery.flot.js
  6. +72 −52 jquery.flot.navigate.js
  7. +24 −8 jquery.flot.selection.js
  8. +7,227 −3,287 jquery.js
View
28 API.txt
@@ -901,7 +901,19 @@ can call:
o = pointOffset({ x: xpos, y: ypos, xaxis: 2, yaxis: 3 })
// o.left and o.top now contains the offset within the div
-
+
+ - resize()
+
+ Tells Flot to resize the drawing canvas to the size of the
+ placeholder. You need to run setupGrid() and draw() afterwards as
+ canvas resizing is a destructive operation. This is used
+ internally by the resize plugin.
+
+ - shutdown()
+
+ Cleans up any event handlers Flot has currently registered. This
+ is used internally.
+
There are also some members that let you peek inside the internal
workings of Flot which is useful in some cases. Note that if you change
@@ -998,6 +1010,8 @@ Here's an overview of the phases Flot goes through:
7. Responding to events, if any
+ 8. Shutdown: this mostly happens in case a plot is overwritten
+
Each hook is simply a function which is put in the appropriate array.
You can add them through the "hooks" option, and they are also available
after the plot is constructed as the "hooks" attribute on the returned
@@ -1146,6 +1160,16 @@ hooks in the plugins bundled with Flot.
crosshair plugin for an example.
+ - shutdown [phase 8]
+
+ function (plot, eventHolder)
+
+ Run when plot.shutdown() is called, which usually only happens in
+ case a plot is overwritten by a new plot. If you're writing a
+ plugin that adds extra DOM elements or event handlers, you should
+ add a callback to clean up after you. Take a look at the section in
+ PLUGINS.txt for more info.
+
Plugins
-------
@@ -1163,7 +1187,7 @@ Here's a brief explanation of how the plugin plumbings work:
Each plugin registers itself in the global array $.plot.plugins. When
you make a new plot object with $.plot, Flot goes through this array
calling the "init" function of each plugin and merging default options
-from the plugin's "option" attribute. The init function gets a
+from the "option" attribute of the plugin. The init function gets a
reference to the plot object created and uses this to register hooks
and add new public methods if needed.
View
@@ -94,8 +94,14 @@ Changes:
- The version comment is now included in the minified jquery.flot.min.js.
- New options.grid.minBorderMargin for adjusting the minimum margin
provided around the border (based on patch by corani, issue 188).
-
-- New hooks: drawSeries
+- Refactor replot behaviour so Flot tries to reuse the existing
+ canvas, adding shutdown() methods to the plot (based on patch by
+ Ryley Breiddal, issue 269). This prevents a memory leak in Chrome
+ and hopefully makes replotting faster for those who are using $.plot
+ instead of .setData()/.draw(). Also update jQuery to 1.5.1 to
+ prevent IE leaks fixed in jQuery.
+
+- New hooks: drawSeries, shutdown
Bug fixes:
View
@@ -1,20 +1,22 @@
Writing plugins
---------------
-To make a new plugin, create an init function and a set of options (if
-needed), stuff it into an object and put it in the $.plot.plugins
-array. For example:
+All you need to do to make a new plugin is creating an init function
+and a set of options (if needed), stuffing it into an object and
+putting it in the $.plot.plugins array. For example:
- function myCoolPluginInit(plot) { plot.coolstring = "Hello!" };
- var myCoolOptions = { coolstuff: { show: true } }
- $.plot.plugins.push({ init: myCoolPluginInit, options: myCoolOptions });
+ function myCoolPluginInit(plot) {
+ plot.coolstring = "Hello!";
+ };
- // now when $.plot is called, the returned object will have the
+ $.plot.plugins.push({ init: myCoolPluginInit, options: { ... } });
+
+ // if $.plot is called, it will return a plot object with the
// attribute "coolstring"
Now, given that the plugin might run in many different places, it's
-a good idea to avoid leaking names. We can avoid this by wrapping the
-above lines in an anonymous function which we call immediately, like
+a good idea to avoid leaking names. The usual trick here is wrap the
+above lines in an anonymous function which is called immediately, like
this: (function () { inner code ... })(). To make it even more robust
in case $ is not bound to jQuery but some other Javascript library, we
can write it as
@@ -24,6 +26,13 @@ can write it as
// ...
})(jQuery);
+There's a complete example below, but you should also check out the
+plugins bundled with Flot.
+
+
+Complete example
+----------------
+
Here is a simple debug plugin which alerts each of the series in the
plot. It has a single option that control whether it is enabled and
how much info to output:
@@ -75,16 +84,41 @@ This simple plugin illustrates a couple of points:
- Variables in the init function can be used to store plot-specific
state between the hooks.
+The two last points are important because there may be multiple plots
+on the same page, and you'd want to make sure they are not mixed up.
+
+
+Shutting down a plugin
+----------------------
+
+Each plot object has a shutdown hook which is run when plot.shutdown()
+is called. This usually mostly happens in case another plot is made on
+top of an existing one.
+
+The purpose of the hook is to give you a chance to unbind any event
+handlers you've registered and remove any extra DOM things you've
+inserted.
+
+The problem with event handlers is that you can have registered a
+handler which is run in some point in the future, e.g. with
+setTimeout(). Meanwhile, the plot may have been shutdown and removed,
+but because your event handler is still referencing it, it can't be
+garbage collected yet, and worse, if your handler eventually runs, it
+may overwrite stuff on a completely different plot.
+
-Options guidelines
-==================
+Some hints on the options
+-------------------------
Plugins should always support appropriate options to enable/disable
them because the plugin user may have several plots on the same page
-where only one should use the plugin.
+where only one should use the plugin. In most cases it's probably a
+good idea if the plugin is turned off rather than on per default, just
+like most of the powerful features in Flot.
-If the plugin needs series-specific options, you can put them in
-"series" in the options object, e.g.
+If the plugin needs options that are specific to each series, like the
+points or lines options in core Flot, you can put them in "series" in
+the options object, e.g.
var options = {
series: {
@@ -95,10 +129,8 @@ If the plugin needs series-specific options, you can put them in
}
}
-Then they will be copied by Flot into each series, providing the
-defaults in case the plugin user doesn't specify any. Again, in most
-cases it's probably a good idea if the plugin is turned off rather
-than on per default, just like most of the powerful features in Flot.
+Then they will be copied by Flot into each series, providing default
+values in case none are specified.
Think hard and long about naming the options. These names are going to
be public API, and code is going to depend on them if the plugin is
View
@@ -90,34 +90,37 @@ The plugin also adds four public methods:
crosshair.locked = false;
}
- plot.hooks.bindEvents.push(function (plot, eventHolder) {
- if (!plot.getOptions().crosshair.mode)
+ function onMouseOut(e) {
+ if (crosshair.locked)
return;
- eventHolder.mouseout(function () {
- if (crosshair.locked)
- return;
+ if (crosshair.x != -1) {
+ crosshair.x = -1;
+ plot.triggerRedrawOverlay();
+ }
+ }
- if (crosshair.x != -1) {
- crosshair.x = -1;
- plot.triggerRedrawOverlay();
- }
- });
-
- eventHolder.mousemove(function (e) {
- if (crosshair.locked)
- return;
+ function onMouseMove(e) {
+ if (crosshair.locked)
+ return;
- if (plot.getSelection && plot.getSelection()) {
- crosshair.x = -1; // hide the crosshair while selecting
- return;
- }
+ if (plot.getSelection && plot.getSelection()) {
+ crosshair.x = -1; // hide the crosshair while selecting
+ return;
+ }
- var offset = plot.offset();
- crosshair.x = Math.max(0, Math.min(e.pageX - offset.left, plot.width()));
- crosshair.y = Math.max(0, Math.min(e.pageY - offset.top, plot.height()));
- plot.triggerRedrawOverlay();
- });
+ var offset = plot.offset();
+ crosshair.x = Math.max(0, Math.min(e.pageX - offset.left, plot.width()));
+ crosshair.y = Math.max(0, Math.min(e.pageY - offset.top, plot.height()));
+ plot.triggerRedrawOverlay();
+ }
+
+ plot.hooks.bindEvents.push(function (plot, eventHolder) {
+ if (!plot.getOptions().crosshair.mode)
+ return;
+
+ eventHolder.mouseout(onMouseOut);
+ eventHolder.mousemove(onMouseMove);
});
plot.hooks.drawOverlay.push(function (plot, ctx) {
@@ -148,6 +151,11 @@ The plugin also adds four public methods:
}
ctx.restore();
});
+
+ plot.hooks.shutdown.push(function (plot, eventHolder) {
+ eventHolder.unbind("mouseout", onMouseOut);
+ eventHolder.unbind("mousemove", onMouseMove);
+ });
}
$.plot.plugins.push({
Oops, something went wrong.

0 comments on commit 4e02781

Please sign in to comment.