@fusioncharts/charts
Version:
JavaScript Data Visualisation Library
1 lines • 9.37 kB
JavaScript
"use strict";exports.__esModule=true;exports.default=void 0;var UNDEF,isWithinCircle=function isWithinCircle(x,y,cx,cy,r){return Math.pow(cx-x,2)+Math.pow(cy-y,2)<=Math.pow(r,2)},mathPI=Math.PI,mathCos=Math.cos,mathSin=Math.sin,mathMax=Math.max,mathMin=Math.min,deg2rad=mathPI/180,rad=function rad(deg){return deg%360*deg2rad},orientation=function orientation(p1x,p1y,q1x,q1y,p2x,p2y){var A=q1y-p1y,B=p2x-q1x,C=q1x-p1x,D=p2y-q1y,val1,val2,val;val1=A*B;val2=C*D;if(isNaN(val1)){val1=0}if(isNaN(val2)){val2=0}val=val1-val2;if(val===0){return 0}return val>0?1:2},onSegment=function onSegment(p1x,p1y,q1x,q1y,p2x,p2y){if(q1x<=mathMax(p1x,p2x)&&q1x>=mathMin(p1x,p2x)&&q1y<=mathMax(p1y,p2y)&&q1y>=mathMin(p1y,p2y)){return true}return false},doIntersect=function doIntersect(p1x,p1y,q1x,q1y,p2x,p2y,q2x,q2y){var o1=orientation(p1x,p1y,q1x,q1y,p2x,p2y),o2=orientation(p1x,p1y,q1x,q1y,q2x,q2y),o3=orientation(p2x,p2y,q2x,q2y,p1x,p1y),o4=orientation(p2x,p2y,q2x,q2y,q1x,q1y);if(o1!==o2&&o3!==o4){return true}if(o1===0&&onSegment(p1x,p1y,p2x,p2y,q1x,q1y)){return true}if(o2===0&&onSegment(p1x,p1y,q2x,q2y,q1x,q1y)){return true}if(o3===0&&onSegment(p2x,p2y,p1x,p1y,q2x,q2y)){return true}if(o4===0&&onSegment(p2x,p2y,q1x,q1y,q2x,q2y)){return true}return false},isWithinPolygon=function isWithinPolygon(x,y,cx,cy,r,sides,startAngle){var i,extreme=Infinity,angle,count=0,p1x,p2x,p2y,p1y,inangle,inside=false;if(isWithinCircle(x,y,cx,cy,r)&&sides>=3){angle=startAngle===UNDEF?mathPI*.5:rad(startAngle);inangle=2*mathPI/sides;p1x=cx+r*mathCos(-angle);p1y=cy+r*mathSin(-angle);for(i=0;i<sides;i++){angle+=inangle;p2x=cx+r*mathCos(-angle);p2y=cy+r*mathSin(-angle);if(doIntersect(p1x,p1y,p2x,p2y,x,y,extreme,y)){if(orientation(p1x,p1y,x,y,p2x,p2y)===0){return onSegment(p1x,p1y,x,y,p2x,p2y)}count++}p1x=p2x;p1y=p2y}inside=count%2!==0}return inside},_compair2closest=function _compair2closest(p1,p2){if(!p1||p1&&p2&&p2.i>p1.i){return p2}return p1};function kdTreeAbs(kdArray){"use strict";var arr=kdArray,tolerance=arr&&arr[0]&&arr[0].r||5,i,max=Math.max,floor=Math.floor,sqrt=Math.sqrt,min=Math.min,log=Math.log,exp=Math.exp,pow=Math.pow,_quickselectX,_quickselectY;function swap(array,swapindexi,swapindexj){var tmp=array[swapindexi];array[swapindexi]=array[swapindexj];array[swapindexj]=tmp}_quickselectX=function quickselectX(copyArr,indexk,lft,rght){var n,m,z,s,sd,newLeft,newRight,t,indexi,indexj,left=lft,right=rght;while(right>left){if(right-left>600){n=right-left+1;m=indexk-left+1;z=log(n);s=.5*exp(2*z/3);sd=.5*sqrt(z*s*(n-s)/n)*(m-n/2<0?-1:1);newLeft=max(left,floor(indexk-m*s/n+sd));newRight=min(right,floor(indexk+(n-m)*s/n+sd));_quickselectX(copyArr,indexk,newLeft,newRight)}t=copyArr[indexk];indexi=left;indexj=right;swap(copyArr,left,indexk);if(copyArr[right].x>t.x){swap(copyArr,left,right)}while(indexi<indexj){swap(copyArr,indexi,indexj);indexi++;indexj--;while(copyArr[indexi].x<t.x){indexi++}while(copyArr[indexj].x>t.x){indexj--}}if(copyArr[left].x===t.x){swap(copyArr,left,indexj)}else{indexj++;swap(copyArr,indexj,right)}if(indexj<=indexk){left=indexj+1}if(indexk<=indexj){right=indexj-1}}};_quickselectY=function quickselectY(array,indexk,lft,rght){var n,m,z,s,sd,newLeft,newRight,t,indexi,indexj,left=lft,right=rght,copyarr=array;while(right>left){if(right-left>600){n=right-left+1;m=indexk-left+1;z=log(n);s=.5*exp(2*z/3);sd=.5*sqrt(z*s*(n-s)/n)*(m-n/2<0?-1:1);newLeft=max(left,floor(indexk-m*s/n+sd));newRight=min(right,floor(indexk+(n-m)*s/n+sd));_quickselectY(copyarr,indexk,newLeft,newRight)}t=copyarr[indexk];indexi=left;indexj=right;swap(copyarr,left,indexk);if(copyarr[right].y>t.y){swap(copyarr,left,right)}while(indexi<indexj){swap(copyarr,indexi,indexj);indexi++;indexj--;while(copyarr[indexi].y<t.y){indexi++}while(copyarr[indexj].y>t.y){indexj--}}if(copyarr[left].y===t.y){swap(copyarr,left,indexj)}else{indexj++;swap(copyarr,indexj,right)}if(indexj<=indexk){left=indexj+1}if(indexk<=indexj){right=indexj-1}}};arr=arr||[];for(i=arr.length;i--;){if(arr[i].r>tolerance){tolerance=arr[i].r}arr[i].i=i;arr[i].x=+arr[i].x;arr[i].y=+arr[i].y}function buildKdTree(copyArr,left,right,isY){var ob={},mid,access=isY?"y":"x";if(left===right){ob.point=copyArr[left];return ob}if(right-left===1){if(copyArr[left][access]>copyArr[right][access]){ob.point=copyArr[left];ob.left={point:copyArr[right]}}else{ob.point=copyArr[right];ob.left={point:copyArr[left]}}return ob}mid=left+right>>1;if(isY){_quickselectY(copyArr,mid,left,right)}else{_quickselectX(copyArr,mid,left,right)}ob.point=copyArr[mid];ob.left=buildKdTree(copyArr,left,mid-1,!isY);ob.right=buildKdTree(copyArr,mid+1,right,!isY);return ob}return{tree:arr.length===0?{}:buildKdTree(arr,0,arr.length-1,false),search:function search(x,y,type,opts){var tree=this.tree,options=opts,res,x1=x-tolerance,x2=x+tolerance,y1=y-tolerance,y2=y+tolerance,lastHovered=false,lastDist=0,searchX,searchY;options=options||{};function calcDist(distx,disty,distp,distq){return sqrt(pow(distx-distp,2)+pow(disty-distq,2))}function inRange(a,b){if(type==="circle"){return calcDist(a,b,x,y)<=x1}return a>=x1&&a<=x2&&b>=y1&&b<=y2}function inRangeApply(a,r1,r2){return a>=r1&&a<=r2}function apply(ob){var currentHovered=inRangeApply(x,ob.x1,ob.x2)&&inRangeApply(y,ob.y1,ob.y2),currentDist=calcDist(x,y,ob.point.x,ob.point.y);if(!res){res=ob;lastHovered=currentHovered;lastDist=currentDist;return}if(currentHovered){if(lastHovered){if(ob.point.i>res.point.i){res=ob;lastHovered=currentHovered;lastDist=currentDist}}else{res=ob;lastHovered=currentHovered;lastDist=currentDist}}else{if(!lastHovered){if(currentDist<lastDist){res=ob;lastHovered=currentHovered;lastDist=currentDist}}}}function defineSearchArea(rx,ry){if(type==="circle"){x1=rx}else{x1=x-rx||0;x2=x+rx||0;y1=y-ry||0;y2=y+ry||0}}searchY=function searchY(ob){var rx,ry;if(!ob||!ob.point){return}rx=options.rx||ob.point.r;ry=options.ry||ob.point.r;defineSearchArea(rx,ry);if(inRange(ob.point.x,ob.point.y)){apply(ob)}if(y1<=ob.point.y){searchX(ob.left)}if(y2>=ob.point.y){searchX(ob.right)}};searchX=function searchX(ob){var rx,ry;if(!ob||!ob.point){return}rx=options.rx||ob.point.r;ry=options.ry||ob.point.r;defineSearchArea(rx,ry);if(inRange(ob.point.x,ob.point.y)){apply(ob)}if(x1<=ob.point.x){searchY(ob.left)}if(x2>=ob.point.x){searchY(ob.right)}};searchX(tree);return res&&res.point||res}}}var KdTree=function(){function KdTree(shape){var kdTreeObj=this;kdTreeObj.configure(shape)}var _proto=KdTree.prototype;_proto.configure=function configure(shape){var kdTreeObj=this;kdTreeObj.validatorFn=shape?kdTreeObj.shapeValidator():kdTreeObj.defaultValidator()};_proto.defaultValidator=function defaultValidator(){var kdTreeObj=this;return function(setElem){var mousePoint=kdTreeObj.mousePoint;return!!(setElem&&Math.pow((setElem.x-mousePoint.x)/kdTreeObj.xLimit,2)+Math.pow((setElem.y-mousePoint.y)/kdTreeObj.yLimit,2)<=1)}};_proto.shapeValidator=function shapeValidator(){var kdTreeObj=this;return function(setElem){var mousePoint=kdTreeObj.mousePoint,shapeInfo=setElem&&setElem.shapeInfo,inside=false,type,innerradius,r,x=mousePoint.x,y=mousePoint.y,sides,startAngle,x1,x2,y1,y2;type=shapeInfo&&shapeInfo.type;switch(type){case"circle":r=shapeInfo.radius;inside=isWithinCircle(x,y,setElem.x,setElem.y,r);break;case"arc":innerradius=shapeInfo.innerradius;r=shapeInfo.radius;inside=!isWithinCircle(x,y,setElem.x,setElem.y,innerradius)&&isWithinCircle(x,y,setElem.x,setElem.y,r);break;case"polygon":r=shapeInfo.radius;startAngle=shapeInfo.startAngle;sides=shapeInfo.sides;inside=isWithinPolygon(x,y,setElem.x,setElem.y,r,sides,startAngle);break;case"rect":case"rectangle":x1=setElem.x;y1=setElem.y;x2=x1+(shapeInfo.width||0);y2=y1+(shapeInfo.height||0);inside=x>=x1&&x<=x2&&y>=y1&&y<=y2;break;case"default":inside=false;break}return inside}};_proto.buildKdTree=function buildKdTree(points){this.kdTree=kdTreeAbs(points);this.tree=this.kdTree.tree;return this};_proto.getNeighbour=function getNeighbour(point,basicSearch,type){var kDTreeObj=this,tree=kDTreeObj.tree,validatorFn=kDTreeObj.validatorFn;kDTreeObj.mousePoint=point;if(basicSearch){return this.kdTree&&this.kdTree.search(point.x,point.y,type,point.options)}if(tree){return kDTreeObj._searchBtwnLimit(tree,true,validatorFn)}};_proto._searchBtwnLimit=function _searchBtwnLimit(tree,isX,validatorFn){var _tree$point=tree.point,x=_tree$point.x,y=_tree$point.y,_tree$point$shapeInfo=_tree$point.shapeInfo,width=_tree$point$shapeInfo.width,height=_tree$point$shapeInfo.height,radius=_tree$point$shapeInfo.radius,halfWidth=width!=null?width:radius,halfHeight=height!=null?height:radius;var point,returnPoint,kdTreeObj=this,axis=isX?"x":"y",newLimitBox={x1:x-halfWidth,x2:x+halfWidth,y1:y-halfHeight,y2:y+halfHeight},lowerLimit=isX?newLimitBox.x1:newLimitBox.y1,upperLimit=isX?newLimitBox.x2:newLimitBox.y2;point=tree&&tree.point&&tree.point[axis];if(point===UNDEF){return}if(validatorFn(tree.point)){returnPoint=tree.point}if(point>=lowerLimit&&tree.left){returnPoint=_compair2closest(returnPoint,kdTreeObj._searchBtwnLimit(tree.left,!isX,validatorFn))}if(point<=upperLimit&&tree.right){returnPoint=_compair2closest(returnPoint,kdTreeObj._searchBtwnLimit(tree.right,!isX,validatorFn))}return returnPoint};_proto._setSearchLimit=function _setSearchLimit(xLimit,yLimit){var kdTree=this;kdTree.xLimit=xLimit;kdTree.yLimit=yLimit};return KdTree}();KdTree.prototype.constructor=KdTree;var _default=exports.default=KdTree;