You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
2 lines
13 KiB
2 lines
13 KiB
// https://d3js.org/d3-hierarchy/ Version 1.1.5. Copyright 2017 Mike Bostock.
|
|
!function(n,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports):"function"==typeof define&&define.amd?define(["exports"],r):r(n.d3=n.d3||{})}(this,function(n){"use strict";function r(n,r){return n.parent===r.parent?1:2}function e(n){return n.reduce(t,0)/n.length}function t(n,r){return n+r.x}function u(n){return 1+n.reduce(i,0)}function i(n,r){return Math.max(n,r.y)}function o(n){for(var r;r=n.children;)n=r[0];return n}function f(n){for(var r;r=n.children;)n=r[r.length-1];return n}function a(n){var r=0,e=n.children,t=e&&e.length;if(t)for(;--t>=0;)r+=e[t].value;else r=1;n.value=r}function c(n,r){if(n===r)return n;var e=n.ancestors(),t=r.ancestors(),u=null;for(n=e.pop(),r=t.pop();n===r;)u=n,n=e.pop(),r=t.pop();return u}function h(n,r){var e,t,u,i,o,f=new v(n),a=+n.value&&(f.value=n.value),c=[f];for(null==r&&(r=p);e=c.pop();)if(a&&(e.value=+e.data.value),(u=r(e.data))&&(o=u.length))for(e.children=new Array(o),i=o-1;i>=0;--i)c.push(t=e.children[i]=new v(u[i])),t.parent=e,t.depth=e.depth+1;return f.eachBefore(s)}function l(){return h(this).eachBefore(d)}function p(n){return n.children}function d(n){n.data=n.data.data}function s(n){var r=0;do{n.height=r}while((n=n.parent)&&n.height<++r)}function v(n){this.data=n,this.depth=this.height=0,this.parent=null}function x(n){for(var r,e,t=n.length;t;)e=Math.random()*t--|0,r=n[t],n[t]=n[e],n[e]=r;return n}function y(n,r){var e,t;if(w(r,n))return[r];for(e=0;e<n.length;++e)if(g(r,n[e])&&w(B(n[e],r),n))return[n[e],r];for(e=0;e<n.length-1;++e)for(t=e+1;t<n.length;++t)if(g(B(n[e],n[t]),r)&&g(B(n[e],r),n[t])&&g(B(n[t],r),n[e])&&w(M(n[e],n[t],r),n))return[n[e],n[t],r];throw new Error}function g(n,r){var e=n.r-r.r,t=r.x-n.x,u=r.y-n.y;return e<0||e*e<t*t+u*u}function m(n,r){var e=n.r-r.r+1e-6,t=r.x-n.x,u=r.y-n.y;return e>0&&e*e>t*t+u*u}function w(n,r){for(var e=0;e<r.length;++e)if(!m(n,r[e]))return!1;return!0}function _(n){switch(n.length){case 1:return z(n[0]);case 2:return B(n[0],n[1]);case 3:return M(n[0],n[1],n[2])}}function z(n){return{x:n.x,y:n.y,r:n.r}}function B(n,r){var e=n.x,t=n.y,u=n.r,i=r.x,o=r.y,f=r.r,a=i-e,c=o-t,h=f-u,l=Math.sqrt(a*a+c*c);return{x:(e+i+a/l*h)/2,y:(t+o+c/l*h)/2,r:(l+u+f)/2}}function M(n,r,e){var t=n.x,u=n.y,i=n.r,o=r.x,f=r.y,a=r.r,c=e.x,h=e.y,l=e.r,p=t-o,d=t-c,s=u-f,v=u-h,x=a-i,y=l-i,g=t*t+u*u-i*i,m=g-o*o-f*f+a*a,w=g-c*c-h*h+l*l,_=d*s-p*v,z=(s*w-v*m)/(2*_)-t,B=(v*x-s*y)/_,M=(d*m-p*w)/(2*_)-u,A=(p*y-d*x)/_,q=B*B+A*A-1,E=2*(i+z*B+M*A),b=z*z+M*M-i*i,S=-(q?(E+Math.sqrt(E*E-4*q*b))/(2*q):b/E);return{x:t+z+B*S,y:u+M+A*S,r:S}}function A(n,r,e){var t=n.x,u=n.y,i=r.r+e.r,o=n.r+e.r,f=r.x-t,a=r.y-u,c=f*f+a*a;if(c){var h=.5+((o*=o)-(i*=i))/(2*c),l=Math.sqrt(Math.max(0,2*i*(o+c)-(o-=c)*o-i*i))/(2*c);e.x=t+h*f+l*a,e.y=u+h*a-l*f}else e.x=t+o,e.y=u}function q(n,r){var e=r.x-n.x,t=r.y-n.y,u=n.r+r.r;return u*u-1e-6>e*e+t*t}function E(n){var r=n._,e=n.next._,t=r.r+e.r,u=(r.x*e.r+e.x*r.r)/t,i=(r.y*e.r+e.y*r.r)/t;return u*u+i*i}function b(n){this._=n,this.next=null,this.previous=null}function S(n){if(!(u=n.length))return 0;var r,e,t,u,i,o,f,a,c,h,l;if(r=n[0],r.x=0,r.y=0,!(u>1))return r.r;if(e=n[1],r.x=-e.r,e.x=r.r,e.y=0,!(u>2))return r.r+e.r;A(e,r,t=n[2]),r=new b(r),e=new b(e),t=new b(t),r.next=t.previous=e,e.next=r.previous=t,t.next=e.previous=r;n:for(f=3;f<u;++f){A(r._,e._,t=n[f]),t=new b(t),a=e.next,c=r.previous,h=e._.r,l=r._.r;do{if(h<=l){if(q(a._,t._)){e=a,r.next=e,e.previous=r,--f;continue n}h+=a._.r,a=a.next}else{if(q(c._,t._)){(r=c).next=e,e.previous=r,--f;continue n}l+=c._.r,c=c.previous}}while(a!==c.next);for(t.previous=r,t.next=e,r.next=e.previous=e=t,i=E(r);(t=t.next)!==e;)(o=E(t))<i&&(r=t,i=o);e=r.next}for(r=[e._],t=e;(t=t.next)!==e;)r.push(t._);for(t=an(r),f=0;f<u;++f)r=n[f],r.x-=t.x,r.y-=t.y;return t.r}function k(n){return null==n?null:I(n)}function I(n){if("function"!=typeof n)throw new Error;return n}function O(){return 0}function j(n){return Math.sqrt(n.value)}function R(n){return function(r){r.children||(r.r=Math.max(0,+n(r)||0))}}function T(n,r){return function(e){if(t=e.children){var t,u,i,o=t.length,f=n(e)*r||0;if(f)for(u=0;u<o;++u)t[u].r+=f;if(i=S(t),f)for(u=0;u<o;++u)t[u].r-=f;e.r=i+f}}}function D(n){return function(r){var e=r.parent;r.r*=n,e&&(r.x=e.x+n*r.x,r.y=e.y+n*r.y)}}function L(n){return n.id}function P(n){return n.parentId}function $(n,r){return n.parent===r.parent?1:2}function C(n){var r=n.children;return r?r[0]:n.t}function F(n){var r=n.children;return r?r[r.length-1]:n.t}function G(n,r,e){var t=e/(r.i-n.i);r.c-=t,r.s+=e,n.c+=t,r.z+=e,r.m+=e}function H(n){for(var r,e=0,t=0,u=n.children,i=u.length;--i>=0;)(r=u[i]).z+=e,r.m+=e,e+=r.s+(t+=r.c)}function J(n,r,e){return n.a.parent===r.parent?n.a:e}function K(n,r){this._=n,this.parent=null,this.children=null,this.A=null,this.a=this,this.z=0,this.m=0,this.c=0,this.s=0,this.t=null,this.i=r}function N(n){for(var r,e,t,u,i,o=new K(n,0),f=[o];r=f.pop();)if(t=r._.children)for(r.children=new Array(i=t.length),u=i-1;u>=0;--u)f.push(e=r.children[u]=new K(t[u],u)),e.parent=r;return(o.parent=new K(null,0)).children=[o],o}function Q(n,r,e,t,u,i){for(var o,f,a,c,h,l,p,d,s,v,x,y=[],g=r.children,m=0,w=0,_=g.length,z=r.value;m<_;){a=u-e,c=i-t;do{h=g[w++].value}while(!h&&w<_);for(l=p=h,x=h*h*(v=Math.max(c/a,a/c)/(z*n)),s=Math.max(p/x,x/l);w<_;++w){if(h+=f=g[w].value,f<l&&(l=f),f>p&&(p=f),x=h*h*v,(d=Math.max(p/x,x/l))>s){h-=f;break}s=d}y.push(o={value:h,dice:a<c,children:g.slice(m,w)}),o.dice?dn(o,e,t,u,z?t+=c*h/z:i):wn(o,e,t,z?e+=a*h/z:u,i),z-=h,m=w}return y}var U=function(){function n(n){var r,h=0;n.eachAfter(function(n){var i=n.children;i?(n.x=e(i),n.y=u(i)):(n.x=r?h+=t(n,r):0,n.y=0,r=n)});var l=o(n),p=f(n),d=l.x-t(l,p)/2,s=p.x+t(p,l)/2;return n.eachAfter(c?function(r){r.x=(r.x-n.x)*i,r.y=(n.y-r.y)*a}:function(r){r.x=(r.x-d)/(s-d)*i,r.y=(1-(n.y?r.y/n.y:1))*a})}var t=r,i=1,a=1,c=!1;return n.separation=function(r){return arguments.length?(t=r,n):t},n.size=function(r){return arguments.length?(c=!1,i=+r[0],a=+r[1],n):c?null:[i,a]},n.nodeSize=function(r){return arguments.length?(c=!0,i=+r[0],a=+r[1],n):c?[i,a]:null},n},V=function(){return this.eachAfter(a)},W=function(n){var r,e,t,u,i=this,o=[i];do{for(r=o.reverse(),o=[];i=r.pop();)if(n(i),e=i.children)for(t=0,u=e.length;t<u;++t)o.push(e[t])}while(o.length);return this},X=function(n){for(var r,e,t=this,u=[t];t=u.pop();)if(n(t),r=t.children)for(e=r.length-1;e>=0;--e)u.push(r[e]);return this},Y=function(n){for(var r,e,t,u=this,i=[u],o=[];u=i.pop();)if(o.push(u),r=u.children)for(e=0,t=r.length;e<t;++e)i.push(r[e]);for(;u=o.pop();)n(u);return this},Z=function(n){return this.eachAfter(function(r){for(var e=+n(r.data)||0,t=r.children,u=t&&t.length;--u>=0;)e+=t[u].value;r.value=e})},nn=function(n){return this.eachBefore(function(r){r.children&&r.children.sort(n)})},rn=function(n){for(var r=this,e=c(r,n),t=[r];r!==e;)r=r.parent,t.push(r);for(var u=t.length;n!==e;)t.splice(u,0,n),n=n.parent;return t},en=function(){for(var n=this,r=[n];n=n.parent;)r.push(n);return r},tn=function(){var n=[];return this.each(function(r){n.push(r)}),n},un=function(){var n=[];return this.eachBefore(function(r){r.children||n.push(r)}),n},on=function(){var n=this,r=[];return n.each(function(e){e!==n&&r.push({source:e.parent,target:e})}),r};v.prototype=h.prototype={constructor:v,count:V,each:W,eachAfter:Y,eachBefore:X,sum:Z,sort:nn,path:rn,ancestors:en,descendants:tn,leaves:un,links:on,copy:l};var fn=Array.prototype.slice,an=function(n){for(var r,e,t=0,u=(n=x(fn.call(n))).length,i=[];t<u;)r=n[t],e&&m(e,r)?++t:(e=_(i=y(i,r)),t=0);return e},cn=function(n){return S(n),n},hn=function(n){return function(){return n}},ln=function(){function n(n){return n.x=e/2,n.y=t/2,r?n.eachBefore(R(r)).eachAfter(T(u,.5)).eachBefore(D(1)):n.eachBefore(R(j)).eachAfter(T(O,1)).eachAfter(T(u,n.r/Math.min(e,t))).eachBefore(D(Math.min(e,t)/(2*n.r))),n}var r=null,e=1,t=1,u=O;return n.radius=function(e){return arguments.length?(r=k(e),n):r},n.size=function(r){return arguments.length?(e=+r[0],t=+r[1],n):[e,t]},n.padding=function(r){return arguments.length?(u="function"==typeof r?r:hn(+r),n):u},n},pn=function(n){n.x0=Math.round(n.x0),n.y0=Math.round(n.y0),n.x1=Math.round(n.x1),n.y1=Math.round(n.y1)},dn=function(n,r,e,t,u){for(var i,o=n.children,f=-1,a=o.length,c=n.value&&(t-r)/n.value;++f<a;)(i=o[f]).y0=e,i.y1=u,i.x0=r,i.x1=r+=i.value*c},sn=function(){function n(n){var o=n.height+1;return n.x0=n.y0=u,n.x1=e,n.y1=t/o,n.eachBefore(r(t,o)),i&&n.eachBefore(pn),n}function r(n,r){return function(e){e.children&&dn(e,e.x0,n*(e.depth+1)/r,e.x1,n*(e.depth+2)/r);var t=e.x0,i=e.y0,o=e.x1-u,f=e.y1-u;o<t&&(t=o=(t+o)/2),f<i&&(i=f=(i+f)/2),e.x0=t,e.y0=i,e.x1=o,e.y1=f}}var e=1,t=1,u=0,i=!1;return n.round=function(r){return arguments.length?(i=!!r,n):i},n.size=function(r){return arguments.length?(e=+r[0],t=+r[1],n):[e,t]},n.padding=function(r){return arguments.length?(u=+r,n):u},n},vn="$",xn={depth:-1},yn={},gn=function(){function n(n){var t,u,i,o,f,a,c,h=n.length,l=new Array(h),p={};for(u=0;u<h;++u)t=n[u],f=l[u]=new v(t),null!=(a=r(t,u,n))&&(a+="")&&(p[c=vn+(f.id=a)]=c in p?yn:f);for(u=0;u<h;++u)if(f=l[u],null!=(a=e(n[u],u,n))&&(a+="")){if(!(o=p[vn+a]))throw new Error("missing: "+a);if(o===yn)throw new Error("ambiguous: "+a);o.children?o.children.push(f):o.children=[f],f.parent=o}else{if(i)throw new Error("multiple roots");i=f}if(!i)throw new Error("no root");if(i.parent=xn,i.eachBefore(function(n){n.depth=n.parent.depth+1,--h}).eachBefore(s),i.parent=null,h>0)throw new Error("cycle");return i}var r=L,e=P;return n.id=function(e){return arguments.length?(r=I(e),n):r},n.parentId=function(r){return arguments.length?(e=I(r),n):e},n};K.prototype=Object.create(v.prototype);var mn=function(){function n(n){var t=N(n);if(t.eachAfter(r),t.parent.m=-t.z,t.eachBefore(e),a)n.eachBefore(u);else{var c=n,h=n,l=n;n.eachBefore(function(n){n.x<c.x&&(c=n),n.x>h.x&&(h=n),n.depth>l.depth&&(l=n)});var p=c===h?1:i(c,h)/2,d=p-c.x,s=o/(h.x+p+d),v=f/(l.depth||1);n.eachBefore(function(n){n.x=(n.x+d)*s,n.y=n.depth*v})}return n}function r(n){var r=n.children,e=n.parent.children,u=n.i?e[n.i-1]:null;if(r){H(n);var o=(r[0].z+r[r.length-1].z)/2;u?(n.z=u.z+i(n._,u._),n.m=n.z-o):n.z=o}else u&&(n.z=u.z+i(n._,u._));n.parent.A=t(n,u,n.parent.A||e[0])}function e(n){n._.x=n.z+n.parent.m,n.m+=n.parent.m}function t(n,r,e){if(r){for(var t,u=n,o=n,f=r,a=u.parent.children[0],c=u.m,h=o.m,l=f.m,p=a.m;f=F(f),u=C(u),f&&u;)a=C(a),(o=F(o)).a=n,(t=f.z+l-u.z-c+i(f._,u._))>0&&(G(J(f,n,e),n,t),c+=t,h+=t),l+=f.m,c+=u.m,p+=a.m,h+=o.m;f&&!F(o)&&(o.t=f,o.m+=l-h),u&&!C(a)&&(a.t=u,a.m+=c-p,e=n)}return e}function u(n){n.x*=o,n.y=n.depth*f}var i=$,o=1,f=1,a=null;return n.separation=function(r){return arguments.length?(i=r,n):i},n.size=function(r){return arguments.length?(a=!1,o=+r[0],f=+r[1],n):a?null:[o,f]},n.nodeSize=function(r){return arguments.length?(a=!0,o=+r[0],f=+r[1],n):a?[o,f]:null},n},wn=function(n,r,e,t,u){for(var i,o=n.children,f=-1,a=o.length,c=n.value&&(u-e)/n.value;++f<a;)(i=o[f]).x0=r,i.x1=t,i.y0=e,i.y1=e+=i.value*c},_n=(1+Math.sqrt(5))/2,zn=function n(r){function e(n,e,t,u,i){Q(r,n,e,t,u,i)}return e.ratio=function(r){return n((r=+r)>1?r:1)},e}(_n),Bn=function(){function n(n){return n.x0=n.y0=0,n.x1=u,n.y1=i,n.eachBefore(r),o=[0],t&&n.eachBefore(pn),n}function r(n){var r=o[n.depth],t=n.x0+r,u=n.y0+r,i=n.x1-r,p=n.y1-r;i<t&&(t=i=(t+i)/2),p<u&&(u=p=(u+p)/2),n.x0=t,n.y0=u,n.x1=i,n.y1=p,n.children&&(r=o[n.depth+1]=f(n)/2,t+=l(n)-r,u+=a(n)-r,i-=c(n)-r,p-=h(n)-r,i<t&&(t=i=(t+i)/2),p<u&&(u=p=(u+p)/2),e(n,t,u,i,p))}var e=zn,t=!1,u=1,i=1,o=[0],f=O,a=O,c=O,h=O,l=O;return n.round=function(r){return arguments.length?(t=!!r,n):t},n.size=function(r){return arguments.length?(u=+r[0],i=+r[1],n):[u,i]},n.tile=function(r){return arguments.length?(e=I(r),n):e},n.padding=function(r){return arguments.length?n.paddingInner(r).paddingOuter(r):n.paddingInner()},n.paddingInner=function(r){return arguments.length?(f="function"==typeof r?r:hn(+r),n):f},n.paddingOuter=function(r){return arguments.length?n.paddingTop(r).paddingRight(r).paddingBottom(r).paddingLeft(r):n.paddingTop()},n.paddingTop=function(r){return arguments.length?(a="function"==typeof r?r:hn(+r),n):a},n.paddingRight=function(r){return arguments.length?(c="function"==typeof r?r:hn(+r),n):c},n.paddingBottom=function(r){return arguments.length?(h="function"==typeof r?r:hn(+r),n):h},n.paddingLeft=function(r){return arguments.length?(l="function"==typeof r?r:hn(+r),n):l},n},Mn=function(n,r,e,t,u){function i(n,r,e,t,u,o,f){if(n>=r-1){var c=a[n];return c.x0=t,c.y0=u,c.x1=o,void(c.y1=f)}for(var l=h[n],p=e/2+l,d=n+1,s=r-1;d<s;){var v=d+s>>>1;h[v]<p?d=v+1:s=v}p-h[d-1]<h[d]-p&&n+1<d&&--d;var x=h[d]-l,y=e-x;if(o-t>f-u){var g=(t*y+o*x)/e;i(n,d,x,t,u,g,f),i(d,r,y,g,u,o,f)}else{var m=(u*y+f*x)/e;i(n,d,x,t,u,o,m),i(d,r,y,t,m,o,f)}}var o,f,a=n.children,c=a.length,h=new Array(c+1);for(h[0]=f=o=0;o<c;++o)h[o+1]=f+=a[o].value;i(0,c,n.value,r,e,t,u)},An=function(n,r,e,t,u){(1&n.depth?wn:dn)(n,r,e,t,u)},qn=function n(r){function e(n,e,t,u,i){if((o=n._squarify)&&o.ratio===r)for(var o,f,a,c,h,l=-1,p=o.length,d=n.value;++l<p;){for(a=(f=o[l]).children,c=f.value=0,h=a.length;c<h;++c)f.value+=a[c].value;f.dice?dn(f,e,t,u,t+=(i-t)*f.value/d):wn(f,e,t,e+=(u-e)*f.value/d,i),d-=f.value}else n._squarify=o=Q(r,n,e,t,u,i),o.ratio=r}return e.ratio=function(r){return n((r=+r)>1?r:1)},e}(_n);n.cluster=U,n.hierarchy=h,n.pack=ln,n.packSiblings=cn,n.packEnclose=an,n.partition=sn,n.stratify=gn,n.tree=mn,n.treemap=Bn,n.treemapBinary=Mn,n.treemapDice=dn,n.treemapSlice=wn,n.treemapSliceDice=An,n.treemapSquarify=zn,n.treemapResquarify=qn,Object.defineProperty(n,"__esModule",{value:!0})}); |