d3/d3.layout.min.js

15 строки
6.9 KiB
JavaScript

(function(){function A(c){return c.reduce(B,0)}function C(c){for(var h=1,n=0,i=c[0].y,o,l=c.length;h<l;++h)if((o=c[h].y)>i){n=h;i=o}return n}function B(c,h){return c+h.y}function D(c){return c.children}function E(c){return c.value}function F(c,h){return h.area-c.area}d3.layout={};d3.layout.chord=function(){function c(){var a={},j=[],f=d3.range(g),s=[],t,k,q,r,u;i=[];o=[];t=0;for(r=-1;++r<g;){k=0;for(u=-1;++u<g;)k+=l[r][u];j.push(k);s.push(d3.range(g));t+=k}p&&f.sort(function(y,x){return p(j[y],j[x])});
e&&s.forEach(function(y,x){y.sort(function(G,H){return e(l[x][G],l[x][H])})});t=(2*Math.PI-m*g)/t;k=0;for(r=-1;++r<g;){q=k;for(u=-1;++u<g;){var v=f[r],w=s[r][u],z=l[v][w];a[v+"-"+w]={index:v,subindex:w,startAngle:k,endAngle:k+=z*t,value:z}}o.push({index:v,startAngle:q,endAngle:k,value:(k-q)/t});k+=m}for(r=-1;++r<g;)for(u=r-1;++u<g;){f=a[r+"-"+u];s=a[u+"-"+r];if(f.value||s.value)i.push({source:f,target:s})}b&&h()}function h(){i.sort(function(a,j){a=Math.min(a.source.value,a.target.value);j=Math.min(j.source.value,
j.target.value);return b(a,j)})}var n={},i,o,l,g,m=0,p,e,b;n.matrix=function(a){if(!arguments.length)return l;g=(l=a)&&l.length;i=o=null;return n};n.padding=function(a){if(!arguments.length)return m;m=a;i=o=null;return n};n.sortGroups=function(a){if(!arguments.length)return p;p=a;i=o=null;return n};n.sortSubgroups=function(a){if(!arguments.length)return e;e=a;i=null;return n};n.sortChords=function(a){if(!arguments.length)return b;b=a;i&&h();return n};n.chords=function(){i||c();return i};n.groups=
function(){o||c();return o};return n};d3.layout.force=function(){function c(){var b=e.length,a,j,f,s,t,k,q;for(a=0;a<b;++a){j=e[a];f=j.source;s=j.target;k=s.x-f.x;q=s.y-f.y;if(t=Math.sqrt(k*k+q*q)){t=o/(j.distance*j.distance)*(t-l*j.distance)/t;k*=t;q*=t;if(f.fixed){if(!s.fixed){s.x-=k;s.y-=q}}else if(s.fixed){f.x+=k;f.y+=q}else{f.x+=k;f.y+=q;s.x-=k;s.y-=q}}}if((o*=0.99)<1.0E-6)h.stop();n.tick.dispatch({type:"tick"})}var h={},n=d3.dispatch("tick"),i=[1,1],o=0.5,l=30,g,m,p,e;h.on=function(b,a){n[b].add(a);
return h};h.nodes=function(b){if(!arguments.length)return m;m=b;return h};h.links=function(b){if(!arguments.length)return p;p=b;return h};h.size=function(b){if(!arguments.length)return i;i=b;return h};h.distance=function(b){if(!arguments.length)return l;l=b;return h};h.start=function(){var b,a,j,f=m.length;j=p.length;var s=i[0],t=i[1],k=[];for(b=0;b<f;++b){a=m[b];a.x=a.x||Math.random()*s;a.y=a.y||Math.random()*t;a.fixed=0;k[b]=[];for(a=0;a<f;++a)k[b][a]=Infinity;k[b][b]=0}for(b=0;b<j;++b){a=p[b];
k[a.source][a.target]=1;k[a.target][a.source]=1;a.source=m[a.source];a.target=m[a.target]}for(j=0;j<f;++j)for(b=0;b<f;++b)for(a=0;a<f;++a)k[b][a]=Math.min(k[b][a],k[b][j]+k[j][a]);e=[];for(b=0;b<f;++b)for(a=b+1;a<f;++a)e.push({source:m[b],target:m[a],distance:k[b][a]*k[b][a]});e.sort(function(q,r){return q.distance-r.distance});g&&clearInterval(g);g=setInterval(c,24);return h};h.resume=function(){o=0.1;g||(g=setInterval(c,24));return h};h.stop=function(){g=clearInterval(g);return h};h.drag=function(){function b(){if(a){var f=
d3.svg.mouse(j);a.x=f[0];a.y=f[1];h.resume()}}var a,j;this.on("mouseover",function(f){f.fixed=true}).on("mouseout",function(f){if(f!=a)f.fixed=false}).on("mousedown",function(f){(a=f).fixed=true;j=this;d3.event.preventDefault()});d3.select(window).on("mousemove",b).on("mouseup",function(){if(a){b();a.fixed=false;a=j=null}});return h};return h};d3.layout.pie=function(){function c(l){var g=+(typeof i=="function"?i.apply(this,arguments):i),m=(typeof o=="function"?o.apply(this,arguments):o)-i,p=d3.range(l.length);
n!=null&&p.sort(function(a,j){return n(l[a],l[j])});var e=l.map(h);m/=e.reduce(function(a,j){return a+j},0);var b=p.map(function(a){return{value:d=e[a],startAngle:g,endAngle:g+=d*m}});return l.map(function(a,j){return b[p[j]]})}var h=Number,n=null,i=0,o=2*Math.PI;c.value=function(l){if(!arguments.length)return h;h=l;return c};c.sort=function(l){if(!arguments.length)return n;n=l;return c};c.startAngle=function(l){if(!arguments.length)return i;i=l;return c};c.endAngle=function(l){if(!arguments.length)return o;
o=l;return c};return c};d3.layout.stack=function(){function c(i){var o=i.length,l=i[0].length,g,m,p,e=I[h](i);J[n](i,e);for(m=0;m<l;++m){g=1;for(p=i[e[0]][m].y0;g<o;++g)i[e[g]][m].y0=p+=i[e[g-1]][m].y}return i}var h="default",n="zero";c.order=function(i){if(!arguments.length)return h;h=i;return c};c.offset=function(i){if(!arguments.length)return n;n=i;return c};return c};var I={"inside-out":function(c){var h=c.length,n,i=c.map(C),o=c.map(A),l=d3.range(h).sort(function(b,a){return i[b]-i[a]}),g=0,
m=0,p=[],e=[];for(c=0;c<h;c++){n=l[c];if(g<m){g+=o[n];p.push(n)}else{m+=o[n];e.push(n)}}return e.reverse().concat(p)},reverse:function(c){return d3.range(c.length).reverse()},"default":function(c){return d3.range(c.length)}},J={silhouette:function(c,h){var n=c.length,i=c[0].length,o=[],l=0,g,m,p;for(m=0;m<i;++m){for(p=g=0;g<n;g++)p+=c[g][m].y;if(p>l)l=p;o.push(p)}m=0;for(g=h[0];m<i;++m)c[g][m].y0=(l-o[m])/2},wiggle:function(c,h){var n=c.length,i=c[0],o=i.length,l,g,m,p,e,b=h[0],a,j,f,s,t,k;c[b][0].y0=
t=k=0;for(g=1;g<o;++g){for(a=l=0;l<n;++l)a+=c[l][g].y;j=l=0;for(s=i[g].x-i[g-1].x;l<n;++l){m=0;p=h[l];for(f=(c[p][g].y-c[p][g-1].y)/(2*s);m<l;++m)f+=(c[e=h[m]][g].y-c[e][g-1].y)/s;j+=f*c[p][g].y}c[b][g].y0=t-=a?j/a*s:0;if(t<k)k=t}for(g=0;g<o;++g)c[b][g].y0-=k},zero:function(c,h){for(var n=0,i=c[0].length,o=h[0];n<i;++n)c[o][n].y0=0}};d3.layout.treemap=function(){function c(e,b,a){var j=l.call(o,e,b),f={depth:b,data:e};a.push(f);if(j){e=-1;for(var s=j.length,t=f.children=[],k=0,q=b+1;++e<s;){d=c(j[e],
q,a);if(d.value>0){t.push(d);k+=d.value}}f.value=k}else f.value=g.call(o,e,b);b||h(f,p[0]*p[1]/f.value);return f}function h(e,b){var a=e.children;e.area=e.value*b;if(a)for(var j=-1,f=a.length;++j<f;)h(a[j],b)}function n(e){if(e.children){var b={x:e.x,y:e.y,dx:e.dx,dy:e.dy},a=[],j=e.children.slice().sort(F),f,s=Infinity,t=Math.min(b.dx,b.dy);for(a.area=0;(f=j.length)>0;){a.push(f=j[f-1]);a.area+=f.area;f=t;for(var k=a.area,q=void 0,r=0,u=Infinity,v=-1,w=a.length;++v<w;){q=a[v].area;if(q<u)u=q;if(q>
r)r=q}k*=k;f*=f;if((f=Math.max(f*r/k,k/(f*u)))<=s){j.pop();s=f}else{a.area-=a.pop().area;i(a,t,b,false);t=Math.min(b.dx,b.dy);a.length=a.area=0;s=Infinity}}if(a.length){i(a,t,b,true);a.length=a.area=0}e.children.forEach(n)}}function i(e,b,a,j){var f=-1,s=e.length,t=a.x,k=a.y,q=b?m(e.area/b):0,r;if(b==a.dx){if(j||q>a.dy)q=a.dy;for(;++f<s;){r=e[f];r.x=t;r.y=k;r.dy=q;t+=r.dx=m(r.area/q)}r.dx+=a.x+a.dx-t;a.y+=q;a.dy-=q}else{if(j||q>a.dx)q=a.dx;for(;++f<s;){r=e[f];r.x=t;r.y=k;r.dx=q;k+=r.dy=m(r.area/q)}r.dy+=
a.y+a.dy-k;a.x+=q;a.dx-=q}}function o(e){var b=[];e=c(e,0,b);e.x=0;e.y=0;e.dx=p[0];e.dy=p[1];n(e);return b}var l=D,g=E,m=Math.round,p=[1,1];o.children=function(e){if(!arguments.length)return l;l=e;return o};o.value=function(e){if(!arguments.length)return g;g=e;return o};o.size=function(e){if(!arguments.length)return p;p=e;return o};o.round=function(e){if(!arguments.length)return m!=Number;m=e?Math.round:Number;return o};return o}})();