PLUGINS.txt 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. Writing plugins
  2. ---------------
  3. All you need to do to make a new plugin is creating an init function
  4. and a set of options (if needed), stuffing it into an object and
  5. putting it in the $.plot.plugins array. For example:
  6. function myCoolPluginInit(plot) {
  7. plot.coolstring = "Hello!";
  8. };
  9. $.plot.plugins.push({ init: myCoolPluginInit, options: { ... } });
  10. // if $.plot is called, it will return a plot object with the
  11. // attribute "coolstring"
  12. Now, given that the plugin might run in many different places, it's
  13. a good idea to avoid leaking names. The usual trick here is wrap the
  14. above lines in an anonymous function which is called immediately, like
  15. this: (function () { inner code ... })(). To make it even more robust
  16. in case $ is not bound to jQuery but some other Javascript library, we
  17. can write it as
  18. (function ($) {
  19. // plugin definition
  20. // ...
  21. })(jQuery);
  22. There's a complete example below, but you should also check out the
  23. plugins bundled with Flot.
  24. Complete example
  25. ----------------
  26. Here is a simple debug plugin which alerts each of the series in the
  27. plot. It has a single option that control whether it is enabled and
  28. how much info to output:
  29. (function ($) {
  30. function init(plot) {
  31. var debugLevel = 1;
  32. function checkDebugEnabled(plot, options) {
  33. if (options.debug) {
  34. debugLevel = options.debug;
  35. plot.hooks.processDatapoints.push(alertSeries);
  36. }
  37. }
  38. function alertSeries(plot, series, datapoints) {
  39. var msg = "series " + series.label;
  40. if (debugLevel > 1)
  41. msg += " with " + series.data.length + " points";
  42. alert(msg);
  43. }
  44. plot.hooks.processOptions.push(checkDebugEnabled);
  45. }
  46. var options = { debug: 0 };
  47. $.plot.plugins.push({
  48. init: init,
  49. options: options,
  50. name: "simpledebug",
  51. version: "0.1"
  52. });
  53. })(jQuery);
  54. We also define "name" and "version". It's not used by Flot, but might
  55. be helpful for other plugins in resolving dependencies.
  56. Put the above in a file named "jquery.flot.debug.js", include it in an
  57. HTML page and then it can be used with:
  58. $.plot($("#placeholder"), [...], { debug: 2 });
  59. This simple plugin illustrates a couple of points:
  60. - It uses the anonymous function trick to avoid name pollution.
  61. - It can be enabled/disabled through an option.
  62. - Variables in the init function can be used to store plot-specific
  63. state between the hooks.
  64. The two last points are important because there may be multiple plots
  65. on the same page, and you'd want to make sure they are not mixed up.
  66. Shutting down a plugin
  67. ----------------------
  68. Each plot object has a shutdown hook which is run when plot.shutdown()
  69. is called. This usually mostly happens in case another plot is made on
  70. top of an existing one.
  71. The purpose of the hook is to give you a chance to unbind any event
  72. handlers you've registered and remove any extra DOM things you've
  73. inserted.
  74. The problem with event handlers is that you can have registered a
  75. handler which is run in some point in the future, e.g. with
  76. setTimeout(). Meanwhile, the plot may have been shutdown and removed,
  77. but because your event handler is still referencing it, it can't be
  78. garbage collected yet, and worse, if your handler eventually runs, it
  79. may overwrite stuff on a completely different plot.
  80. Some hints on the options
  81. -------------------------
  82. Plugins should always support appropriate options to enable/disable
  83. them because the plugin user may have several plots on the same page
  84. where only one should use the plugin. In most cases it's probably a
  85. good idea if the plugin is turned off rather than on per default, just
  86. like most of the powerful features in Flot.
  87. If the plugin needs options that are specific to each series, like the
  88. points or lines options in core Flot, you can put them in "series" in
  89. the options object, e.g.
  90. var options = {
  91. series: {
  92. downsample: {
  93. algorithm: null,
  94. maxpoints: 1000
  95. }
  96. }
  97. }
  98. Then they will be copied by Flot into each series, providing default
  99. values in case none are specified.
  100. Think hard and long about naming the options. These names are going to
  101. be public API, and code is going to depend on them if the plugin is
  102. successful.