jquery.flot.navigate.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /*
  2. Flot plugin for adding panning and zooming capabilities to a plot.
  3. The default behaviour is double click and scrollwheel up/down to zoom
  4. in, drag to pan. The plugin defines plot.zoom({ center }),
  5. plot.zoomOut() and plot.pan(offset) so you easily can add custom
  6. controls. It also fires a "plotpan" and "plotzoom" event when
  7. something happens, useful for synchronizing plots.
  8. Options:
  9. zoom: {
  10. interactive: false
  11. trigger: "dblclick" // or "click" for single click
  12. amount: 1.5 // 2 = 200% (zoom in), 0.5 = 50% (zoom out)
  13. }
  14. pan: {
  15. interactive: false
  16. cursor: "move" // CSS mouse cursor value used when dragging, e.g. "pointer"
  17. frameRate: 20
  18. }
  19. xaxis, yaxis, x2axis, y2axis: {
  20. zoomRange: null // or [number, number] (min range, max range) or false
  21. panRange: null // or [number, number] (min, max) or false
  22. }
  23. "interactive" enables the built-in drag/click behaviour. If you enable
  24. interactive for pan, then you'll have a basic plot that supports
  25. moving around; the same for zoom.
  26. "amount" specifies the default amount to zoom in (so 1.5 = 150%)
  27. relative to the current viewport.
  28. "cursor" is a standard CSS mouse cursor string used for visual
  29. feedback to the user when dragging.
  30. "frameRate" specifies the maximum number of times per second the plot
  31. will update itself while the user is panning around on it (set to null
  32. to disable intermediate pans, the plot will then not update until the
  33. mouse button is released).
  34. "zoomRange" is the interval in which zooming can happen, e.g. with
  35. zoomRange: [1, 100] the zoom will never scale the axis so that the
  36. difference between min and max is smaller than 1 or larger than 100.
  37. You can set either end to null to ignore, e.g. [1, null]. If you set
  38. zoomRange to false, zooming on that axis will be disabled.
  39. "panRange" confines the panning to stay within a range, e.g. with
  40. panRange: [-10, 20] panning stops at -10 in one end and at 20 in the
  41. other. Either can be null, e.g. [-10, null]. If you set
  42. panRange to false, panning on that axis will be disabled.
  43. Example API usage:
  44. plot = $.plot(...);
  45. // zoom default amount in on the pixel (10, 20)
  46. plot.zoom({ center: { left: 10, top: 20 } });
  47. // zoom out again
  48. plot.zoomOut({ center: { left: 10, top: 20 } });
  49. // zoom 200% in on the pixel (10, 20)
  50. plot.zoom({ amount: 2, center: { left: 10, top: 20 } });
  51. // pan 100 pixels to the left and 20 down
  52. plot.pan({ left: -100, top: 20 })
  53. Here, "center" specifies where the center of the zooming should
  54. happen. Note that this is defined in pixel space, not the space of the
  55. data points (you can use the p2c helpers on the axes in Flot to help
  56. you convert between these).
  57. "amount" is the amount to zoom the viewport relative to the current
  58. range, so 1 is 100% (i.e. no change), 1.5 is 150% (zoom in), 0.7 is
  59. 70% (zoom out). You can set the default in the options.
  60. */
  61. // First two dependencies, jquery.event.drag.js and
  62. // jquery.mousewheel.js, we put them inline here to save people the
  63. // effort of downloading them.
  64. /*
  65. jquery.event.drag.js ~ v1.5 ~ Copyright (c) 2008, Three Dub Media (http://threedubmedia.com)
  66. Licensed under the MIT License ~ http://threedubmedia.googlecode.com/files/MIT-LICENSE.txt
  67. */
  68. (function(E){E.fn.drag=function(L,K,J){if(K){this.bind("dragstart",L)}if(J){this.bind("dragend",J)}return !L?this.trigger("drag"):this.bind("drag",K?K:L)};var A=E.event,B=A.special,F=B.drag={not:":input",distance:0,which:1,dragging:false,setup:function(J){J=E.extend({distance:F.distance,which:F.which,not:F.not},J||{});J.distance=I(J.distance);A.add(this,"mousedown",H,J);if(this.attachEvent){this.attachEvent("ondragstart",D)}},teardown:function(){A.remove(this,"mousedown",H);if(this===F.dragging){F.dragging=F.proxy=false}G(this,true);if(this.detachEvent){this.detachEvent("ondragstart",D)}}};B.dragstart=B.dragend={setup:function(){},teardown:function(){}};function H(L){var K=this,J,M=L.data||{};if(M.elem){K=L.dragTarget=M.elem;L.dragProxy=F.proxy||K;L.cursorOffsetX=M.pageX-M.left;L.cursorOffsetY=M.pageY-M.top;L.offsetX=L.pageX-L.cursorOffsetX;L.offsetY=L.pageY-L.cursorOffsetY}else{if(F.dragging||(M.which>0&&L.which!=M.which)||E(L.target).is(M.not)){return }}switch(L.type){case"mousedown":E.extend(M,E(K).offset(),{elem:K,target:L.target,pageX:L.pageX,pageY:L.pageY});A.add(document,"mousemove mouseup",H,M);G(K,false);F.dragging=null;return false;case !F.dragging&&"mousemove":if(I(L.pageX-M.pageX)+I(L.pageY-M.pageY)<M.distance){break}L.target=M.target;J=C(L,"dragstart",K);if(J!==false){F.dragging=K;F.proxy=L.dragProxy=E(J||K)[0]}case"mousemove":if(F.dragging){J=C(L,"drag",K);if(B.drop){B.drop.allowed=(J!==false);B.drop.handler(L)}if(J!==false){break}L.type="mouseup"}case"mouseup":A.remove(document,"mousemove mouseup",H);if(F.dragging){if(B.drop){B.drop.handler(L)}C(L,"dragend",K)}G(K,true);F.dragging=F.proxy=M.elem=false;break}return true}function C(M,K,L){M.type=K;var J=E.event.handle.call(L,M);return J===false?false:J||M.result}function I(J){return Math.pow(J,2)}function D(){return(F.dragging===false)}function G(K,J){if(!K){return }K.unselectable=J?"off":"on";K.onselectstart=function(){return J};if(K.style){K.style.MozUserSelect=J?"":"none"}}})(jQuery);
  69. /* jquery.mousewheel.min.js
  70. * Copyright (c) 2009 Brandon Aaron (http://brandonaaron.net)
  71. * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
  72. * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
  73. * Thanks to: http://adomas.org/javascript-mouse-wheel/ for some pointers.
  74. * Thanks to: Mathias Bank(http://www.mathias-bank.de) for a scope bug fix.
  75. *
  76. * Version: 3.0.2
  77. *
  78. * Requires: 1.2.2+
  79. */
  80. (function(c){var a=["DOMMouseScroll","mousewheel"];c.event.special.mousewheel={setup:function(){if(this.addEventListener){for(var d=a.length;d;){this.addEventListener(a[--d],b,false)}}else{this.onmousewheel=b}},teardown:function(){if(this.removeEventListener){for(var d=a.length;d;){this.removeEventListener(a[--d],b,false)}}else{this.onmousewheel=null}}};c.fn.extend({mousewheel:function(d){return d?this.bind("mousewheel",d):this.trigger("mousewheel")},unmousewheel:function(d){return this.unbind("mousewheel",d)}});function b(f){var d=[].slice.call(arguments,1),g=0,e=true;f=c.event.fix(f||window.event);f.type="mousewheel";if(f.wheelDelta){g=f.wheelDelta/120}if(f.detail){g=-f.detail/3}d.unshift(f,g);return c.event.handle.apply(this,d)}})(jQuery);
  81. (function ($) {
  82. var options = {
  83. xaxis: {
  84. zoomRange: null, // or [number, number] (min range, max range)
  85. panRange: null // or [number, number] (min, max)
  86. },
  87. zoom: {
  88. interactive: false,
  89. trigger: "dblclick", // or "click" for single click
  90. amount: 1.5 // how much to zoom relative to current position, 2 = 200% (zoom in), 0.5 = 50% (zoom out)
  91. },
  92. pan: {
  93. interactive: false,
  94. cursor: "move",
  95. frameRate: 20
  96. }
  97. };
  98. function init(plot) {
  99. function onZoomClick(e, zoomOut) {
  100. var c = plot.offset();
  101. c.left = e.pageX - c.left;
  102. c.top = e.pageY - c.top;
  103. if (zoomOut)
  104. plot.zoomOut({ center: c });
  105. else
  106. plot.zoom({ center: c });
  107. }
  108. function onMouseWheel(e, delta) {
  109. onZoomClick(e, delta < 0);
  110. return false;
  111. }
  112. var prevCursor = 'default', prevPageX = 0, prevPageY = 0,
  113. panTimeout = null;
  114. function onDragStart(e) {
  115. if (e.which != 1) // only accept left-click
  116. return false;
  117. var c = plot.getPlaceholder().css('cursor');
  118. if (c)
  119. prevCursor = c;
  120. plot.getPlaceholder().css('cursor', plot.getOptions().pan.cursor);
  121. prevPageX = e.pageX;
  122. prevPageY = e.pageY;
  123. }
  124. function onDrag(e) {
  125. var frameRate = plot.getOptions().pan.frameRate;
  126. if (panTimeout || !frameRate)
  127. return;
  128. panTimeout = setTimeout(function () {
  129. plot.pan({ left: prevPageX - e.pageX,
  130. top: prevPageY - e.pageY });
  131. prevPageX = e.pageX;
  132. prevPageY = e.pageY;
  133. panTimeout = null;
  134. }, 1 / frameRate * 1000);
  135. }
  136. function onDragEnd(e) {
  137. if (panTimeout) {
  138. clearTimeout(panTimeout);
  139. panTimeout = null;
  140. }
  141. plot.getPlaceholder().css('cursor', prevCursor);
  142. plot.pan({ left: prevPageX - e.pageX,
  143. top: prevPageY - e.pageY });
  144. }
  145. function bindEvents(plot, eventHolder) {
  146. var o = plot.getOptions();
  147. if (o.zoom.interactive) {
  148. eventHolder[o.zoom.trigger](onZoomClick);
  149. eventHolder.mousewheel(onMouseWheel);
  150. }
  151. if (o.pan.interactive) {
  152. eventHolder.bind("dragstart", { distance: 10 }, onDragStart);
  153. eventHolder.bind("drag", onDrag);
  154. eventHolder.bind("dragend", onDragEnd);
  155. }
  156. }
  157. plot.zoomOut = function (args) {
  158. if (!args)
  159. args = {};
  160. if (!args.amount)
  161. args.amount = plot.getOptions().zoom.amount
  162. args.amount = 1 / args.amount;
  163. plot.zoom(args);
  164. }
  165. plot.zoom = function (args) {
  166. if (!args)
  167. args = {};
  168. var c = args.center,
  169. amount = args.amount || plot.getOptions().zoom.amount,
  170. w = plot.width(), h = plot.height();
  171. if (!c)
  172. c = { left: w / 2, top: h / 2 };
  173. var xf = c.left / w,
  174. yf = c.top / h,
  175. minmax = {
  176. x: {
  177. min: c.left - xf * w / amount,
  178. max: c.left + (1 - xf) * w / amount
  179. },
  180. y: {
  181. min: c.top - yf * h / amount,
  182. max: c.top + (1 - yf) * h / amount
  183. }
  184. };
  185. $.each(plot.getAxes(), function(_, axis) {
  186. var opts = axis.options,
  187. min = minmax[axis.direction].min,
  188. max = minmax[axis.direction].max,
  189. zr = opts.zoomRange;
  190. if (zr === false) // no zooming on this axis
  191. return;
  192. min = axis.c2p(min);
  193. max = axis.c2p(max);
  194. if (min > max) {
  195. // make sure min < max
  196. var tmp = min;
  197. min = max;
  198. max = tmp;
  199. }
  200. var range = max - min;
  201. if (zr &&
  202. ((zr[0] != null && range < zr[0]) ||
  203. (zr[1] != null && range > zr[1])))
  204. return;
  205. opts.min = min;
  206. opts.max = max;
  207. });
  208. plot.setupGrid();
  209. plot.draw();
  210. if (!args.preventEvent)
  211. plot.getPlaceholder().trigger("plotzoom", [ plot ]);
  212. }
  213. plot.pan = function (args) {
  214. var delta = {
  215. x: +args.left,
  216. y: +args.top
  217. };
  218. if (isNaN(delta.x))
  219. delta.x = 0;
  220. if (isNaN(delta.y))
  221. delta.y = 0;
  222. $.each(plot.getAxes(), function (_, axis) {
  223. var opts = axis.options,
  224. min, max, d = delta[axis.direction];
  225. min = axis.c2p(axis.p2c(axis.min) + d),
  226. max = axis.c2p(axis.p2c(axis.max) + d);
  227. var pr = opts.panRange;
  228. if (pr === false) // no panning on this axis
  229. return;
  230. if (pr) {
  231. // check whether we hit the wall
  232. if (pr[0] != null && pr[0] > min) {
  233. d = pr[0] - min;
  234. min += d;
  235. max += d;
  236. }
  237. if (pr[1] != null && pr[1] < max) {
  238. d = pr[1] - max;
  239. min += d;
  240. max += d;
  241. }
  242. }
  243. opts.min = min;
  244. opts.max = max;
  245. });
  246. plot.setupGrid();
  247. plot.draw();
  248. if (!args.preventEvent)
  249. plot.getPlaceholder().trigger("plotpan", [ plot ]);
  250. }
  251. function shutdown(plot, eventHolder) {
  252. eventHolder.unbind(plot.getOptions().zoom.trigger, onZoomClick);
  253. eventHolder.unbind("mousewheel", onMouseWheel);
  254. eventHolder.unbind("dragstart", onDragStart);
  255. eventHolder.unbind("drag", onDrag);
  256. eventHolder.unbind("dragend", onDragEnd);
  257. if (panTimeout)
  258. clearTimeout(panTimeout);
  259. }
  260. plot.hooks.bindEvents.push(bindEvents);
  261. plot.hooks.shutdown.push(shutdown);
  262. }
  263. $.plot.plugins.push({
  264. init: init,
  265. options: options,
  266. name: 'navigate',
  267. version: '1.3'
  268. });
  269. })(jQuery);