jquery.flot.crosshair.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /* Flot plugin for showing crosshairs when the mouse hovers over the plot.
  2. Copyright (c) 2007-2014 IOLA and Ole Laursen.
  3. Licensed under the MIT license.
  4. The plugin supports these options:
  5. crosshair: {
  6. mode: null or "x" or "y" or "xy"
  7. color: color
  8. lineWidth: number
  9. }
  10. Set the mode to one of "x", "y" or "xy". The "x" mode enables a vertical
  11. crosshair that lets you trace the values on the x axis, "y" enables a
  12. horizontal crosshair and "xy" enables them both. "color" is the color of the
  13. crosshair (default is "rgba(170, 0, 0, 0.80)"), "lineWidth" is the width of
  14. the drawn lines (default is 1).
  15. The plugin also adds four public methods:
  16. - setCrosshair( pos )
  17. Set the position of the crosshair. Note that this is cleared if the user
  18. moves the mouse. "pos" is in coordinates of the plot and should be on the
  19. form { x: xpos, y: ypos } (you can use x2/x3/... if you're using multiple
  20. axes), which is coincidentally the same format as what you get from a
  21. "plothover" event. If "pos" is null, the crosshair is cleared.
  22. - clearCrosshair()
  23. Clear the crosshair.
  24. - lockCrosshair(pos)
  25. Cause the crosshair to lock to the current location, no longer updating if
  26. the user moves the mouse. Optionally supply a position (passed on to
  27. setCrosshair()) to move it to.
  28. Example usage:
  29. var myFlot = $.plot( $("#graph"), ..., { crosshair: { mode: "x" } } };
  30. $("#graph").bind( "plothover", function ( evt, position, item ) {
  31. if ( item ) {
  32. // Lock the crosshair to the data point being hovered
  33. myFlot.lockCrosshair({
  34. x: item.datapoint[ 0 ],
  35. y: item.datapoint[ 1 ]
  36. });
  37. } else {
  38. // Return normal crosshair operation
  39. myFlot.unlockCrosshair();
  40. }
  41. });
  42. - unlockCrosshair()
  43. Free the crosshair to move again after locking it.
  44. */
  45. (function ($) {
  46. var options = {
  47. crosshair: {
  48. mode: null, // one of null, "x", "y" or "xy",
  49. color: "rgba(170, 0, 0, 0.80)",
  50. lineWidth: 1
  51. }
  52. };
  53. function init(plot) {
  54. // position of crosshair in pixels
  55. var crosshair = {x: -1, y: -1, locked: false, highlighted: false};
  56. plot.setCrosshair = function setCrosshair(pos) {
  57. if (!pos) {
  58. crosshair.x = -1;
  59. } else {
  60. var o = plot.p2c(pos);
  61. crosshair.x = Math.max(0, Math.min(o.left, plot.width()));
  62. crosshair.y = Math.max(0, Math.min(o.top, plot.height()));
  63. }
  64. plot.triggerRedrawOverlay();
  65. };
  66. plot.clearCrosshair = plot.setCrosshair; // passes null for pos
  67. plot.lockCrosshair = function lockCrosshair(pos) {
  68. if (pos) {
  69. plot.setCrosshair(pos);
  70. }
  71. crosshair.locked = true;
  72. };
  73. plot.unlockCrosshair = function unlockCrosshair() {
  74. crosshair.locked = false;
  75. crosshair.rect = null;
  76. };
  77. function onMouseOut(e) {
  78. if (crosshair.locked) {
  79. return;
  80. }
  81. if (crosshair.x !== -1) {
  82. crosshair.x = -1;
  83. plot.triggerRedrawOverlay();
  84. }
  85. }
  86. function onMouseMove(e) {
  87. var offset = plot.offset();
  88. if (crosshair.locked) {
  89. var mouseX = Math.max(0, Math.min(e.pageX - offset.left, plot.width()));
  90. var mouseY = Math.max(0, Math.min(e.pageY - offset.top, plot.height()));
  91. if ((mouseX > crosshair.x - 4) && (mouseX < crosshair.x + 4) && (mouseY > crosshair.y - 4) && (mouseY < crosshair.y + 4)) {
  92. if (!crosshair.highlighted) {
  93. crosshair.highlighted = true;
  94. plot.triggerRedrawOverlay();
  95. }
  96. } else {
  97. if (crosshair.highlighted) {
  98. crosshair.highlighted = false;
  99. plot.triggerRedrawOverlay();
  100. }
  101. }
  102. return;
  103. }
  104. if (plot.getSelection && plot.getSelection()) {
  105. crosshair.x = -1; // hide the crosshair while selecting
  106. return;
  107. }
  108. crosshair.x = Math.max(0, Math.min(e.pageX - offset.left, plot.width()));
  109. crosshair.y = Math.max(0, Math.min(e.pageY - offset.top, plot.height()));
  110. plot.triggerRedrawOverlay();
  111. }
  112. plot.hooks.bindEvents.push(function (plot, eventHolder) {
  113. if (!plot.getOptions().crosshair.mode) {
  114. return;
  115. }
  116. eventHolder.mouseout(onMouseOut);
  117. eventHolder.mousemove(onMouseMove);
  118. });
  119. plot.hooks.drawOverlay.push(function (plot, ctx) {
  120. var c = plot.getOptions().crosshair;
  121. if (!c.mode) {
  122. return;
  123. }
  124. var plotOffset = plot.getPlotOffset();
  125. ctx.save();
  126. ctx.translate(plotOffset.left, plotOffset.top);
  127. if (crosshair.x !== -1) {
  128. var adj = plot.getOptions().crosshair.lineWidth % 2 ? 0.5 : 0;
  129. ctx.strokeStyle = c.color;
  130. ctx.lineWidth = c.lineWidth;
  131. ctx.lineJoin = "round";
  132. ctx.beginPath();
  133. if (c.mode.indexOf("x") !== -1) {
  134. var drawX = Math.floor(crosshair.x) + adj;
  135. ctx.moveTo(drawX, 0);
  136. ctx.lineTo(drawX, plot.height());
  137. }
  138. if (c.mode.indexOf("y") !== -1) {
  139. var drawY = Math.floor(crosshair.y) + adj;
  140. ctx.moveTo(0, drawY);
  141. ctx.lineTo(plot.width(), drawY);
  142. }
  143. if (crosshair.locked) {
  144. if (crosshair.highlighted) ctx.fillStyle = 'orange';
  145. else ctx.fillStyle = c.color;
  146. ctx.fillRect(Math.floor(crosshair.x) + adj - 4, Math.floor(crosshair.y) + adj - 4, 8, 8);
  147. }
  148. ctx.stroke();
  149. }
  150. ctx.restore();
  151. });
  152. plot.hooks.shutdown.push(function (plot, eventHolder) {
  153. eventHolder.unbind("mouseout", onMouseOut);
  154. eventHolder.unbind("mousemove", onMouseMove);
  155. });
  156. }
  157. $.plot.plugins.push({
  158. init: init,
  159. options: options,
  160. name: 'crosshair',
  161. version: '1.0'
  162. });
  163. })(jQuery);