UNPKG

d3-flextree

Version:

Flexible tree layout algorithm that allows for variable node sizes.

3 lines (2 loc) 9.03 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e(t.d3=t.d3||{})}(this,function(t){"use strict";function e(t){var e=0,n=t.children,r=n&&n.length;if(r)for(;--r>=0;)e+=n[r].value;else e=1;t.value=e}function n(t,e){var n,i,h,l,c,a=new u(t),f=+t.value&&(a.value=t.value),s=[a];for(null==e&&(e=r);n=s.pop();)if(f&&(n.value=+n.data.value),(h=e(n.data))&&(c=h.length))for(n.children=new Array(c),l=c-1;l>=0;--l)s.push(i=n.children[l]=new u(h[l])),i.parent=n,i.depth=n.depth+1;return a.eachBefore(o)}function r(t){return t.children}function i(t){t.data=t.data.data}function o(t){var e=0;do{t.height=e}while((t=t.parent)&&t.height<++e)}function u(t){this.data=t,this.depth=this.height=0,this.parent=null}u.prototype=n.prototype={constructor:u,count:function(){return this.eachAfter(e)},each:function(t){var e,n,r,i,o=this,u=[o];do{for(e=u.reverse(),u=[];o=e.pop();)if(t(o),n=o.children)for(r=0,i=n.length;r<i;++r)u.push(n[r])}while(u.length);return this},eachAfter:function(t){for(var e,n,r,i=this,o=[i],u=[];i=o.pop();)if(u.push(i),e=i.children)for(n=0,r=e.length;n<r;++n)o.push(e[n]);for(;i=u.pop();)t(i);return this},eachBefore:function(t){for(var e,n,r=this,i=[r];r=i.pop();)if(t(r),e=r.children)for(n=e.length-1;n>=0;--n)i.push(e[n]);return this},sum:function(t){return this.eachAfter(function(e){for(var n=+t(e.data)||0,r=e.children,i=r&&r.length;--i>=0;)n+=r[i].value;e.value=n})},sort:function(t){return this.eachBefore(function(e){e.children&&e.children.sort(t)})},path:function(t){for(var e=this,n=function(t,e){if(t===e)return t;var n=t.ancestors(),r=e.ancestors(),i=null;for(t=n.pop(),e=r.pop();t===e;)i=t,t=n.pop(),e=r.pop();return i}(e,t),r=[e];e!==n;)e=e.parent,r.push(e);for(var i=r.length;t!==n;)r.splice(i,0,t),t=t.parent;return r},ancestors:function(){for(var t=this,e=[t];t=t.parent;)e.push(t);return e},descendants:function(){var t=[];return this.each(function(e){t.push(e)}),t},leaves:function(){var t=[];return this.eachBefore(function(e){e.children||t.push(e)}),t},links:function(){var t=this,e=[];return t.each(function(n){n!==t&&e.push({source:n.parent,target:n})}),e},copy:function(){return n(this).eachBefore(i)}};var h=function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")},l=function(){function t(t,e){for(var n=0;n<e.length;n++){var r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,r.key,r)}}return function(e,n,r){return n&&t(e.prototype,n),r&&t(e,r),e}}(),c=function(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function, not "+typeof e);t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}}),e&&(Object.setPrototypeOf?Object.setPrototypeOf(t,e):t.__proto__=e)},a=function(t,e){if(!t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!e||"object"!=typeof e&&"function"!=typeof e?t:e},f=function(){return function(t,e){if(Array.isArray(t))return t;if(Symbol.iterator in Object(t))return function(t,e){var n=[],r=!0,i=!1,o=void 0;try{for(var u,h=t[Symbol.iterator]();!(r=(u=h.next()).done)&&(n.push(u.value),!e||n.length!==e);r=!0);}catch(t){i=!0,o=t}finally{try{!r&&h.return&&h.return()}finally{if(i)throw o}}return n}(t,e);throw new TypeError("Invalid attempt to destructure non-iterable instance")}}(),s=Object.freeze({children:function(t){return t.children},nodeSize:function(t){return t.data.size},spacing:0});function d(t){var e=Object.assign({},s,t);function r(t){var n=e[t];return"function"==typeof n?n:function(){return n}}function i(t){var e,n,i,f=u((e=o(),n=r("nodeSize"),i=r("spacing"),function(t){function e(t){h(this,e);var n=a(this,(e.__proto__||Object.getPrototypeOf(e)).call(this,t));return Object.assign(n,{x:0,y:0,relX:0,prelim:0,shift:0,change:0,lExt:n,lExtRelX:0,lThr:null,rExt:n,rExtRelX:0,rThr:null}),n}return c(e,t),l(e,[{key:"spacing",value:function(t){return i(this.data,t.data)}},{key:"update",value:function(){return p(this),g(this),this}},{key:"size",get:function(){return n(this.data)}},{key:"x",get:function(){return this.data.x},set:function(t){this.data.x=t}},{key:"y",get:function(){return this.data.y},set:function(t){this.data.y=t}}]),e}(e)),t,function(t){return t.children});return f.update(),f.data}function o(){var t=r("nodeSize"),e=r("spacing");return function(n){function r(t){return h(this,r),a(this,(r.__proto__||Object.getPrototypeOf(r)).call(this,t))}return c(r,n),l(r,[{key:"copy",value:function(){var t=u(this.constructor,this,function(t){return t.children});return t.each(function(t){return t.data=t.data.data}),t}},{key:"spacing",value:function(t){return e(this,t)}},{key:"size",get:function(){return t(this)}},{key:"nodes",get:function(){return this.descendants()}},{key:"xSize",get:function(){return this.size[0]}},{key:"ySize",get:function(){return this.size[1]}},{key:"top",get:function(){return this.y}},{key:"bottom",get:function(){return this.y+this.ySize}},{key:"left",get:function(){return this.x-this.xSize/2}},{key:"right",get:function(){return this.x+this.xSize/2}},{key:"root",get:function(){var t=this.ancestors();return t[t.length-1]}},{key:"numChildren",get:function(){return this.hasChildren?this.children.length:0}},{key:"hasChildren",get:function(){return!this.noChildren}},{key:"noChildren",get:function(){return null===this.children}},{key:"firstChild",get:function(){return this.hasChildren?this.children[0]:null}},{key:"lastChild",get:function(){return this.hasChildren?this.children[this.numChildren-1]:null}},{key:"extents",get:function(){return(this.children||[]).reduce(function(t,e){return r.maxExtents(t,e.extents)},this.nodeExtents)}},{key:"nodeExtents",get:function(){return{top:this.top,bottom:this.bottom,left:this.left,right:this.right}}}],[{key:"maxExtents",value:function(t,e){return{top:Math.min(t.top,e.top),bottom:Math.max(t.bottom,e.bottom),left:Math.min(t.left,e.left),right:Math.max(t.right,e.right)}}}]),r}(n.prototype.constructor)}function u(t,e,n){return function e(r,i){var o=new t(r);Object.assign(o,{parent:i,depth:null===i?0:i.depth+1,height:0,length:1});var u=n(r)||[];return o.children=0===u.length?null:u.map(function(t){return e(t,o)}),o.children&&Object.assign(o,o.children.reduce(function(t,e){return{height:Math.max(t.height,e.height+1),length:t.length+e.length}},o)),o}(e,null)}return Object.assign(i,{nodeSize:function(t){return arguments.length?(e.nodeSize=t,i):e.nodeSize},spacing:function(t){return arguments.length?(e.spacing=t,i):e.spacing},children:function(t){return arguments.length?(e.children=t,i):e.children},hierarchy:function(t,n){var r=void 0===n?e.children:n;return u(o(),t,r)},dump:function(t){var e=r("nodeSize");return function t(n){return function(r){var i=n+" ",o=n+" ",u=r.x,h=r.y,l=e(r),c=r.children||[],a=0===c.length?" ":","+i+"children: ["+o+c.map(t(o)).join(o)+i+"],"+n;return"{ size: ["+l.join(", ")+"],"+i+"x: "+u+", y: "+h+a+"},"}}("\n")(t)}}),i}d.version="2.1.1";var p=function t(e){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0;return e.y=n,(e.children||[]).reduce(function(n,r){var i=f(n,2),o=i[0],u=i[1];t(r,e.y+e.ySize);var h=(0===o?r.lExt:r.rExt).bottom;return 0!==o&&y(e,o,u),[o+1,w(h,o,u)]},[0,null]),v(e),z(e),e},g=function t(e,n,r){void 0===n&&(n=-e.relX-e.prelim,r=0);var i=n+e.relX;return e.relX=i+e.prelim-r,e.prelim=0,e.x=r+e.relX,(e.children||[]).forEach(function(n){return t(n,i,e.x)}),e},v=function(t){(t.children||[]).reduce(function(t,e){var n=f(t,2),r=n[0],i=n[1],o=r+e.shift,u=i+o+e.change;return e.relX+=u,[o,u]},[0,0])},y=function(t,e,n){for(var r=t.children[e-1],i=t.children[e],o=r,u=r.relX,h=i,l=i.relX,c=!0;o&&h;){o.bottom>n.lowY&&(n=n.next);var a=u+o.prelim-(l+h.prelim)+o.xSize/2+h.xSize/2+o.spacing(h);(a>0||a<0&&c)&&(l+=a,x(i,a),m(t,e,n.index,a)),c=!1;var f=o.bottom,s=h.bottom;f<=s&&(o=E(o))&&(u+=o.relX),f>=s&&(h=b(h))&&(l+=h.relX)}!o&&h?X(t,e,h,l):o&&!h&&k(t,e,o,u)},x=function(t,e){t.relX+=e,t.lExtRelX+=e,t.rExtRelX+=e},m=function(t,e,n,r){var i=t.children[e],o=e-n;if(o>1){var u=r/o;t.children[n+1].shift+=u,i.shift-=u,i.change-=r-u}},b=function(t){return t.hasChildren?t.firstChild:t.lThr},E=function(t){return t.hasChildren?t.lastChild:t.rThr},X=function(t,e,n,r){var i=t.firstChild,o=i.lExt,u=t.children[e];o.lThr=n;var h=r-n.relX-i.lExtRelX;o.relX+=h,o.prelim-=h,i.lExt=u.lExt,i.lExtRelX=u.lExtRelX},k=function(t,e,n,r){var i=t.children[e],o=i.rExt,u=t.children[e-1];o.rThr=n;var h=r-n.relX-i.rExtRelX;o.relX+=h,o.prelim-=h,i.rExt=u.rExt,i.rExtRelX=u.rExtRelX},z=function(t){if(t.hasChildren){var e=t.firstChild,n=t.lastChild,r=(e.prelim+e.relX-e.xSize/2+n.relX+n.prelim+n.xSize/2)/2;Object.assign(t,{prelim:r,lExt:e.lExt,lExtRelX:e.lExtRelX,rExt:n.rExt,rExtRelX:n.rExtRelX})}},w=function(t,e,n){for(;null!==n&&t>=n.lowY;)n=n.next;return{lowY:t,index:e,next:n}};t.flextree=d,Object.defineProperty(t,"__esModule",{value:!0})}); //# sourceMappingURL=d3-flextree.min.js.map