PHP/JavaScript webapp to analyse spending habits
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

jquery.flot.selection.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. Flot plugin for selecting regions.
  3. The plugin defines the following options:
  4. selection: {
  5. mode: null or "x" or "y" or "xy",
  6. color: color
  7. }
  8. You enable selection support by setting the mode to one of "x", "y" or
  9. "xy". In "x" mode, the user will only be able to specify the x range,
  10. similarly for "y" mode. For "xy", the selection becomes a rectangle
  11. where both ranges can be specified. "color" is color of the selection.
  12. When selection support is enabled, a "plotselected" event will be emitted
  13. on the DOM element you passed into the plot function. The event
  14. handler gets one extra parameter with the ranges selected on the axes,
  15. like this:
  16. placeholder.bind("plotselected", function(event, ranges) {
  17. alert("You selected " + ranges.xaxis.from + " to " + ranges.xaxis.to)
  18. // similar for yaxis, secondary axes are in x2axis
  19. // and y2axis if present
  20. });
  21. The "plotselected" event is only fired when the user has finished
  22. making the selection. A "plotselecting" event is fired during the
  23. process with the same parameters as the "plotselected" event, in case
  24. you want to know what's happening while it's happening,
  25. A "plotunselected" event with no arguments is emitted when the user
  26. clicks the mouse to remove the selection.
  27. The plugin allso adds the following methods to the plot object:
  28. - setSelection(ranges, preventEvent)
  29. Set the selection rectangle. The passed in ranges is on the same
  30. form as returned in the "plotselected" event. If the selection
  31. mode is "x", you should put in either an xaxis (or x2axis) object,
  32. if the mode is "y" you need to put in an yaxis (or y2axis) object
  33. and both xaxis/x2axis and yaxis/y2axis if the selection mode is
  34. "xy", like this:
  35. setSelection({ xaxis: { from: 0, to: 10 }, yaxis: { from: 40, to: 60 } });
  36. setSelection will trigger the "plotselected" event when called. If
  37. you don't want that to happen, e.g. if you're inside a
  38. "plotselected" handler, pass true as the second parameter.
  39. - clearSelection(preventEvent)
  40. Clear the selection rectangle. Pass in true to avoid getting a
  41. "plotunselected" event.
  42. - getSelection()
  43. Returns the current selection in the same format as the
  44. "plotselected" event. If there's currently no selection, the
  45. function returns null.
  46. */
  47. (function ($) {
  48. function init(plot) {
  49. var selection = {
  50. first: { x: -1, y: -1}, second: { x: -1, y: -1},
  51. show: false,
  52. active: false
  53. };
  54. // FIXME: The drag handling implemented here should be
  55. // abstracted out, there's some similar code from a library in
  56. // the navigation plugin, this should be massaged a bit to fit
  57. // the Flot cases here better and reused. Doing this would
  58. // make this plugin much slimmer.
  59. var savedhandlers = {};
  60. function onMouseMove(e) {
  61. if (selection.active) {
  62. plot.getPlaceholder().trigger("plotselecting", [ getSelection() ]);
  63. updateSelection(e);
  64. }
  65. }
  66. function onMouseDown(e) {
  67. if (e.which != 1) // only accept left-click
  68. return;
  69. // cancel out any text selections
  70. document.body.focus();
  71. // prevent text selection and drag in old-school browsers
  72. if (document.onselectstart !== undefined && savedhandlers.onselectstart == null) {
  73. savedhandlers.onselectstart = document.onselectstart;
  74. document.onselectstart = function () { return false; };
  75. }
  76. if (document.ondrag !== undefined && savedhandlers.ondrag == null) {
  77. savedhandlers.ondrag = document.ondrag;
  78. document.ondrag = function () { return false; };
  79. }
  80. setSelectionPos(selection.first, e);
  81. selection.active = true;
  82. $(document).one("mouseup", onMouseUp);
  83. }
  84. function onMouseUp(e) {
  85. // revert drag stuff for old-school browsers
  86. if (document.onselectstart !== undefined)
  87. document.onselectstart = savedhandlers.onselectstart;
  88. if (document.ondrag !== undefined)
  89. document.ondrag = savedhandlers.ondrag;
  90. // no more draggy-dee-drag
  91. selection.active = false;
  92. updateSelection(e);
  93. if (selectionIsSane())
  94. triggerSelectedEvent();
  95. else {
  96. // this counts as a clear
  97. plot.getPlaceholder().trigger("plotunselected", [ ]);
  98. plot.getPlaceholder().trigger("plotselecting", [ null ]);
  99. }
  100. return false;
  101. }
  102. function getSelection() {
  103. if (!selectionIsSane())
  104. return null;
  105. var x1 = Math.min(selection.first.x, selection.second.x),
  106. x2 = Math.max(selection.first.x, selection.second.x),
  107. y1 = Math.max(selection.first.y, selection.second.y),
  108. y2 = Math.min(selection.first.y, selection.second.y);
  109. var r = {};
  110. var axes = plot.getAxes();
  111. if (axes.xaxis.used)
  112. r.xaxis = { from: axes.xaxis.c2p(x1), to: axes.xaxis.c2p(x2) };
  113. if (axes.x2axis.used)
  114. r.x2axis = { from: axes.x2axis.c2p(x1), to: axes.x2axis.c2p(x2) };
  115. if (axes.yaxis.used)
  116. r.yaxis = { from: axes.yaxis.c2p(y1), to: axes.yaxis.c2p(y2) };
  117. if (axes.y2axis.used)
  118. r.y2axis = { from: axes.y2axis.c2p(y1), to: axes.y2axis.c2p(y2) };
  119. return r;
  120. }
  121. function triggerSelectedEvent() {
  122. var r = getSelection();
  123. plot.getPlaceholder().trigger("plotselected", [ r ]);
  124. // backwards-compat stuff, to be removed in future
  125. var axes = plot.getAxes();
  126. if (axes.xaxis.used && axes.yaxis.used)
  127. plot.getPlaceholder().trigger("selected", [ { x1: r.xaxis.from, y1: r.yaxis.from, x2: r.xaxis.to, y2: r.yaxis.to } ]);
  128. }
  129. function clamp(min, value, max) {
  130. return value < min? min: (value > max? max: value);
  131. }
  132. function setSelectionPos(pos, e) {
  133. var o = plot.getOptions();
  134. var offset = plot.getPlaceholder().offset();
  135. var plotOffset = plot.getPlotOffset();
  136. pos.x = clamp(0, e.pageX - offset.left - plotOffset.left, plot.width());
  137. pos.y = clamp(0, e.pageY - offset.top - plotOffset.top, plot.height());
  138. if (o.selection.mode == "y")
  139. pos.x = pos == selection.first? 0: plot.width();
  140. if (o.selection.mode == "x")
  141. pos.y = pos == selection.first? 0: plot.height();
  142. }
  143. function updateSelection(pos) {
  144. if (pos.pageX == null)
  145. return;
  146. setSelectionPos(selection.second, pos);
  147. if (selectionIsSane()) {
  148. selection.show = true;
  149. plot.triggerRedrawOverlay();
  150. }
  151. else
  152. clearSelection(true);
  153. }
  154. function clearSelection(preventEvent) {
  155. if (selection.show) {
  156. selection.show = false;
  157. plot.triggerRedrawOverlay();
  158. if (!preventEvent)
  159. plot.getPlaceholder().trigger("plotunselected", [ ]);
  160. }
  161. }
  162. function setSelection(ranges, preventEvent) {
  163. var axis, range, axes = plot.getAxes();
  164. var o = plot.getOptions();
  165. if (o.selection.mode == "y") {
  166. selection.first.x = 0;
  167. selection.second.x = plot.width();
  168. }
  169. else {
  170. axis = ranges["xaxis"]? axes["xaxis"]: (ranges["x2axis"]? axes["x2axis"]: axes["xaxis"]);
  171. range = ranges["xaxis"] || ranges["x2axis"] || { from:ranges["x1"], to:ranges["x2"] }
  172. selection.first.x = axis.p2c(Math.min(range.from, range.to));
  173. selection.second.x = axis.p2c(Math.max(range.from, range.to));
  174. }
  175. if (o.selection.mode == "x") {
  176. selection.first.y = 0;
  177. selection.second.y = plot.height();
  178. }
  179. else {
  180. axis = ranges["yaxis"]? axes["yaxis"]: (ranges["y2axis"]? axes["y2axis"]: axes["yaxis"]);
  181. range = ranges["yaxis"] || ranges["y2axis"] || { from:ranges["y1"], to:ranges["y2"] }
  182. selection.first.y = axis.p2c(Math.min(range.from, range.to));
  183. selection.second.y = axis.p2c(Math.max(range.from, range.to));
  184. }
  185. selection.show = true;
  186. plot.triggerRedrawOverlay();
  187. if (!preventEvent)
  188. triggerSelectedEvent();
  189. }
  190. function selectionIsSane() {
  191. var minSize = 5;
  192. return Math.abs(selection.second.x - selection.first.x) >= minSize &&
  193. Math.abs(selection.second.y - selection.first.y) >= minSize;
  194. }
  195. plot.clearSelection = clearSelection;
  196. plot.setSelection = setSelection;
  197. plot.getSelection = getSelection;
  198. plot.hooks.bindEvents.push(function(plot, eventHolder) {
  199. var o = plot.getOptions();
  200. if (o.selection.mode != null)
  201. eventHolder.mousemove(onMouseMove);
  202. if (o.selection.mode != null)
  203. eventHolder.mousedown(onMouseDown);
  204. });
  205. plot.hooks.drawOverlay.push(function (plot, ctx) {
  206. // draw selection
  207. if (selection.show && selectionIsSane()) {
  208. var plotOffset = plot.getPlotOffset();
  209. var o = plot.getOptions();
  210. ctx.save();
  211. ctx.translate(plotOffset.left, plotOffset.top);
  212. var c = $.color.parse(o.selection.color);
  213. ctx.strokeStyle = c.scale('a', 0.8).toString();
  214. ctx.lineWidth = 1;
  215. ctx.lineJoin = "round";
  216. ctx.fillStyle = c.scale('a', 0.4).toString();
  217. var x = Math.min(selection.first.x, selection.second.x),
  218. y = Math.min(selection.first.y, selection.second.y),
  219. w = Math.abs(selection.second.x - selection.first.x),
  220. h = Math.abs(selection.second.y - selection.first.y);
  221. ctx.fillRect(x, y, w, h);
  222. ctx.strokeRect(x, y, w, h);
  223. ctx.restore();
  224. }
  225. });
  226. }
  227. $.plot.plugins.push({
  228. init: init,
  229. options: {
  230. selection: {
  231. mode: null, // one of null, "x", "y" or "xy"
  232. color: "#e8cfac"
  233. }
  234. },
  235. name: 'selection',
  236. version: '1.0'
  237. });
  238. })(jQuery);