1 line
3.4 KiB
JavaScript
1 line
3.4 KiB
JavaScript
!function(m){m.plot.plugins.push({init:function(a){var d={first:{x:-1,y:-1},second:{x:-1,y:-1},show:!1,active:!1},o={},n=null;function i(e){d.active&&(u(e),a.getPlaceholder().trigger("plotselecting",[t()]))}function s(e){1==e.which&&(document.body.focus(),void 0!==document.onselectstart&&null==o.onselectstart&&(o.onselectstart=document.onselectstart,document.onselectstart=function(){return!1}),void 0!==document.ondrag&&null==o.ondrag&&(o.ondrag=document.ondrag,document.ondrag=function(){return!1}),l(d.first,e),d.active=!0,n=function(e){var t;t=e,n=null,void 0!==document.onselectstart&&(document.onselectstart=o.onselectstart),void 0!==document.ondrag&&(document.ondrag=o.ondrag),d.active=!1,u(t),h()?r():(a.getPlaceholder().trigger("plotunselected",[]),a.getPlaceholder().trigger("plotselecting",[null]))},m(document).one("mouseup",n))}function t(){if(!h())return null;if(!d.show)return null;var i={},s=d.first,r=d.second;return m.each(a.getAxes(),function(e,t){if(t.used){var o=t.c2p(s[t.direction]),n=t.c2p(r[t.direction]);i[e]={from:Math.min(o,n),to:Math.max(o,n)}}}),i}function r(){var e=t();a.getPlaceholder().trigger("plotselected",[e]),e.xaxis&&e.yaxis&&a.getPlaceholder().trigger("selected",[{x1:e.xaxis.from,y1:e.yaxis.from,x2:e.xaxis.to,y2:e.yaxis.to}])}function c(e,t,o){return t<e?e:o<t?o:t}function l(e,t){var o=a.getOptions(),n=a.getPlaceholder().offset(),i=a.getPlotOffset();e.x=c(0,t.pageX-n.left-i.left,a.width()),e.y=c(0,t.pageY-n.top-i.top,a.height()),"y"==o.selection.mode&&(e.x=e==d.first?0:a.width()),"x"==o.selection.mode&&(e.y=e==d.first?0:a.height())}function u(e){null!=e.pageX&&(l(d.second,e),h()?(d.show=!0,a.triggerRedrawOverlay()):f(!0))}function f(e){d.show&&(d.show=!1,a.triggerRedrawOverlay(),e||a.getPlaceholder().trigger("plotunselected",[]))}function g(e,t){var o,n,i,s,r=a.getAxes();for(var c in r)if((o=r[c]).direction==t&&(e[s=t+o.n+"axis"]||1!=o.n||(s=t+"axis"),e[s])){n=e[s].from,i=e[s].to;break}if(e[s]||(o="x"==t?a.getXAxes()[0]:a.getYAxes()[0],n=e[t+"1"],i=e[t+"2"]),null!=n&&null!=i&&i<n){var l=n;n=i,i=l}return{from:n,to:i,axis:o}}function h(){var e=a.getOptions().selection.minSize;return Math.abs(d.second.x-d.first.x)>=e&&Math.abs(d.second.y-d.first.y)>=e}a.clearSelection=f,a.setSelection=function(e,t){var o,n=a.getOptions();"y"==n.selection.mode?(d.first.x=0,d.second.x=a.width()):(o=g(e,"x"),d.first.x=o.axis.p2c(o.from),d.second.x=o.axis.p2c(o.to)),"x"==n.selection.mode?(d.first.y=0,d.second.y=a.height()):(o=g(e,"y"),d.first.y=o.axis.p2c(o.from),d.second.y=o.axis.p2c(o.to)),d.show=!0,a.triggerRedrawOverlay(),!t&&h()&&r()},a.getSelection=t,a.hooks.bindEvents.push(function(e,t){null!=e.getOptions().selection.mode&&(t.mousemove(i),t.mousedown(s))}),a.hooks.drawOverlay.push(function(e,t){if(d.show&&h()){var o=e.getPlotOffset(),n=e.getOptions();t.save(),t.translate(o.left,o.top);var i=m.color.parse(n.selection.color);t.strokeStyle=i.scale("a",.8).toString(),t.lineWidth=1,t.lineJoin=n.selection.shape,t.fillStyle=i.scale("a",.4).toString();var s=Math.min(d.first.x,d.second.x)+.5,r=Math.min(d.first.y,d.second.y)+.5,c=Math.abs(d.second.x-d.first.x)-1,l=Math.abs(d.second.y-d.first.y)-1;t.fillRect(s,r,c,l),t.strokeRect(s,r,c,l),t.restore()}}),a.hooks.shutdown.push(function(e,t){t.unbind("mousemove",i),t.unbind("mousedown",s),n&&m(document).unbind("mouseup",n)})},options:{selection:{mode:null,color:"#e8cfac",shape:"round",minSize:5}},name:"selection",version:"1.1"})}(jQuery); |