jquery.flot.selection.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. /* Flot plugin for selecting regions of a plot.
  2. Copyright (c) 2007-2014 IOLA and Ole Laursen.
  3. Licensed under the MIT license.
  4. The plugin supports these options:
  5. selection: {
  6. mode: null or "x" or "y" or "xy" or "smart",
  7. color: color,
  8. shape: "round" or "miter" or "bevel",
  9. minSize: number of pixels
  10. }
  11. Selection support is enabled by setting the mode to one of "x", "y" or "xy".
  12. In "x" mode, the user will only be able to specify the x range, similarly for
  13. "y" mode. For "xy", the selection becomes a rectangle where both ranges can be
  14. specified. "color" is color of the selection (if you need to change the color
  15. later on, you can get to it with plot.getOptions().selection.color). "shape"
  16. is the shape of the corners of the selection.
  17. "minSize" is the minimum size a selection can be in pixels. This value can
  18. be customized to determine the smallest size a selection can be and still
  19. have the selection rectangle be displayed. When customizing this value, the
  20. fact that it refers to pixels, not axis units must be taken into account.
  21. Thus, for example, if there is a bar graph in time mode with BarWidth set to 1
  22. minute, setting "minSize" to 1 will not make the minimum selection size 1
  23. minute, but rather 1 pixel. Note also that setting "minSize" to 0 will prevent
  24. "plotunselected" events from being fired when the user clicks the mouse without
  25. dragging.
  26. When selection support is enabled, a "plotselected" event will be emitted on
  27. the DOM element you passed into the plot function. The event handler gets a
  28. parameter with the ranges selected on the axes, like this:
  29. placeholder.bind( "plotselected", function( event, ranges ) {
  30. alert("You selected " + ranges.xaxis.from + " to " + ranges.xaxis.to)
  31. // similar for yaxis - with multiple axes, the extra ones are in
  32. // x2axis, x3axis, ...
  33. });
  34. The "plotselected" event is only fired when the user has finished making the
  35. selection. A "plotselecting" event is fired during the process with the same
  36. parameters as the "plotselected" event, in case you want to know what's
  37. happening while it's happening,
  38. A "plotunselected" event with no arguments is emitted when the user clicks the
  39. mouse to remove the selection. As stated above, setting "minSize" to 0 will
  40. destroy this behavior.
  41. The plugin allso adds the following methods to the plot object:
  42. - setSelection( ranges, preventEvent )
  43. Set the selection rectangle. The passed in ranges is on the same form as
  44. returned in the "plotselected" event. If the selection mode is "x", you
  45. should put in either an xaxis range, if the mode is "y" you need to put in
  46. an yaxis range and both xaxis and yaxis if the selection mode is "xy", like
  47. this:
  48. setSelection({ xaxis: { from: 0, to: 10 }, yaxis: { from: 40, to: 60 } });
  49. setSelection will trigger the "plotselected" event when called. If you don't
  50. want that to happen, e.g. if you're inside a "plotselected" handler, pass
  51. true as the second parameter. If you are using multiple axes, you can
  52. specify the ranges on any of those, e.g. as x2axis/x3axis/... instead of
  53. xaxis, the plugin picks the first one it sees.
  54. - clearSelection( preventEvent )
  55. Clear the selection rectangle. Pass in true to avoid getting a
  56. "plotunselected" event.
  57. - getSelection()
  58. Returns the current selection in the same format as the "plotselected"
  59. event. If there's currently no selection, the function returns null.
  60. */
  61. (function ($) {
  62. function init(plot) {
  63. var selection = {
  64. first: {x: -1, y: -1},
  65. second: {x: -1, y: -1},
  66. show: false,
  67. currentMode: 'xy',
  68. active: false
  69. };
  70. var SNAPPING_CONSTANT = $.plot.uiConstants.SNAPPING_CONSTANT;
  71. // FIXME: The drag handling implemented here should be
  72. // abstracted out, there's some similar code from a library in
  73. // the navigation plugin, this should be massaged a bit to fit
  74. // the Flot cases here better and reused. Doing this would
  75. // make this plugin much slimmer.
  76. var savedhandlers = {};
  77. var mouseUpHandler = null;
  78. function onMouseMove(e) {
  79. if (selection.active) {
  80. updateSelection(e);
  81. plot.getPlaceholder().trigger("plotselecting", [ getSelection() ]);
  82. }
  83. }
  84. function onMouseDown(e) {
  85. // only accept left-click
  86. if (e.which !== 1) return;
  87. // cancel out any text selections
  88. document.body.focus();
  89. // prevent text selection and drag in old-school browsers
  90. if (document.onselectstart !== undefined && savedhandlers.onselectstart == null) {
  91. savedhandlers.onselectstart = document.onselectstart;
  92. document.onselectstart = function () { return false; };
  93. }
  94. if (document.ondrag !== undefined && savedhandlers.ondrag == null) {
  95. savedhandlers.ondrag = document.ondrag;
  96. document.ondrag = function () { return false; };
  97. }
  98. setSelectionPos(selection.first, e);
  99. selection.active = true;
  100. // this is a bit silly, but we have to use a closure to be
  101. // able to whack the same handler again
  102. mouseUpHandler = function (e) { onMouseUp(e); };
  103. $(document).one("mouseup", mouseUpHandler);
  104. }
  105. function onMouseUp(e) {
  106. mouseUpHandler = null;
  107. // revert drag stuff for old-school browsers
  108. if (document.onselectstart !== undefined) {
  109. document.onselectstart = savedhandlers.onselectstart;
  110. }
  111. if (document.ondrag !== undefined) {
  112. document.ondrag = savedhandlers.ondrag;
  113. }
  114. // no more dragging
  115. selection.active = false;
  116. updateSelection(e);
  117. if (selectionIsSane()) {
  118. triggerSelectedEvent();
  119. } else {
  120. // this counts as a clear
  121. plot.getPlaceholder().trigger("plotunselected", [ ]);
  122. plot.getPlaceholder().trigger("plotselecting", [ null ]);
  123. }
  124. return false;
  125. }
  126. function getSelection() {
  127. if (!selectionIsSane()) return null;
  128. if (!selection.show) return null;
  129. var r = {},
  130. c1 = {x: selection.first.x, y: selection.first.y},
  131. c2 = {x: selection.second.x, y: selection.second.y};
  132. if (selectionDirection(plot) === 'x') {
  133. c1.y = 0;
  134. c2.y = plot.height();
  135. }
  136. if (selectionDirection(plot) === 'y') {
  137. c1.x = 0;
  138. c2.x = plot.width();
  139. }
  140. $.each(plot.getAxes(), function (name, axis) {
  141. if (axis.used) {
  142. var p1 = axis.c2p(c1[axis.direction]), p2 = axis.c2p(c2[axis.direction]);
  143. r[name] = { from: Math.min(p1, p2), to: Math.max(p1, p2) };
  144. }
  145. });
  146. return r;
  147. }
  148. function triggerSelectedEvent() {
  149. var r = getSelection();
  150. plot.getPlaceholder().trigger("plotselected", [ r ]);
  151. // backwards-compat stuff, to be removed in future
  152. if (r.xaxis && r.yaxis) {
  153. plot.getPlaceholder().trigger("selected", [ { x1: r.xaxis.from, y1: r.yaxis.from, x2: r.xaxis.to, y2: r.yaxis.to } ]);
  154. }
  155. }
  156. function clamp(min, value, max) {
  157. return value < min ? min : (value > max ? max : value);
  158. }
  159. function selectionDirection(plot) {
  160. var o = plot.getOptions();
  161. if (o.selection.mode === 'smart') {
  162. return selection.currentMode;
  163. } else {
  164. return o.selection.mode;
  165. }
  166. }
  167. function updateMode(pos) {
  168. if (selection.first) {
  169. var delta = {
  170. x: pos.x - selection.first.x,
  171. y: pos.y - selection.first.y
  172. };
  173. if (Math.abs(delta.x) < SNAPPING_CONSTANT) {
  174. selection.currentMode = 'y';
  175. } else if (Math.abs(delta.y) < SNAPPING_CONSTANT) {
  176. selection.currentMode = 'x';
  177. } else {
  178. selection.currentMode = 'xy';
  179. }
  180. }
  181. }
  182. function setSelectionPos(pos, e) {
  183. var offset = plot.getPlaceholder().offset();
  184. var plotOffset = plot.getPlotOffset();
  185. pos.x = clamp(0, e.pageX - offset.left - plotOffset.left, plot.width());
  186. pos.y = clamp(0, e.pageY - offset.top - plotOffset.top, plot.height());
  187. if (pos !== selection.first) updateMode(pos);
  188. if (selectionDirection(plot) === "y") {
  189. pos.x = pos === selection.first ? 0 : plot.width();
  190. }
  191. if (selectionDirection(plot) === "x") {
  192. pos.y = pos === selection.first ? 0 : plot.height();
  193. }
  194. }
  195. function updateSelection(pos) {
  196. if (pos.pageX == null) return;
  197. setSelectionPos(selection.second, pos);
  198. if (selectionIsSane()) {
  199. selection.show = true;
  200. plot.triggerRedrawOverlay();
  201. } else clearSelection(true);
  202. }
  203. function clearSelection(preventEvent) {
  204. if (selection.show) {
  205. selection.show = false;
  206. selection.currentMode = '';
  207. plot.triggerRedrawOverlay();
  208. if (!preventEvent) {
  209. plot.getPlaceholder().trigger("plotunselected", [ ]);
  210. }
  211. }
  212. }
  213. // function taken from markings support in Flot
  214. function extractRange(ranges, coord) {
  215. var axis, from, to, key, axes = plot.getAxes();
  216. for (var k in axes) {
  217. axis = axes[k];
  218. if (axis.direction === coord) {
  219. key = coord + axis.n + "axis";
  220. if (!ranges[key] && axis.n === 1) {
  221. // support x1axis as xaxis
  222. key = coord + "axis";
  223. }
  224. if (ranges[key]) {
  225. from = ranges[key].from;
  226. to = ranges[key].to;
  227. break;
  228. }
  229. }
  230. }
  231. // backwards-compat stuff - to be removed in future
  232. if (!ranges[key]) {
  233. axis = coord === "x" ? plot.getXAxes()[0] : plot.getYAxes()[0];
  234. from = ranges[coord + "1"];
  235. to = ranges[coord + "2"];
  236. }
  237. // auto-reverse as an added bonus
  238. if (from != null && to != null && from > to) {
  239. var tmp = from;
  240. from = to;
  241. to = tmp;
  242. }
  243. return { from: from, to: to, axis: axis };
  244. }
  245. function setSelection(ranges, preventEvent) {
  246. var range;
  247. if (selectionDirection(plot) === "y") {
  248. selection.first.x = 0;
  249. selection.second.x = plot.width();
  250. } else {
  251. range = extractRange(ranges, "x");
  252. selection.first.x = range.axis.p2c(range.from);
  253. selection.second.x = range.axis.p2c(range.to);
  254. }
  255. if (selectionDirection(plot) === "x") {
  256. selection.first.y = 0;
  257. selection.second.y = plot.height();
  258. } else {
  259. range = extractRange(ranges, "y");
  260. selection.first.y = range.axis.p2c(range.from);
  261. selection.second.y = range.axis.p2c(range.to);
  262. }
  263. selection.show = true;
  264. plot.triggerRedrawOverlay();
  265. if (!preventEvent && selectionIsSane()) {
  266. triggerSelectedEvent();
  267. }
  268. }
  269. function selectionIsSane() {
  270. var minSize = plot.getOptions().selection.minSize;
  271. return Math.abs(selection.second.x - selection.first.x) >= minSize &&
  272. Math.abs(selection.second.y - selection.first.y) >= minSize;
  273. }
  274. plot.clearSelection = clearSelection;
  275. plot.setSelection = setSelection;
  276. plot.getSelection = getSelection;
  277. plot.hooks.bindEvents.push(function(plot, eventHolder) {
  278. var o = plot.getOptions();
  279. if (o.selection.mode != null) {
  280. eventHolder.mousemove(onMouseMove);
  281. eventHolder.mousedown(onMouseDown);
  282. }
  283. });
  284. function drawSelectionDecorations(ctx, x, y, w, h, oX, oY, mode) {
  285. var spacing = 3;
  286. var fullEarWidth = 15;
  287. var earWidth = Math.max(0, Math.min(fullEarWidth, w / 2 - 2, h / 2 - 2));
  288. ctx.fillStyle = '#ffffff';
  289. if (mode === 'xy') {
  290. ctx.beginPath();
  291. ctx.moveTo(x, y + earWidth);
  292. ctx.lineTo(x - 3, y + earWidth);
  293. ctx.lineTo(x - 3, y - 3);
  294. ctx.lineTo(x + earWidth, y - 3);
  295. ctx.lineTo(x + earWidth, y);
  296. ctx.lineTo(x, y);
  297. ctx.closePath();
  298. ctx.moveTo(x, y + h - earWidth);
  299. ctx.lineTo(x - 3, y + h - earWidth);
  300. ctx.lineTo(x - 3, y + h + 3);
  301. ctx.lineTo(x + earWidth, y + h + 3);
  302. ctx.lineTo(x + earWidth, y + h);
  303. ctx.lineTo(x, y + h);
  304. ctx.closePath();
  305. ctx.moveTo(x + w, y + earWidth);
  306. ctx.lineTo(x + w + 3, y + earWidth);
  307. ctx.lineTo(x + w + 3, y - 3);
  308. ctx.lineTo(x + w - earWidth, y - 3);
  309. ctx.lineTo(x + w - earWidth, y);
  310. ctx.lineTo(x + w, y);
  311. ctx.closePath();
  312. ctx.moveTo(x + w, y + h - earWidth);
  313. ctx.lineTo(x + w + 3, y + h - earWidth);
  314. ctx.lineTo(x + w + 3, y + h + 3);
  315. ctx.lineTo(x + w - earWidth, y + h + 3);
  316. ctx.lineTo(x + w - earWidth, y + h);
  317. ctx.lineTo(x + w, y + h);
  318. ctx.closePath();
  319. ctx.stroke();
  320. ctx.fill();
  321. }
  322. x = oX;
  323. y = oY;
  324. if (mode === 'x') {
  325. ctx.beginPath();
  326. ctx.moveTo(x, y + fullEarWidth);
  327. ctx.lineTo(x, y - fullEarWidth);
  328. ctx.lineTo(x - spacing, y - fullEarWidth);
  329. ctx.lineTo(x - spacing, y + fullEarWidth);
  330. ctx.closePath();
  331. ctx.moveTo(x + w, y + fullEarWidth);
  332. ctx.lineTo(x + w, y - fullEarWidth);
  333. ctx.lineTo(x + w + spacing, y - fullEarWidth);
  334. ctx.lineTo(x + w + spacing, y + fullEarWidth);
  335. ctx.closePath();
  336. ctx.stroke();
  337. ctx.fill();
  338. }
  339. if (mode === 'y') {
  340. ctx.beginPath();
  341. ctx.moveTo(x - fullEarWidth, y);
  342. ctx.lineTo(x + fullEarWidth, y);
  343. ctx.lineTo(x + fullEarWidth, y - spacing);
  344. ctx.lineTo(x - fullEarWidth, y - spacing);
  345. ctx.closePath();
  346. ctx.moveTo(x - fullEarWidth, y + h);
  347. ctx.lineTo(x + fullEarWidth, y + h);
  348. ctx.lineTo(x + fullEarWidth, y + h + spacing);
  349. ctx.lineTo(x - fullEarWidth, y + h + spacing);
  350. ctx.closePath();
  351. ctx.stroke();
  352. ctx.fill();
  353. }
  354. }
  355. plot.hooks.drawOverlay.push(function (plot, ctx) {
  356. // draw selection
  357. if (selection.show && selectionIsSane()) {
  358. var plotOffset = plot.getPlotOffset();
  359. var o = plot.getOptions();
  360. ctx.save();
  361. ctx.translate(plotOffset.left, plotOffset.top);
  362. var c = $.color.parse(o.selection.color);
  363. ctx.strokeStyle = c.scale('a', 1).toString();
  364. ctx.lineWidth = 1;
  365. ctx.lineJoin = o.selection.shape;
  366. ctx.fillStyle = c.scale('a', 0.4).toString();
  367. var x = Math.min(selection.first.x, selection.second.x) + 0.5,
  368. oX = x,
  369. y = Math.min(selection.first.y, selection.second.y) + 0.5,
  370. oY = y,
  371. w = Math.abs(selection.second.x - selection.first.x) - 1,
  372. h = Math.abs(selection.second.y - selection.first.y) - 1;
  373. if (selectionDirection(plot) === 'x') {
  374. h += y;
  375. y = 0;
  376. }
  377. if (selectionDirection(plot) === 'y') {
  378. w += x;
  379. x = 0;
  380. }
  381. ctx.fillRect(0, 0, plot.width(), plot.height());
  382. ctx.clearRect(x, y, w, h);
  383. drawSelectionDecorations(ctx, x, y, w, h, oX, oY, selectionDirection(plot));
  384. ctx.restore();
  385. }
  386. });
  387. plot.hooks.shutdown.push(function (plot, eventHolder) {
  388. eventHolder.unbind("mousemove", onMouseMove);
  389. eventHolder.unbind("mousedown", onMouseDown);
  390. if (mouseUpHandler) {
  391. $(document).unbind("mouseup", mouseUpHandler);
  392. }
  393. });
  394. }
  395. $.plot.plugins.push({
  396. init: init,
  397. options: {
  398. selection: {
  399. mode: null, // one of null, "x", "y" or "xy"
  400. color: "#888888",
  401. shape: "round", // one of "round", "miter", or "bevel"
  402. minSize: 5 // minimum number of pixels
  403. }
  404. },
  405. name: 'selection',
  406. version: '1.1'
  407. });
  408. })(jQuery);