1 /* Flot plugin for showing crosshairs when the mouse hovers over the plot. 2 3 Copyright (c) 2007-2013 IOLA and Ole Laursen. 4 Licensed under the MIT license. 5 6 The plugin supports these options: 7 8 crosshair: { 9 mode: null or "x" or "y" or "xy" 10 color: color 11 lineWidth: number 12 } 13 14 Set the mode to one of "x", "y" or "xy". The "x" mode enables a vertical 15 crosshair that lets you trace the values on the x axis, "y" enables a 16 horizontal crosshair and "xy" enables them both. "color" is the color of the 17 crosshair (default is "rgba(170, 0, 0, 0.80)"), "lineWidth" is the width of 18 the drawn lines (default is 1). 19 20 The plugin also adds four public methods: 21 22 - setCrosshair( pos ) 23 24 Set the position of the crosshair. Note that this is cleared if the user 25 moves the mouse. "pos" is in coordinates of the plot and should be on the 26 form { x: xpos, y: ypos } (you can use x2/x3/... if you're using multiple 27 axes), which is coincidentally the same format as what you get from a 28 "plothover" event. If "pos" is null, the crosshair is cleared. 29 30 - clearCrosshair() 31 32 Clear the crosshair. 33 34 - lockCrosshair(pos) 35 36 Cause the crosshair to lock to the current location, no longer updating if 37 the user moves the mouse. Optionally supply a position (passed on to 38 setCrosshair()) to move it to. 39 40 Example usage: 41 42 var myFlot = $.plot( $("#graph"), ..., { crosshair: { mode: "x" } } }; 43 $("#graph").bind( "plothover", function ( evt, position, item ) { 44 if ( item ) { 45 // Lock the crosshair to the data point being hovered 46 myFlot.lockCrosshair({ 47 x: item.datapoint[ 0 ], 48 y: item.datapoint[ 1 ] 49 }); 50 } else { 51 // Return normal crosshair operation 52 myFlot.unlockCrosshair(); 53 } 54 }); 55 56 - unlockCrosshair() 57 58 Free the crosshair to move again after locking it. 59 */(function(e){function n(e){function n(n){if(t.locked)return;t.x!=-1&&(t.x=-1,e.triggerRedrawOverlay())}function r(n){if(t.locked)return;if(e.getSelection&&e.getSelection()){t.x=-1;return}var r=e.offset();t.x=Math.max(0,Math.min(n.pageX-r.left,e.width())),t.y=Math.max(0,Math.min(n.pageY-r.top,e.height())),e.triggerRedrawOverlay()}var t={x:-1,y:-1,locked:!1};e.setCrosshair=function(r){if(!r)t.x=-1;else{var i=e.p2c(r);t.x=Math.max(0,Math.min(i.left,e.width())),t.y=Math.max(0,Math.min(i.top,e.height()))}e.triggerRedrawOverlay()},e.clearCrosshair=e.setCrosshair,e.lockCrosshair=function(r){r&&e.setCrosshair(r),t.locked=!0},e.unlockCrosshair=function(){t.locked=!1},e.hooks.bindEvents.push(function(e,t){if(!e.getOptions().crosshair.mode)return;t.mouseout(n),t.mousemove(r)}),e.hooks.drawOverlay.push(function(e,n){var r=e.getOptions().crosshair;if(!r.mode)return;var i=e.getPlotOffset();n.save(),n.translate(i.left,i.top);if(t.x!=-1){var s=e.getOptions().crosshair.lineWidth%2===0?0:.5;n.strokeStyle=r.color,n.lineWidth=r.lineWidth,n.lineJoin="round",n.beginPath();if(r.mode.indexOf("x")!=-1){var o=Math.round(t.x)+s;n.moveTo(o,0),n.lineTo(o,e.height())}if(r.mode.indexOf("y")!=-1){var u=Math.round(t.y)+s;n.moveTo(0,u),n.lineTo(e.width(),u)}n.stroke()}n.restore()}),e.hooks.shutdown.push(function(e,t){t.unbind("mouseout",n),t.unbind("mousemove",r)})}var t={crosshair:{mode:null,color:"rgba(170, 0, 0, 0.80)",lineWidth:1}};e.plot.plugins.push({init:n,options:t,name:"crosshair",version:"1.0"})})(jQuery);