jquery.flot.crosshair.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /* Flot plugin for showing crosshairs when the mouse hovers over the plot.
  2. Copyright (c) 2007-2013 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 };
  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. crosshair.locked = true;
  71. };
  72. plot.unlockCrosshair = function unlockCrosshair() {
  73. crosshair.locked = false;
  74. };
  75. function onMouseOut(e) {
  76. if (crosshair.locked)
  77. return;
  78. if (crosshair.x != -1) {
  79. crosshair.x = -1;
  80. plot.triggerRedrawOverlay();
  81. }
  82. }
  83. function onMouseMove(e) {
  84. if (crosshair.locked)
  85. return;
  86. if (plot.getSelection && plot.getSelection()) {
  87. crosshair.x = -1; // hide the crosshair while selecting
  88. return;
  89. }
  90. var offset = plot.offset();
  91. crosshair.x = Math.max(0, Math.min(e.pageX - offset.left, plot.width()));
  92. crosshair.y = Math.max(0, Math.min(e.pageY - offset.top, plot.height()));
  93. plot.triggerRedrawOverlay();
  94. }
  95. plot.hooks.bindEvents.push(function (plot, eventHolder) {
  96. if (!plot.getOptions().crosshair.mode)
  97. return;
  98. eventHolder.mouseout(onMouseOut);
  99. eventHolder.mousemove(onMouseMove);
  100. });
  101. plot.hooks.drawOverlay.push(function (plot, ctx) {
  102. var c = plot.getOptions().crosshair;
  103. if (!c.mode)
  104. return;
  105. var plotOffset = plot.getPlotOffset();
  106. ctx.save();
  107. ctx.translate(plotOffset.left, plotOffset.top);
  108. if (crosshair.x != -1) {
  109. var adj = plot.getOptions().crosshair.lineWidth % 2 === 0 ? 0 : 0.5;
  110. ctx.strokeStyle = c.color;
  111. ctx.lineWidth = c.lineWidth;
  112. ctx.lineJoin = "round";
  113. ctx.beginPath();
  114. if (c.mode.indexOf("x") != -1) {
  115. var drawX = Math.round(crosshair.x) + adj;
  116. ctx.moveTo(drawX, 0);
  117. ctx.lineTo(drawX, plot.height());
  118. }
  119. if (c.mode.indexOf("y") != -1) {
  120. var drawY = Math.round(crosshair.y) + adj;
  121. ctx.moveTo(0, drawY);
  122. ctx.lineTo(plot.width(), drawY);
  123. }
  124. ctx.stroke();
  125. }
  126. ctx.restore();
  127. });
  128. plot.hooks.shutdown.push(function (plot, eventHolder) {
  129. eventHolder.unbind("mouseout", onMouseOut);
  130. eventHolder.unbind("mousemove", onMouseMove);
  131. });
  132. }
  133. $.plot.plugins.push({
  134. init: init,
  135. options: options,
  136. name: 'crosshair',
  137. version: '1.0'
  138. });
  139. })(jQuery);