| 7 (function($){function init(plot){var selection={first:{x:-1,y:-1},second:{x:-1,y
:-1},show:false,active:false};var savedhandlers={};var mouseUpHandler=null;funct
ion onMouseMove(e){if(selection.active){updateSelection(e);plot.getPlaceholder()
.trigger("plotselecting",[getSelection()])}}function onMouseDown(e){if(e.which!=
1)return;document.body.focus();if(document.onselectstart!==undefined&&savedhandl
ers.onselectstart==null){savedhandlers.onselectstart=document.onselectstart;docu
ment.onselectstart=function(){return false}}if(document.ondrag!==undefined&&save
dhandlers.ondrag==null){savedhandlers.ondrag=document.ondrag;document.ondrag=fun
ction(){return false}}setSelectionPos(selection.first,e);selection.active=true;m
ouseUpHandler=function(e){onMouseUp(e)};$(document).one("mouseup",mouseUpHandler
)}function onMouseUp(e){mouseUpHandler=null;if(document.onselectstart!==undefine
d)document.onselectstart=savedhandlers.onselectstart;if(document.ondrag!==undefi
ned)document.ondrag=savedhandlers.ondrag;selection.active=false;updateSelection(
e);if(selectionIsSane())triggerSelectedEvent();else{plot.getPlaceholder().trigge
r("plotunselected",[]);plot.getPlaceholder().trigger("plotselecting",[null])}ret
urn false}function getSelection(){if(!selectionIsSane())return null;if(!selectio
n.show)return null;var r={},c1=selection.first,c2=selection.second;$.each(plot.g
etAxes(),function(name,axis){if(axis.used){var p1=axis.c2p(c1[axis.direction]),p
2=axis.c2p(c2[axis.direction]);r[name]={from:Math.min(p1,p2),to:Math.max(p1,p2)}
}});return r}function triggerSelectedEvent(){var r=getSelection();plot.getPlaceh
older().trigger("plotselected",[r]);if(r.xaxis&&r.yaxis)plot.getPlaceholder().tr
igger("selected",[{x1:r.xaxis.from,y1:r.yaxis.from,x2:r.xaxis.to,y2:r.yaxis.to}]
)}function clamp(min,value,max){return value<min?min:value>max?max:value}functio
n setSelectionPos(pos,e){var o=plot.getOptions();var offset=plot.getPlaceholder(
).offset();var plotOffset=plot.getPlotOffset();pos.x=clamp(0,e.pageX-offset.left
-plotOffset.left,plot.width());pos.y=clamp(0,e.pageY-offset.top-plotOffset.top,p
lot.height());if(o.selection.mode=="y")pos.x=pos==selection.first?0:plot.width()
;if(o.selection.mode=="x")pos.y=pos==selection.first?0:plot.height()}function up
dateSelection(pos){if(pos.pageX==null)return;setSelectionPos(selection.second,po
s);if(selectionIsSane()){selection.show=true;plot.triggerRedrawOverlay()}else cl
earSelection(true)}function clearSelection(preventEvent){if(selection.show){sele
ction.show=false;plot.triggerRedrawOverlay();if(!preventEvent)plot.getPlaceholde
r().trigger("plotunselected",[])}}function extractRange(ranges,coord){var axis,f
rom,to,key,axes=plot.getAxes();for(var k in axes){axis=axes[k];if(axis.direction
==coord){key=coord+axis.n+"axis";if(!ranges[key]&&axis.n==1)key=coord+"axis";if(
ranges[key]){from=ranges[key].from;to=ranges[key].to;break}}}if(!ranges[key]){ax
is=coord=="x"?plot.getXAxes()[0]:plot.getYAxes()[0];from=ranges[coord+"1"];to=ra
nges[coord+"2"]}if(from!=null&&to!=null&&from>to){var tmp=from;from=to;to=tmp}re
turn{from:from,to:to,axis:axis}}function setSelection(ranges,preventEvent){var a
xis,range,o=plot.getOptions();if(o.selection.mode=="y"){selection.first.x=0;sele
ction.second.x=plot.width()}else{range=extractRange(ranges,"x");selection.first.
x=range.axis.p2c(range.from);selection.second.x=range.axis.p2c(range.to)}if(o.se
lection.mode=="x"){selection.first.y=0;selection.second.y=plot.height()}else{ran
ge=extractRange(ranges,"y");selection.first.y=range.axis.p2c(range.from);selecti
on.second.y=range.axis.p2c(range.to)}selection.show=true;plot.triggerRedrawOverl
ay();if(!preventEvent&&selectionIsSane())triggerSelectedEvent()}function selecti
onIsSane(){var minSize=plot.getOptions().selection.minSize;return Math.abs(selec
tion.second.x-selection.first.x)>=minSize&&Math.abs(selection.second.y-selection
.first.y)>=minSize}plot.clearSelection=clearSelection;plot.setSelection=setSelec
tion;plot.getSelection=getSelection;plot.hooks.bindEvents.push(function(plot,eve
ntHolder){var o=plot.getOptions();if(o.selection.mode!=null){eventHolder.mousemo
ve(onMouseMove);eventHolder.mousedown(onMouseDown)}});plot.hooks.drawOverlay.pus
h(function(plot,ctx){if(selection.show&&selectionIsSane()){var plotOffset=plot.g
etPlotOffset();var o=plot.getOptions();ctx.save();ctx.translate(plotOffset.left,
plotOffset.top);var c=$.color.parse(o.selection.color);ctx.strokeStyle=c.scale("
a",.8).toString();ctx.lineWidth=1;ctx.lineJoin=o.selection.shape;ctx.fillStyle=c
.scale("a",.4).toString();var x=Math.min(selection.first.x,selection.second.x)+.
5,y=Math.min(selection.first.y,selection.second.y)+.5,w=Math.abs(selection.secon
d.x-selection.first.x)-1,h=Math.abs(selection.second.y-selection.first.y)-1;ctx.
fillRect(x,y,w,h);ctx.strokeRect(x,y,w,h);ctx.restore()}});plot.hooks.shutdown.p
ush(function(plot,eventHolder){eventHolder.unbind("mousemove",onMouseMove);event
Holder.unbind("mousedown",onMouseDown);if(mouseUpHandler)$(document).unbind("mou
seup",mouseUpHandler)})}$.plot.plugins.push({init:init,options:{selection:{mode:
null,color:"#e8cfac",shape:"round",minSize:5}},name:"selection",version:"1.1"})}
)(jQuery); |