UNPKG

14.9 kBJavaScriptView Raw
1!function(t,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n((t="undefined"!=typeof globalThis?globalThis:t||self).Coordinate={})}(this,function(t){"use strict";function g(t,n){for(var r=0,e=n.length,o=t.length;r<e;r++,o++)t[o]=n[r];return t}function e(t,n){return r.call(t)==="[object "+n+"]"}var r={}.toString,u=function(t){return Array.isArray?Array.isArray(t):e(t,"Array")},s=function(t){if("object"!=typeof(n=t)||null===n||!e(t,"Object"))return!1;var n;if(null===Object.getPrototypeOf(t))return!0;for(var r=t;null!==Object.getPrototypeOf(r);)r=Object.getPrototypeOf(r);return Object.getPrototypeOf(t)===r},h=5;function n(t){for(var n=[],r=1;r<arguments.length;r++)n[r-1]=arguments[r];for(var e=0;e<n.length;e+=1)!function t(n,r,e,o){for(var a in e=e||0,o=o||h,r){var i;r.hasOwnProperty(a)&&(null!==(i=r[a])&&s(i)?(s(n[a])||(n[a]={}),e<o?t(n[a],i,e+1,o):n[a]=r[a]):u(i)?(n[a]=[],n[a]=n[a].concat(i)):void 0!==i&&(n[a]=i))}}(t,n[e]);return t}function m(t){return t}var v="undefined"!=typeof Float32Array?Float32Array:Array;function M(){var t=new v(9);return v!=Float32Array&&(t[1]=0,t[2]=0,t[3]=0,t[5]=0,t[6]=0,t[7]=0),t[0]=1,t[4]=1,t[8]=1,t}Math.hypot||(Math.hypot=function(){for(var t=0,n=arguments.length;n--;)t+=arguments[n]*arguments[n];return Math.sqrt(t)});var o,y=function(t,n,r){var e=n[0],o=n[1],a=n[2],i=n[3],u=n[4],s=n[5],h=n[6],f=n[7],c=n[8],p=r[0],d=r[1],l=r[2],g=r[3],m=r[4],v=r[5],M=r[6],n=r[7],r=r[8];return t[0]=p*e+d*i+l*h,t[1]=p*o+d*u+l*f,t[2]=p*a+d*s+l*c,t[3]=g*e+m*i+v*h,t[4]=g*o+m*u+v*f,t[5]=g*a+m*s+v*c,t[6]=M*e+n*i+r*h,t[7]=M*o+n*u+r*f,t[8]=M*a+n*s+r*c,t};function l(t,n,r){for(;t<n;)t+=2*Math.PI;for(;r<t;)t-=2*Math.PI;return t}o=new v(3),v!=Float32Array&&(o[0]=0,o[1]=0,o[2]=0);function a(t,n,r,e,o){var a=t[0],i=t[1],t=M();return i=[a,i],(t=t)[0]=1,t[1]=0,t[2]=0,t[3]=0,t[4]=1,t[5]=0,t[6]=i[0],t[7]=i[1],t[8]=1,t}class i{constructor(t){this.options=n({},this.getDefaultOptions()),this.update(t)}getOptions(){return this.options}update(t={}){this.options=n({},this.options,t),this.rescale(t)}rescale(t){}}function f(n,r){for(let t=0;t<r.length;t+=1)n.has(r[t])||n.set(r[t],t)}function c(t){const{value:n,from:r,to:e,mapper:o,notFoundReturn:a}=t;let i=o.get(n);if(void 0===i){if(a)return a;i=r.push(n)-1,o.set(n,i)}return e[i%e.length]}class p extends i{constructor(t){super(t)}getDefaultOptions(){return{domain:[],range:[]}}map(t){return 0===this.domainIndexMap.size&&f(this.domainIndexMap,this.getDomain()),c({value:t,mapper:this.domainIndexMap,from:this.getDomain(),to:this.getRange(),notFoundReturn:this.options.unknown})}invert(t){return 0===this.rangeIndexMap.size&&f(this.rangeIndexMap,this.getRange()),c({value:t,mapper:this.rangeIndexMap,from:this.getRange(),to:this.getDomain(),notFoundReturn:this.options.unknown})}rescale(t){if(!this.rangeIndexMap)return this.rangeIndexMap=new Map,void(this.domainIndexMap=new Map);t&&!t.range||this.rangeIndexMap.clear(),t&&!t.domain&&!t.compare||(this.domainIndexMap.clear(),this.sortedDomain=void 0)}clone(){return new p(this.options)}getRange(){return this.options.range}getDomain(){if(this.sortedDomain)return this.sortedDomain;var{domain:t,compare:n}=this.options;return this.sortedDomain=n?[...t].sort(n):t,this.sortedDomain}}function d(t){var n={range:[0,1],align:.5,round:!1,paddingInner:0,paddingOuter:0,...t},{range:r,stepAmount:e,paddingOuter:o,paddingInner:a}=n;let i,u,s=r[0];t=r[1],r=t-s;i=r/Math.max(1,2*o+(e-a)),n.round&&(i=Math.floor(i)),s+=(r-i*(e-a))*n.align,u=i*(1-a),n.round&&(s=Math.round(s),u=Math.round(u));t=function(n,t,r){var t=(t-n)/r,e=Number.isInteger(t)?t:Math.floor(t)+1;if(e<=0)return[];const o=new Array(e-1);for(let t=0;t<e;t+=1)o[t]=n+t*r;return o}(s,t,i);return{step:i,adjustedRange:t,bandWidth:u}}class w extends p{constructor(t){super(t)}getDefaultOptions(){return{domain:[],range:[0,1],align:.5,round:!1,paddingInner:0,paddingOuter:0,padding:0,unknown:void 0}}clone(){return new w(this.options)}getStep(){return this.step}getBandWidth(){return this.bandWidth}getRange(){return this.adjustedRange}getPaddingInner(){var{padding:t,paddingInner:n}=this.options;return 0<t?t:n}getPaddingOuter(){var{padding:t,paddingOuter:n}=this.options;return 0<t?t:n}rescale(){super.rescale();var{align:t,domain:n,range:r,round:e}=this.options,{step:r,bandWidth:e,adjustedRange:n}=d({align:t,range:r,round:e,paddingInner:this.getPaddingInner(),paddingOuter:this.getPaddingOuter(),stepAmount:n.length});this.step=r,this.bandWidth=e,this.adjustedRange=n}}function x(t,...n){return n.reduce((n,r)=>t=>n(r(t)),t)}function O(n,r){return r-n?t=>(t-n)/(r-n):t=>.5}const I=(n,r)=>t=>n*(1-t)+r*t,b=(n,r)=>t=>Math.round(I(n,r)(t));const A=Math.sqrt(50),D=Math.sqrt(10),P=Math.sqrt(2);function k(t,n,r){t=(n-t)/Math.max(0,r),r=Math.floor(Math.log(t)/Math.LN10),t/=10**r;return 0<=r?(t>=A?10:t>=D?5:t>=P?2:1)*10**r:-(10**-r)/(t>=A?10:t>=D?5:t>=P?2:1)}const j=(t,n,r=5)=>{const e=[t,n];let o=0,a=e.length-1,i=e[o],u=e[a],s;return u<i&&([i,u]=[u,i],[o,a]=[a,o]),s=k(i,u,r),0<s?(i=Math.floor(i/s)*s,u=Math.ceil(u/s)*s,s=k(i,u,r)):s<0&&(i=Math.ceil(i*s)/s,u=Math.floor(u*s)/s,s=k(i,u,r)),0<s?(e[o]=Math.floor(i/s)*s,e[a]=Math.ceil(u/s)*s):s<0&&(e[o]=Math.ceil(i*s)/s,e[a]=Math.floor(u*s)/s),e};function N(t){return null!=t&&!Number.isNaN(t)}const T=(t,n,r)=>{let e,o,a=t,i=n;if(a===i&&0<r)return[a];let u=k(a,i,r);if(0===u||!Number.isFinite(u))return[];if(0<u){a=Math.ceil(a/u),i=Math.floor(i/u),o=new Array(e=Math.ceil(i-a+1));for(let t=0;t<e;t+=1)o[t]=(a+t)*u}else{u=-u,a=Math.ceil(a*u),i=Math.floor(i*u),o=new Array(e=Math.ceil(i-a+1));for(let t=0;t<e;t+=1)o[t]=(a+t)/u}return o},F=(t,n,r)=>{var[e,o]=t,[t,n]=n;let a,i;return i=e<o?(a=O(e,o),r(t,n)):(a=O(o,e),r(n,t)),x(i,a)},R=(e,t,n)=>{const o=Math.min(e.length,t.length)-1,a=new Array(o),i=new Array(o);var r=e[0]>e[o],u=r?[...e].reverse():e,s=r?[...t].reverse():t;for(let t=0;t<o;t+=1)a[t]=O(u[t],u[t+1]),i[t]=n(s[t],s[t+1]);return t=>{var n=function(t,n,r,e,o){let a=r||0,i=e||t.length;const u=o||(t=>t);for(;a<i;){var s=Math.floor((a+i)/2);u(t[s])>n?i=s:a=s+1}return a}(e,t,1,o)-1,r=a[n];return x(i[n],r)(t)}},C=(t,n,r,e)=>{const o=2<Math.min(t.length,n.length)?R:F;r=e?b:r;return o(t,n,r)};class q extends i{getDefaultOptions(){return{domain:[0,1],range:[0,1],nice:!1,clamp:!1,round:!1,interpolate:I,tickCount:5}}map(t){return N(t)?this.output(t):this.options.unknown}invert(t){return N(t)?this.input(t):this.options.unknown}nice(){var t,n,r,e;this.options.nice&&([t,n,r,...e]=this.getTickMethodOptions(),this.options.domain=this.chooseNice()(t,n,r,...e))}getTicks(){const{tickMethod:t}=this.options;var[n,r,e,...o]=this.getTickMethodOptions();return t(n,r,e,...o)}getTickMethodOptions(){var{domain:t,tickCount:n}=this.options;return[t[0],t[t.length-1],n]}chooseNice(){return j}rescale(){this.nice();var[t,n]=this.chooseTransforms();this.composeOutput(t,this.chooseClamp(t)),this.composeInput(t,n,this.chooseClamp(n))}chooseClamp(t){var{clamp:n,range:r}=this.options,t=this.options.domain.map(t),r=Math.min(t.length,r.length);return n?function(t,n){const r=n<t?n:t,e=n<t?t:n;return t=>Math.min(Math.max(r,t),e)}(t[0],t[r-1]):m}composeOutput(t,n){const{domain:r,range:e,round:o,interpolate:a}=this.options;var i=C(r.map(t),e,a,o);this.output=x(i,n,t)}composeInput(t,n,r){const{domain:e,range:o,interpolate:a}=this.options;t=C(o,e.map(t),a);this.input=x(n,r,t)}}class S extends q{getDefaultOptions(){return{domain:[0,1],range:[0,1],unknown:void 0,nice:!1,clamp:!1,round:!1,interpolate:I,tickMethod:T,tickCount:5}}chooseTransforms(){return[m,m]}clone(){return new S(this.options)}}function W(t,n){for(var r=[],e=0,o=t.length;e<o;e++)r.push(t[e].substr(0,n));return r}var z=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"],J=["January","February","March","April","May","June","July","August","September","October","November","December"],_=W(J,3);!function(t){for(var n=[],r=1;r<arguments.length;r++)n[r-1]=arguments[r];for(var e=0,o=n;e<o.length;e++){var a,i=o[e];for(a in i)t[a]=i[a]}}({},{dayNamesShort:W(z,3),dayNames:z,monthNamesShort:_,monthNames:J,amPm:["am","pm"],DoFn:function(t){return t+["th","st","nd","rd"][3<t%10?0:(t-t%10!=10?1:0)*t%10]}});function B(t,n,r,e,o){var a=new S({range:[n,n+e]}),i=new S({range:[r,r+o]});return{transform:function(t){var n=t[0],t=t[1];return[a.map(n),i.map(t)]},untransform:function(t){var n=t[0],t=t[1];return[a.invert(n),i.invert(t)]}}}function E(t,n,r,e,o){return(0,t[0])(n,r,e,o)}function L(t,n,r,e,o){return t[0]}function G(t,n,r,e,o){var a=t[0],i=t[1],u=t[2],t=t[3],s=new S({range:[u,t]}),h=new S({range:[a,i]}),f=1<(e=o/e)?1:e,c=1<e?1/e:1;return{transform:function(t){var n=t[0],t=t[1],n=h.map(n),t=s.map(t);return[.5*(t*Math.cos(n)*f)+.5,.5*(t*Math.sin(n)*c)+.5]},untransform:function(t){var n=t[0],r=t[1],t=2*(n-.5)/f,n=2*(r-.5)/c,r=Math.sqrt(Math.pow(t,2)+Math.pow(n,2)),t=l(Math.atan2(n,t),a,i);return[h.invert(t),s.invert(r)]}}}function H(t,n,r,e,o){return{transform:function(t){var n=t[0];return[t[1],n]},untransform:function(t){var n=t[0];return[t[1],n]}}}function K(t,n,r,e,o){var a=t[0],i=t[1],t=M();return i=[a,i],(t=t)[0]=i[0],t[1]=0,t[2]=0,t[3]=0,t[4]=i[1],t[5]=0,t[6]=0,t[7]=0,t[8]=1,t}function Q(t){for(var n=[],r=1;r<arguments.length;r++)n[r-1]=arguments[r];return K.apply(void 0,g([[-1,-1]],n))}function U(t){for(var n=[],r=1;r<arguments.length;r++)n[r-1]=arguments[r];return K.apply(void 0,g([[-1,1]],n))}function V(t){for(var n=[],r=1;r<arguments.length;r++)n[r-1]=arguments[r];return K.apply(void 0,g([[1,-1]],n))}function X(t,n,r,e,o){var a=t[0],i=M();return t=i,i=a,a=Math.sin(i),i=Math.cos(i),t[0]=i,t[1]=a,t[2]=0,t[3]=-a,t[4]=i,t[5]=0,t[6]=0,t[7]=0,t[8]=1,t}function Y(t,n,r,e,o){var a=t[0],i=t[1],u=t[2],s=(t[3]-u)/(+i/(2*Math.PI)+1),h=s/(2*Math.PI),f=new S({range:[u,u+.99*s]}),c=new S({range:[a,i]}),p=1<(e=o/e)?1:e,d=1<e?1/e:1;return{transform:function(t){var n=t[0],t=t[1],n=c.map(n),t=f.map(t);return[.5*(Math.cos(n)*(h*n+t)*p)+.5,.5*(Math.sin(n)*(h*n+t)*d)+.5]},untransform:function(t){var n=t[0],r=t[1],t=2*(n-.5)/p,n=2*(r-.5)/d,r=Math.sqrt(Math.pow(t,2)+Math.pow(n,2)),t=l(Math.atan2(n,t)+Math.floor(r/s)*Math.PI*2,a,i),r=r-h*t;return[c.invert(t),f.invert(r)]}}}function Z(t,n,r,e,o){var u=t[0],s=t[1],a=t[2],t=t[3],h=new S({range:[a,t]});return{transform:function(t){for(var n=[],r=t.length,e=new w({domain:new Array(r).fill(0).map(function(t,n){return n}),range:[u,s]}),o=0;o<r;o++){var a=t[o],i=e.map(o),a=h.map(a);n.push(i,a)}return n.push(1,n[1]),n},untransform:function(t){for(var n=[],r=0;r<t.length-2;r+=2){var e=t[r+1];n.push(h.invert(e))}return n}}}function $(t){return 1/Math.tan(t)}function tt(t,n,r,e,o){var a=$(t[0]);return{transform:function(t){var n=t[0],t=t[1];return[n+t*a,t]},untransform:function(t){var n=t[0],t=t[1];return[n-t*a,t]}}}function nt(t,n,r,e,o){var a=$(t[0]);return{transform:function(t){var n=t[0];return[n,t[1]+n*a]},untransform:function(t){var n=t[0];return[n,t[1]-n*a]}}}function rt(t,n,r,e,o){var a=t<n,e=(a?n-e:o-n)||o-e,a=a?-1:1;return a*e*(r+1)/(r+e/((t-n)*a))+n}function et(t,n,r,e,o){var a=t<n,e=(a?n-e:o-n)||o-e;return e/(e*(r+1)/(t-n)-r*(a?-1:1))+n}function ot(t,r,n,e,o){var a=t[0],i=t[1];return{transform:function(t){var n=t[0],t=t[1];return[rt(n,a,i,r,r+e),t]},untransform:function(t){var n=t[0],t=t[1];return[et(n,a,i,r,r+e),t]}}}function at(t,n,r,e,o){var a=t[0],i=t[1];return{transform:function(t){return[t[0],rt(t[1],a,i,r,r+o)]},untransform:function(t){return[t[0],et(t[1],a,i,r,r+o)]}}}function it(t,r,e,o,a){var i=t[0],u=t[1],s=t[2],h=t[3];return{transform:function(t){var n=t[0],t=t[1];return[rt(n,i,s,r,r+o),rt(t,u,h,e,e+a)]},untransform:function(t){var n=t[0],t=t[1];return[et(n,i,s,r,r+o),et(t,u,h,e,e+a)]}}}function ut(t,n,r,e,o){var a=t[0],i=t[1],u=t[2],s=t[3];return{transform:function(t){var n=t[0],r=t[1],e=n-a,o=r-i,t=Math.sqrt(e*e+o*o);if(u<t)return[n,r];t=rt(t,0,s,0,u),e=Math.atan2(o,e);return[a+t*Math.cos(e),i+t*Math.sin(e)]},untransform:function(t){var n=t[0],r=t[1],e=n-a,o=r-i,t=Math.sqrt(e*e+o*o);if(u<t)return[n,r];t=et(t,0,s,0,u),e=Math.atan2(o,e);return[a+t*Math.cos(e),i+t*Math.sin(e)]}}}st.prototype.update=function(t){this.options=n({},this.options,t),this.recoordinate()},st.prototype.clone=function(){return new st(this.options)},st.prototype.getOptions=function(){return this.options},st.prototype.clear=function(){this.update({transformations:[]})},st.prototype.getSize=function(){var t=this.options;return[t.width,t.height]},st.prototype.getCenter=function(){var t=this.options,n=t.x,r=t.y;return[(n+t.width)/2,(r+t.height)/2]},st.prototype.transform=function(){for(var t=[],n=0;n<arguments.length;n++)t[n]=arguments[n];var r=this.options.transformations;return this.update({transformations:g(g([],r),[g([],t)])}),this},st.prototype.map=function(t){return this.output(t)},st.prototype.invert=function(t){return this.input(t)},st.prototype.recoordinate=function(){this.output=this.compose(),this.input=this.compose(!0)},st.prototype.compose=function(t){for(var n=(t=void 0===t?!1:t)?g([],this.options.transformations).reverse():this.options.transformations,r=t?function(t){return t.untransform}:function(t){return t.transform},e=[],a=[],o=function(t,n){return a.push((n=void 0===n?!0:n)?(o=t,function(t){for(var n=[],r=0;r<t.length-1;r+=2){var e=[t[r],t[r+1]],e=o(e);n.push.apply(n,e)}return n}):t);var o},i=0,u=n;i<u.length;i++){var s,h,f,c=u[i],p=c[0],d=c.slice(1),l=this.transformers[p];l&&(s=(f=this.options).x,h=f.y,c=f.width,f=f.height,c=l(g([],d),s,h,c,f),(f=c)instanceof Float32Array||f instanceof Array?e.push(c):(e.length&&(o(this.createMatrixTransform(e,t)),e.splice(0,e.length)),o(r(c)||m,"parallel"!==p)))}return e.length&&o(this.createMatrixTransform(e,t)),function(t){for(var n=[],r=1;r<arguments.length;r++)n[r-1]=arguments[r];return t?n.reduce(function(n,r){return function(t){return r(n(t))}},t):m}.apply(void 0,a)},st.prototype.createMatrixTransform=function(t,n){var r,e,o,a,i,u,s,h,f,c,p,d,l=M();return n&&t.reverse(),t.forEach(function(t){return y(l,l,t)}),n&&(p=r=l,(d=new v(9))[0]=p[0],d[1]=p[1],d[2]=p[2],d[3]=p[3],d[4]=p[4],d[5]=p[5],d[6]=p[6],d[7]=p[7],d[8]=p[8],o=(e=d)[0],a=e[1],i=e[2],u=e[3],s=e[4],h=e[5],f=e[6],c=e[7],t=e[8],(e=o*(n=t*s-h*c)+a*(p=-t*u+h*f)+i*(d=c*u-s*f))&&(r[0]=n*(e=1/e),r[1]=(-t*a+i*c)*e,r[2]=(h*a-i*s)*e,r[3]=p*e,r[4]=(t*o-i*f)*e,r[5]=(-h*o+i*u)*e,r[6]=d*e,r[7]=(-c*o+a*f)*e,r[8]=(s*o-a*u)*e)),function(t){var n,r,e,o,a=[t[0],t[1],1];return e=l,o=(r=n=a)[0],t=r[1],r=r[2],n[0]=o*e[0]+t*e[3]+r*e[6],n[1]=o*e[1]+t*e[4]+r*e[7],n[2]=o*e[2]+t*e[5]+r*e[8],[a[0],a[1]]}},J=st;function st(t){this.options={x:0,y:0,width:300,height:150,transformations:[]},this.transformers={cartesian:B,translate:a,custom:E,matrix:L,polar:G,transpose:H,scale:K,"shear.x":tt,"shear.y":nt,reflect:Q,"reflect.x":U,"reflect.y":V,rotate:X,helix:Y,parallel:Z,fisheye:it,"fisheye.x":ot,"fisheye.y":at,"fisheye.circular":ut},this.update(t)}t.Coordinate=J,Object.defineProperty(t,"__esModule",{value:!0})});