Three.js 3D Treemap (Squarified, Drill-Down)

Hierarchical data as a 3D squarified treemap in Three.js, showing a synthetic code repository with area as file size, height as recent changes, colour by top-level folder, nesting depth control and click to drill down.

Key topics: treemap, squarified treemap, hierarchical data visualization, three.js treemap, disk usage, drill down

What this code does

- Squarified Algorithm: items are added to a strip while the worst aspect ratio improves, then the strip is laid out and the remaining rectangle shrinks (Bruls, Huizing & van Wijk).
- Nesting: each folder's rectangle is recursively subdivided with padding up to the chosen depth.
- Extrusion: height encodes a second metric (recent changes per file), showing hot spots that area alone hides.
- Drill-Down: clicking re-roots the treemap at the folder you clicked, and Up returns to the parent.
- Rendering: every tile is one instance of a unit box, scaled and coloured per cell.
- Controls: height metric, nesting depth, padding and up one level.

Continue Learning

JavaScript Implementation

ES6+
// Squarified treemap: add items to a row while the worst aspect ratio improves, then lay the row out

function squarify (items, x, y, w, h, out) {
  const scale = (w * h) / sum(items.map(i => i.size))
  let rest = items.map(it => ({ it, a: it.size * scale })).sort((a, b) => b.a - a.a)
  while (rest.length) {
    const row = takeRowMinimisingWorstAspect(rest, Math.min(w, h))
    const thick = sum(row.map(q => q.a)) / (w >= h ? h : w)
    row.forEach(q => out.push(placeAlong(q, thick)))   // vertical strip if w ≥ h, else horizontal
    if (w >= h) { x += thick; w -= thick } else { y += thick; h -= thick }
    rest = rest.slice(row.length)
  }
}
// Each cell → an instanced box: scale (w, height, h), colour by top-level folder