Prevent duplicated parent values, unstable tiles and tiny unreadable leaves.
Treemaps: validate hierarchy and leaf totals before assigning area
Area is a sum contract
A treemap divides a rectangle into nested areas proportional to a nonnegative measure. To compare storage used by product teams, assign each leaf one value and derive parent totals from those leaves. If a parent row already contains the total, summing it with its children doubles the area. Validate that every leaf has one parent, that the hierarchy has no cycle, and that the root total agrees with the source. Grain checks protect the basic unit before layout begins.
Labels cannot rescue tiny tiles
A rectangle's area is hard to estimate precisely, and small leaves cannot hold readable text. Use the treemap for broad composition and a sorted table or bar view for exact comparison. Group genuinely small categories into a named remainder only when their combined value and membership are reported. Do not simply omit leaves to make labels fit. Stacked bars often work better when the hierarchy has only one level or exact cross-category comparison matters.
Keep identity across versions
Many tiling algorithms can rearrange rectangles after a small value change. A user comparing two snapshots may see tiles move and mistake movement for a new category. Use stable IDs, a declared sort and a layout method chosen for the comparison task. If two versions must be compared, consider a fixed hierarchy with an accompanying difference bar; do not imply that a tile's new screen position has meaning. Shared-scale panels help only when the same root total basis is explicit.
Separate structure from measure
Zero-value leaves may be valid members of the hierarchy but have no visible area. Missing measures are different: they should be counted and surfaced outside the tiled display. A negative measure cannot be represented as area without a different visual contract. Tooltips should show path, leaf value, parent total and share; keyboard users need an ordered text tree or table. Accessible text can retain the small leaves that the drawing cannot label.
Worked contract
def hierarchy_totals(
parent_of: dict[str, str | None], leaf_values: dict[str, int]
) -> dict[str, int]:
internal = {parent for parent in parent_of.values() if parent is not None}
if set(leaf_values) != set(parent_of) - internal or any(value < 0 for value in leaf_values.values()):
raise ValueError("invalid leaf value")
if any(parent is not None and parent not in parent_of for parent in parent_of.values()):
raise ValueError("unknown parent")
for start in parent_of:
seen = set()
node = start
while node is not None:
if node in seen:
raise ValueError("cycle in hierarchy")
seen.add(node)
node = parent_of[node]
totals = {node: 0 for node in parent_of}
for leaf, value in leaf_values.items():
node = leaf
while node is not None:
totals[node] += value
node = parent_of[node]
return totals
tree = {"all": None, "support": "all", "mobile": "support",
"web": "support", "payments": "all"}
totals = hierarchy_totals(tree, {"mobile": 47, "web": 81, "payments": 31})
assert totals["support"] == 128 and totals["all"] == 159Performance and operating cost
For V nodes, L leaves and maximum depth H, cycle validation and ancestor accumulation take O((V + L)H) time and O(V + H) space for totals and one ancestor set. A deep or very large hierarchy should validate and sum in one postorder pass, O(V). Production layout adds sorting, tiling and label placement. Every terminal node needs a nonnegative measure for area encoding.
Common Mistakes
- Adding stored parent totals to leaf values and doubling displayed area.
- Treating zero-area leaves as nonexistent members.
- Reading a tile move between snapshots as a change in business structure.
- Dropping tiny categories without reporting the remainder.
Validation drill
Build a three-level storage hierarchy with 47 leaves. Insert one duplicate parent total, one missing measure and one cycle; reject each before rendering, then compare the resulting treemap with a sorted leaf table.
Read next
- Stacked bars: separate composition from volume and preserve denominators
- Aggregation grain and denominator: make every plotted mark auditable
- Accessible charts: encode meaning without relying on color alone
- Small multiples: compare groups with honest scales and facet order
