zingchart
Version:
Our JavaScript charting library is a commercial product. But the full branded version is free to try, forever.
8 lines (7 loc) • 9.49 kB
JavaScript
/*
All of the code within the ZingChart software is developed and copyrighted by ZingChart, Inc., and may not be copied,
replicated, or used in any other software or application without prior permission from ZingChart. All usage must coincide with the
ZingChart End User License Agreement which can be requested by email at support@zingchart.com.
Build 2.9.16-hf1
*/
zingchart.setModule("selection-tool"),zingchart.plugins.selectionTool=zingchart.plugins.selectionTool||{},zingchart.plugins.selectionTool.freeFormType={},zingchart.plugins.selectionTool.filter={text:"",nodes:[]},zingchart.plugins.selectionTool.charts=zingchart.plugins.selectionTool.charts||{},zingchart.plugins.selectionTool.angle2D=function(e,i,t,o){var n,l;for(l=Math.atan2(i,e),n=Math.atan2(o,t)-l;n>Math.PI;)n-=2*Math.PI;for(;n<-Math.PI;)n+=2*Math.PI;return n},zingchart.plugins.selectionTool.isInsidePolygon=function(e,i,t){for(var o=0,n=[],l=[],a=e.length,c=0;c<a;c++)n[0]=e[c][0]-i,n[1]=e[c][1]-t,l[0]=e[(c+1)%a][0]-i,l[1]=e[(c+1)%a][1]-t,o+=zingchart.plugins.selectionTool.angle2D(n[0],n[1],l[0],l[1]);return Math.abs(o)>=Math.PI},zingchart.plugins.selectionTool.intersectRect=function(e,i){return!(i.iX>e.iX+e.I||i.iX+i.I<e.iX||i.iY>e.iY+e.F||i.iY+i.F<e.iY)},zingchart.plugins.selectionTool.checkAgainstFilters=function(MX){var bValidNode=!0,sFilterText=zingchart.plugins.selectionTool.filter.text;if(""!==sFilterText){bValidNode=!1,sFilterText=sFilterText.replace(/%plot-index/gi,MX.plotindex).replace(/%node-index/gi,MX.index).replace(/%node-keyvalue/gi,MX.keyvalue).replace(/%node-value/gi,MX.value);try{bValidNode=eval(sFilterText)}catch(e){bValidNode=!1}}if(!bValidNode){var aFilterNodes=zingchart.plugins.selectionTool.filter.nodes;if(aFilterNodes.length){bValidNode=!1;for(var i=0;i<aFilterNodes.length;i++)if(null!==ZC._n_(aFilterNodes[i].plotindex)&&aFilterNodes[i].plotindex===MX.plotindex&&(null!==ZC._n_(aFilterNodes[i].index)&&aFilterNodes[i].index===MX.index||null!==ZC._n_(aFilterNodes[i].keyvalue)&&aFilterNodes[i].keyvalue===MX.keyvalue)){bValidNode=!0;break}}}return bValidNode},function(){var e=-1,i=-1,t=[],o="",n=null,l=!1,a=!1,c=function(n){zingchart.exec(n.id,"removeobject",{type:"shape",id:n.id+"-selection-poly"}),zingchart.exec(n.id,"removeobject",{type:"label",id:n.id+"-selection-rectangle"}),t=[],e=-1,i=-1,o=""},r=function(e,i){if(l){var t=zingchart.getLoader(e.id),o=t.MF||t.o,n=o.graphset?o.graphset[0]:o,a={};n["selection-tool"]&&n["selection-tool"].cursor&&(a=n["selection-tool"].cursor,ZC._todash_(a));var c={id:e.id+"-selection-cursor",type:"plus",x:e.x,y:e.y,"line-width":1,"line-color":"#cc0000",size:5,alpha:e.ev.shiftKey||e.ev.metaKey||i?1:0,flat:!0};a.label&&(c.label={anchor:"bl",alpha:e.ev.shiftKey||e.ev.metaKey||i?1:0,text:"Click to add points"}),ZC._cp_(a,c),zingchart.exec(e.id,"addobject",{type:"shape",data:c})}},s=function(e,i){l&&zingchart.exec(e.id,"updateobject",{type:"shape",id:e.id+"-selection-cursor",data:{x:e.x,y:e.y,alpha:e.ev.shiftKey||e.ev.metaKey||i?1:0,label:{alpha:e.ev.shiftKey||e.ev.metaKey||i?1:0}}})},d=function(e){a=!1,zingchart.exec(e.id,"removeobject",{type:"shape",id:e.id+"-selection-cursor"})},h=function(n){if(""!==o){var l,a,r,s,d,h,g=zingchart.getLoader(n.id),p=[],u=[],f=[],y=null,x=zingchart.exec(n.id,"getplotlength",{graphid:n.graphid});if(o="",n.ev.shiftKey||n.ev.metaKey||16===n.ev.keyCode){for(t.length>=2&&t.push([n.x,n.y]),l=0;l<x;l++)for(r=zingchart.exec(n.id,"getobjectinfo",{graphid:n.graphid,object:"plot",plotindex:l}),f[l]=r.xdata["append-selection"],d=zingchart.exec(n.id,"getnodelength",{graphid:n.graphid,plotindex:l}),a=0;a<d;a++)s=zingchart.exec(n.id,"getobjectinfo",{graphid:n.graphid,object:"node",plotindex:l,nodeindex:a}),null===ZC._n_(s)||s.xdata["ignore-selection"]||(zingchart.plugins.selectionTool.isInsidePolygon(t,s.x,s.y)||zingchart.plugins.selectionTool.isInsidePolygon(t,s.x+s.width,s.y)||zingchart.plugins.selectionTool.isInsidePolygon(t,s.x+s.width,s.y+s.height)||zingchart.plugins.selectionTool.isInsidePolygon(t,s.x,s.y+s.height))&&zingchart.plugins.selectionTool.checkAgainstFilters(s)&&(u.push(s),null===ZC._n_(p[l])&&(p[l]=[]),p[l].push(a));(y=g.FI()).poly=t,ZC.AO.C0("zingchart.plugins.selection-tool.mouseup",g,y)}else if(-1!==e&&-1!==i){var b,z,v,m,T=Math.min(e,n.x),C=Math.max(e,n.x),F=Math.min(i,n.y),M=Math.max(i,n.y);if(C-T<4||M-F<4)return;for(t=[[T,F],[T,M],[C,M],[C,F],[T,F]],l=0;l<x;l++)for(r=zingchart.exec(n.id,"getobjectinfo",{graphid:n.graphid,object:"plot",plotindex:l}),f[l]=r.xdata["append-selection"],h=!1,-1!==ZC.AV(["vbar","hbar","vbar3d","hbar3d"],r.type)&&(h=!0),d=zingchart.exec(n.id,"getnodelength",{graphid:n.graphid,plotindex:l}),a=0;a<d;a++)s=zingchart.exec(n.id,"getobjectinfo",{graphid:n.graphid,object:"node",plotindex:l,nodeindex:a}),null!==ZC._n_(s)&&!s.xdata["ignore-selection"]&&s.onviewport&&(b=s.x,z=s.y,v=s.width,m=s.height,s.bounds&&(b=s.bounds[0],z=s.bounds[1],v=s.bounds[2]-s.bounds[0],m=s.bounds[3]-s.bounds[1]),(h&&zingchart.plugins.selectionTool.intersectRect({iX:T,iY:F,I:C-T,F:M-F},{iX:b,iY:z,I:v,F:m})||!h&&zingchart.plugins.selectionTool.isInsidePolygon(t,s.x,s.y))&&zingchart.plugins.selectionTool.checkAgainstFilters(s)&&(u.push(s),null===ZC._n_(p[l])&&(p[l]=[]),p[l].push(a)));(y=g.FI()).poly=t,ZC.AO.C0("zingchart.plugins.selection-tool.mouseup",g,y)}if(window.setTimeout(function(){c(n)},64),p.length>0){ZC.mobile&&(ZC.move=!1);var Z=zingchart.exec(n.id,"getselection",{graphid:n.graphid});for(l=0;l<Z.length;l++)if(f[l]&&Z[l])for(p[l]||(p[l]=[]),a=0;a<Z[l].length;a++)-1===ZC.AV(p[l],Z[l][a])&&p[l].push(Z[l][a]);window.setTimeout(function(){n.selection=p,n.nodes=u;var e=ZC.AO.C0("zingchart.plugins.selection-tool.selection",g,n,!0);(null===ZC._n_(e)||ZC._b_(e))&&zingchart.exec(n.id,"setselection",{graphid:n.graphid,selection:p})},1)}}};zingchart.bind(null,"destroy",function(e){zingchart.plugins.selectionTool.charts[e.id]&&(ZC.A4(document).unbind("keyup",g),ZC.A4(document).unbind("keydown",p),zingchart.plugins.selectionTool.charts[e.id]=null)});var g=function(e){16===e.keyCode&&null!==n&&(d(n),t.length>0&&(h(n),n=null))};ZC.A4(document).bind("keyup",g);var p=function(e){if(16===e.keyCode&&0===t.length&&null!==n){var i=zingchart.getLoader(n.id),o=zingchart.getModules(i);if(-1===ZC.AV(o,"selection-tool"))return;var l=i.MF||i.o,c=l.graphset?l.graphset[0]:l;zingchart.plugins.selectionTool.freeFormType[n.id]="lasso",c["selection-tool"]&&c["selection-tool"]["free-form-type"]&&(zingchart.plugins.selectionTool.freeFormType[n.id]=c["selection-tool"]["free-form-type"]),a?s(n,!0):r(n,!0)}};ZC.A4(document).bind("keydown",p),zingchart.bind(null,"dblclick",function(e){t.length>0&&null!==e&&(h(e),d(e),n=null)}),zingchart.bind(null,"mousedown",function(l){if(n=l,-1===l.targetid.indexOf(l.graphid+"-preview")&&-1===l.targetid.indexOf(l.graphid+"-scroll")){var a=zingchart.getLoader(l.id),c=a.MF||a.o,r=c.graphset?c.graphset[0]:c,s=zingchart.getModules(a);if(-1!==ZC.AV(s,"selection-tool")){zingchart.plugins.selectionTool.charts[l.id]=!0,ZC.mobile||l.ev.preventDefault();var d=ZC.AO.C0("zingchart.plugins.selection-tool.beforeselection",a,l,!0);if(void 0===d||d){zingchart.plugins.selectionTool.freeFormType[n.id]||(zingchart.plugins.selectionTool.freeFormType[n.id]="lasso",r["selection-tool"]&&r["selection-tool"]["free-form-type"]&&(zingchart.plugins.selectionTool.freeFormType[n.id]=r["selection-tool"]["free-form-type"]));var h,g={};if(r["selection-tool"]&&r["selection-tool"].mask&&(g=r["selection-tool"].mask,ZC._todash_(g)),"none"===l.target)if(l.ev.shiftKey||l.ev.metaKey)if(t.push([l.x,l.y]),h={id:l.id+"-selection-poly",type:"poly",points:t,"border-width":1,"border-color":"#000000","background-color":"#ff9900",alpha:.2,flat:!0},ZC._cp_(g,h),"lasso"===zingchart.plugins.selectionTool.freeFormType[l.id])zingchart.exec(l.id,"addobject",{type:"shape",data:h});else if(1===t.length)zingchart.exec(l.id,"addobject",{type:"shape",data:h}),zingchart.exec(l.id,"updateobject",{type:"shape",id:l.id+"-selection-cursor",data:{label:{visible:!1}}});else{var p=[].concat(t);p[p.length]=[p[0][0],p[0][1]],zingchart.exec(l.id,"updateobject",{type:"shape",id:l.id+"-selection-poly",data:{points:p}})}else-1===e&&-1===i&&(e=l.x,i=l.y,h={id:l.id+"-selection-rectangle",x:e,y:i,width:4,height:4,"border-width":1,"border-color":"#000000","background-color":"#ff9900",alpha:.2,flat:!0},ZC._cp_(g,h),zingchart.exec(l.id,"addobject",{type:"label",data:h}));o=l.id}}}}),zingchart.bind(null,"mousemove",function(d){var h,g=zingchart.getLoader(d.id),p=zingchart.getModules(g);-1!==ZC.AV(p,"selection-tool")&&(n=d,l=!0,d.id===o?(a?s(d):(a=!0,r(d)),ZC.mobile||d.ev.preventDefault(),d.ev.shiftKey||d.ev.metaKey?"lasso"===zingchart.plugins.selectionTool.freeFormType[d.id]?t.length>0&&(t.push([d.x,d.y]),(h=[].concat(t))[h.length]=[h[0][0],h[0][1]],zingchart.exec(d.id,"updateobject",{type:"shape",id:d.id+"-selection-poly",data:{points:h}})):t.length>0&&((h=[].concat(t)).push([d.x,d.y]),h[h.length]=[h[0][0],h[0][1]],zingchart.exec(d.id,"updateobject",{type:"shape",id:d.id+"-selection-poly",data:{points:h}})):-1!==e&&-1!==i&&zingchart.exec(d.id,"updateobject",{type:"label",id:d.id+"-selection-rectangle",data:{x:Math.min(d.x,e),y:Math.min(d.y,i),width:Math.max(4,Math.abs(d.x-e)),height:Math.max(4,Math.abs(d.y-i))}})):""!==o&&c(d))}),zingchart.bind(null,"mouseout",function(e){l=!1,c(e),d(e)}),zingchart.bind(null,"mouseup",function(e){e.ev.shiftKey||e.ev.metaKey?"lasso"===zingchart.plugins.selectionTool.freeFormType[e.id]&&h(e):h(e)})}();