summaryrefslogtreecommitdiff
path: root/PLUGINS.txt
diff options
context:
space:
mode:
Diffstat (limited to 'PLUGINS.txt')
-rw-r--r--PLUGINS.txt68
1 files changed, 50 insertions, 18 deletions
diff --git a/PLUGINS.txt b/PLUGINS.txt
index 00bf2e5..af3d90b 100644
--- a/PLUGINS.txt
+++ b/PLUGINS.txt
@@ -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