Skip to content

codechu/treeviz-py

━━━━━━━━━━ c o d e c h u  ·  t r e e v i z ━━━━━━━━━━

      input                              output
   ┌────────────────────┐           ┌────────────────────┐
   │ /                  │           │ ┌─────────┬──────┐ │
   │ ├── photos  (450)  │    →      │ │ photos  │ src  │ │
   │ ├── src     (200)  │   layout  │ ├─────────┴──────┤ │
   │ ├── cache   (180)  │           │ │  cache  │ logs │ │
   │ └── logs    (120)  │           │ ├─────────┴──────┤ │
   └────────────────────┘           │ └────────────────┘ │
                                     per-node (x, y, w, h)

━━━━━━━━━ rectangles and arcs. you render. ━━━━━━━━━━

PyPI Python CI License: MIT

Squarified treemap + sunburst layouts — rectangles and arcs, you render.

codechu-treeviz

Squarified treemap and sunburst layout algorithms for hierarchical data. Pure Python, no GUI dependency — gives you rectangles and arcs and lets you render them with whatever toolkit you prefer (Cairo, SVG, Matplotlib, browser canvas, Pillow PNG).

Install

pip install codechu-treeviz

Python 3.10+. Pure stdlib + math, zero third-party deps.

Quick example

from codechu_treeviz import build_tree, TreemapStrategy

# 1. Walk a directory into a TreeNode
root = build_tree("~/Pictures")

# 2. Lay it out — writes (x, y, w, h) onto every node.rect
strategy = TreemapStrategy(min_frac=0.005)
strategy.layout(root, w=800, h=600)

# 3. Render however you like (Cairo, SVG, browser canvas, …)
for child in root.children:
    if child.rect is not None:
        x, y, w, h = child.rect
        print(child.path, (x, y, w, h))

# 4. Hit test for hover/click
node = strategy.hit_test(root, x=120, y=80)

Swap TreemapStrategy() for SunburstStrategy() and the same code renders a radial chart (rects become 7-tuples — see the API docs).

What you get

  • Squarified treemap — Bruls/Huijsen/van Wijk algorithm, aspect-ratio optimized rectangles.
  • Sunburst — circular hierarchical chart with concentric rings.
  • TreeNode builder — turn a (path, size) list into a hierarchical tree with cancel + progress callbacks.
  • Hit testing — find the node at any (x, y) coordinate.
  • "Other" bundling — collapse small slivers into one "(N items)" bucket.
  • Color palette — perceptually balanced default fill colors.

No rendering, no GUI dependency, bounded depth (TREEMAP_MAX_DEPTH = 40) so pathological nesting can't OOM you.

Read more

  • API reference — TreeNode, SizeProvider, VizStrategy, layout functions, hit-test, color helpers.
  • Recipes — disk-usage treemap, sunburst from a directory tree, custom SizeProvider, subclassing VizStrategy, hit-test plumbing.
  • Migration guide
  • Changelog

Family

Library Purpose
codechu-treedata N-ary tree data structures and algorithms
codechu-spark Unicode sparklines, mini bar charts, heatmaps
codechu-fmt Human-readable sizes, durations, rates
codechu-cli CLI primitives — colors, progress, prompts
codechu-color Color palettes, WCAG contrast, color-blind variants

Full ecosystem: github.com/codechu.

Credits

  • Squarified treemap algorithm by Bruls, Huijsen, van Wijk (2000).
  • Sunburst layout following Stasko & Zhang radial visualizations.
  • Inspiration from squarify — single-algorithm; codechu-treeviz extends to multiple strategies.

License

MIT — see LICENSE.

Part of Codechu.

About

Tree visualization primitives for Python: squarified treemap + sunburst layouts. Pure-Python, renderer-agnostic.

Topics

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages