Skip to content

Instantly share code, notes, and snippets.

@alexeagle
Created March 6, 2019 06:38
Show Gist options
  • Save alexeagle/78fc6e4162b9d940daac0dc994d53a49 to your computer and use it in GitHub Desktop.
Save alexeagle/78fc6e4162b9d940daac0dc994d53a49 to your computer and use it in GitHub Desktop.
<!doctype html>
<title>webtreemap</title>
<style>
body {
font-family: sans-serif;
}
#treemap {
width: 800px;
height: 600px;
}
</style>
<div id='treemap'></div>
<script>const data = {"size":0}</script>
<script>window.webtreemap=function(e){function n(r){if(t[r])return t[r].exports;var i=t[r]={i:r,l:!1,exports:{}};return e[r].call(i.exports,i,i.exports,n),i.l=!0,i.exports}var t={};return n.m=e,n.c=t,n.d=function(e,t,r){n.o(e,t)||Object.defineProperty(e,t,{configurable:!1,enumerable:!0,get:r})},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,"a",t),t},n.o=function(e,n){return Object.prototype.hasOwnProperty.call(e,n)},n.p="",n(n.s=1)}([function(e,n,t){"use strict";function r(e){var n=document.createElement("style");n.innerText=h,e.appendChild(n)}function i(e){return e.classList.contains(u)}function o(e){for(var n=0,t=e;t=t.previousElementSibling;)i(t)&&n++;return n}function d(e){for(var n=[],t=e;t&&i(t);)n.unshift(o(t)),t=t.parentElement;return n.shift(),n}function a(e){return Math.round(e)+"px"}function s(e){var n={padding:e.padding||[14,3,3,3],caption:e.caption||function(e){return e.id||""},showNode:e.showNode||function(e,t,r){return t>20&&r>=n.padding[0]},showChildren:e.showChildren||function(e,n,t){return n>40&&t>40}};return n}function l(e,n,t){new f(n,t).render(e)}Object.defineProperty(n,"__esModule",{value:!0});var u="webtreemap-node",h="\n.webtreemap-node {\n cursor: pointer;\n position: absolute;\n border: solid 1px #666;\n box-sizing: border-box;\n overflow: hidden;\n background: white;\n transition: left .15s, top .15s, width .15s, height .15s;\n}\n\n.webtreemap-node:hover {\n background: #ddd;\n}\n\n.webtreemap-caption {\n font-size: 10px;\n text-align: center;\n}\n";n.isDOMNode=i,n.getAddress=d;var f=function(){function e(e,n){this.node=e,this.options=s(n)}return e.prototype.ensureDOM=function(e){if(e.dom)return e.dom;var n=document.createElement("div");if(n.className=u,this.options.caption){var t=document.createElement("div");t.className="webtreemap-caption",t.innerText=this.options.caption(e),n.appendChild(t)}return e.dom=n,n},e.prototype.selectSpan=function(e,n,t){for(var r=e[t].size,i=r,o=0,d=0,a=t;a<e.length;a++){var s=e[a].size;s<r&&(r=s),s>i&&(i=s);var l=o+s,u=Math.max(i*n*n/(l*l),l*l/(r*n*n));if(d&&u>d)break;d=u,o=l}return{end:a,sum:o}},e.prototype.layoutChildren=function(e,n,t,r){var i=e.size,o=e.children;if(o){var d=-1,s=-1,l=t-1,u=r-1,h=this.options.padding;s+=h[0],h[1]&&(l-=h[1]+1),u-=h[2],d+=h[3];var f=0;if(this.options.showChildren(e,l-d,u-s)){var c=Math.sqrt(i/((l-d)*(u-s))),p=d,v=s;e:for(var m=0;m<o.length;){p=d;var g=c*(l-d),w=this.selectSpan(o,g,m),b=w.end,y=w.sum;if(y/i<.1)break;var z=y/g,x=Math.round(z/c)+1;for(f=m;f<b;f++){var M=o[f],C=M.size,O=C/z,E=Math.round(O/c)+1;if(!this.options.showNode(M,E-0,x-0))break e;var _=null==M.dom,N=this.ensureDOM(M),k=N.style;k.left=a(p),k.width=a(E-0),k.top=a(v),k.height=a(x-0),_&&e.dom.appendChild(N),this.layoutChildren(M,n+1,E,x),p+=E-1}v+=x-1,m=b}}for(;f<o.length&&o[f].dom;f++)o[f].dom.parentNode.removeChild(o[f].dom),o[f].dom=void 0}},e.prototype.render=function(e){var n=this;r(e);var t=this.ensureDOM(this.node),o=e.offsetWidth,a=e.offsetHeight;t.onclick=function(e){for(var t=e.target;!i(t);)if(!(t=t.parentElement))return;var r=d(t);n.zoom(r)},t.style.width=o+"px",t.style.height=a+"px",e.appendChild(t),this.layoutChildren(this.node,0,o,a)},e.prototype.zoom=function(e){for(var n=this.node,t=this.options.padding,r=t[0],i=t[1],o=t[2],d=t[3],s=n.dom.offsetWidth,l=n.dom.offsetHeight,u=0,h=e;u<h.length;u++){var f=h[u];if(s-=d+i,l-=r+o,!n.children)throw new Error("bad address");for(var c=0,p=n.children;c<p.length;c++){var v=p[c];v.dom&&(v.dom.style.zIndex="0")}n=n.children[f];var m=n.dom.style;m.zIndex="1",m.left=a(d-1),m.width=a(s),m.top=a(r-1),m.height=a(l)}this.layoutChildren(n,0,s,l)},e}();n.TreeMap=f,n.render=l},function(e,n,t){"use strict";Object.defineProperty(n,"__esModule",{value:!0});var r=t(2);n.flatten=r.flatten,n.rollup=r.rollup,n.sort=r.sort,n.treeify=r.treeify;var i=t(0);n.render=i.render},function(e,n,t){"use strict";function r(e){for(var n={size:0},t=0,r=e;t<r.length;t++)for(var i=r[t],o=i[0],d=i[1],a=o.replace(/\/$/,"").split("/"),s=n;a.length>0;)!function(){var e=a.shift();s.children||(s.children=[]);var n=s.children.find(function(n){return n.id===e});if(n||(n={id:e,size:0},s.children.push(n)),0===a.length){if(0!==n.size)throw new Error("duplicate path "+o+" "+n.size);n.size=d}s=n}();return n}function i(e,n){if(void 0===n&&(n=function(e,n){return e+"/"+n}),e.children){for(var t=0,r=e.children;t<r.length;t++){i(r[t],n)}if(1===e.children.length){var o=e.children[0];e.id+="/"+o.id,e.children=o.children}}}function o(e){if(e.children){e.size=0;for(var n=0,t=e.children;n<t.length;n++){var r=t[n];o(r),e.size+=r.size}}}function d(e){if(e.children){for(var n=0,t=e.children;n<t.length;n++){d(t[n])}e.children.sort(function(e,n){return n.size-e.size})}}Object.defineProperty(n,"__esModule",{value:!0}),n.treeify=r,n.flatten=i,n.rollup=o,n.sort=d}]);</script>
<script>webtreemap.render(document.getElementById("treemap"), data, {
caption: function humanSizeCaption(n) {
var units = ['', 'k', 'm', 'g'];
var unit = 0;
var size = n.size;
while (size > 1024 && unit < units.length - 1) {
size = size / 1024;
unit++;
}
return (n.id || '') + " (" + size.toFixed(1) + units[unit] + ")";
},
});</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment