PHP/JavaScript webapp to analyse spending habits
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

jquery.flot.crosshair.js 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. Flot plugin for showing a crosshair, thin lines, when the mouse hovers
  3. over the plot.
  4. crosshair: {
  5. mode: null or "x" or "y" or "xy"
  6. color: color
  7. lineWidth: number
  8. }
  9. Set the mode to one of "x", "y" or "xy". The "x" mode enables a
  10. vertical crosshair that lets you trace the values on the x axis, "y"
  11. enables a horizontal crosshair and "xy" enables them both. "color" is
  12. the color of the crosshair (default is "rgba(170, 0, 0, 0.80)"),
  13. "lineWidth" is the width of the drawn lines (default is 1).
  14. The plugin also adds four public methods:
  15. - setCrosshair(pos)
  16. Set the position of the crosshair. Note that this is cleared if
  17. the user moves the mouse. "pos" should be on the form { x: xpos,
  18. y: ypos } (or x2 and y2 if you're using the secondary axes), which
  19. is coincidentally the same format as what you get from a "plothover"
  20. event. If "pos" is null, the crosshair is cleared.
  21. - clearCrosshair()
  22. Clear the crosshair.
  23. - lockCrosshair(pos)
  24. Cause the crosshair to lock to the current location, no longer
  25. updating if the user moves the mouse. Optionally supply a position
  26. (passed on to setCrosshair()) to move it to.
  27. Example usage:
  28. var myFlot = $.plot( $("#graph"), ..., { crosshair: { mode: "x" } } };
  29. $("#graph").bind("plothover", function (evt, position, item) {
  30. if (item) {
  31. // Lock the crosshair to the data point being hovered
  32. myFlot.lockCrosshair({ x: item.datapoint[0], y: item.datapoint[1] });
  33. }
  34. else {
  35. // Return normal crosshair operation
  36. myFlot.unlockCrosshair();
  37. }
  38. });
  39. - unlockCrosshair()
  40. Free the crosshair to move again after locking it.
  41. */
  42. (function ($) {
  43. var options = {
  44. crosshair: {
  45. mode: null, // one of null, "x", "y" or "xy",
  46. color: "rgba(170, 0, 0, 0.80)",
  47. lineWidth: 1
  48. }
  49. };
  50. function init(plot) {
  51. // position of crosshair in pixels
  52. var crosshair = { x: -1, y: -1, locked: false };
  53. plot.setCrosshair = function setCrosshair(pos) {
  54. if (!pos)
  55. crosshair.x = -1;
  56. else {
  57. var axes = plot.getAxes();
  58. crosshair.x = Math.max(0, Math.min(pos.x != null ? axes.xaxis.p2c(pos.x) : axes.x2axis.p2c(pos.x2), plot.width()));
  59. crosshair.y = Math.max(0, Math.min(pos.y != null ? axes.yaxis.p2c(pos.y) : axes.y2axis.p2c(pos.y2), plot.height()));
  60. }
  61. plot.triggerRedrawOverlay();
  62. };
  63. plot.clearCrosshair = plot.setCrosshair; // passes null for pos
  64. plot.lockCrosshair = function lockCrosshair(pos) {
  65. if (pos)
  66. plot.setCrosshair(pos);
  67. crosshair.locked = true;
  68. }
  69. plot.unlockCrosshair = function unlockCrosshair() {
  70. crosshair.locked = false;
  71. }
  72. plot.hooks.bindEvents.push(function (plot, eventHolder) {
  73. if (!plot.getOptions().crosshair.mode)
  74. return;
  75. eventHolder.mouseout(function () {
  76. if (crosshair.x != -1) {
  77. crosshair.x = -1;
  78. plot.triggerRedrawOverlay();
  79. }
  80. });
  81. eventHolder.mousemove(function (e) {
  82. if (plot.getSelection && plot.getSelection()) {
  83. crosshair.x = -1; // hide the crosshair while selecting
  84. return;
  85. }
  86. if (crosshair.locked)
  87. return;
  88. var offset = plot.offset();
  89. crosshair.x = Math.max(0, Math.min(e.pageX - offset.left, plot.width()));
  90. crosshair.y = Math.max(0, Math.min(e.pageY - offset.top, plot.height()));
  91. plot.triggerRedrawOverlay();
  92. });
  93. });
  94. plot.hooks.drawOverlay.push(function (plot, ctx) {
  95. var c = plot.getOptions().crosshair;
  96. if (!c.mode)
  97. return;
  98. var plotOffset = plot.getPlotOffset();
  99. ctx.save();
  100. ctx.translate(plotOffset.left, plotOffset.top);
  101. if (crosshair.x != -1) {
  102. ctx.strokeStyle = c.color;
  103. ctx.lineWidth = c.lineWidth;
  104. ctx.lineJoin = "round";
  105. ctx.beginPath();
  106. if (c.mode.indexOf("x") != -1) {
  107. ctx.moveTo(crosshair.x, 0);
  108. ctx.lineTo(crosshair.x, plot.height());
  109. }
  110. if (c.mode.indexOf("y") != -1) {
  111. ctx.moveTo(0, crosshair.y);
  112. ctx.lineTo(plot.width(), crosshair.y);
  113. }
  114. ctx.stroke();
  115. }
  116. ctx.restore();
  117. });
  118. }
  119. $.plot.plugins.push({
  120. init: init,
  121. options: options,
  122. name: 'crosshair',
  123. version: '1.0'
  124. });
  125. })(jQuery);