Prepare a hierarchy for nested circles, count only leaves, and distinguish proportional leaf area from the whitespace inside enclosing parents.
Circle packing: compare leaves without misreading parent circles
Containment communicates ancestry first
Circle packing nests child circles inside larger enclosing circles. The visible enclosure answers where a leaf belongs, while a leaf's area can encode a count. These meanings are easy to mix. A parent circle encloses child circles plus empty space, so its visible area is generally not a precise representation of the sum of its children's counts. Do not place a parent total on the same area scale as leaves and invite literal area comparison. Show exact totals in labels or a companion table. Use a full-path identifier for every node: Open under Dispatch and Open under Billing are different leaves. In a service queue ledger, count each ticket in exactly one terminal queue. If the same ticket can belong to multiple queues, change the measure to queue assignments or choose another view. Treemaps use space more efficiently, while sunbursts show ancestry in rings.
Calculate weights before drawing circles
Build totals upward from terminal leaves, never by adding a separately entered parent value to its children. A leaf with count 16 needs four times the area of a leaf with count four, which means twice the radius before any layout scaling. The worked code computes both prefix totals and target leaf radii, and verifies the root total. A packing algorithm then moves the circles to fit within their enclosing parents, often scaling them together. Padding and the arrangement of siblings introduce empty space. A stable label order and stable layout seed matter in recurring reports, or viewers will mistake a rearranged cluster for a changed relationship. Preserve leaf identifiers through layout updates. Grain and export manifests keep the geometric view tied to its ledger.
Small leaves need another route to inspection
If a leaf's rendered diameter is smaller than a usable touch or focus target, do not rely on the circle for discovery. Provide a searchable hierarchy table with full path, leaf count and parent share; let users focus a row and highlight the circle. Labels should not be forcibly clipped into tiny circles. Put full paths in the table and display only short names where there is enough room. A branch with hundreds of tiny leaves may need a drill-down action with an explicit breadcrumb back to the parent. Grouping them as Other is valid only within one parent and with a declared threshold; the original leaves must remain in the data export. Keyboard focus and text alternatives are part of the chart, not optional extras.
Choose the view for the comparison
A packed-circle diagram is good for exploring a hierarchy and spotting dominant leaves within a branch. It is weak for precise ranking of leaves scattered across different parents because people compare circle areas poorly and blank packing space interferes with apparent size. For rank questions, pair the diagram with a bar chart sorted by count. For proportional share across the entire hierarchy, a treemap may waste less space. For following a path from root to leaf, a sunburst can make depth more explicit. Keep the same top-level colors and comparable scale across updates, and state whether the geometry was recalculated after a filter. A filter that removes a branch should not silently make every surviving leaf look larger when the task is to compare before and after. Scale integrity and linked filters apply even without visible axes.
Worked contract
from collections import Counter
from math import isclose, pi, sqrt
def prepare_queue_pack(leaf_counts):
if not leaf_counts:
raise ValueError("leaf counts are required")
totals, seen = Counter(), set()
for path, count in leaf_counts:
if (not isinstance(path, tuple) or len(path) != 3 or
any(not isinstance(label, str) or not label.strip()
for label in path) or path in seen or
isinstance(count, bool) or not isinstance(count, int) or count <= 0):
raise ValueError("each unique leaf needs three labels and a positive count")
seen.add(path)
for depth in range(4):
totals[path[:depth]] += count
leaves = {path: sqrt(count / pi) for path, count in leaf_counts}
assert totals[()] == sum(leaf_counts[index][1]
for index in range(len(leaf_counts)))
return totals, leaves
queue_leaves = [
(("Operations", "Dispatch", "Open"), 16),
(("Operations", "Dispatch", "Closed"), 4),
(("Operations", "Returns", "Open"), 9),
(("Operations", "Returns", "Closed"), 7),
(("Finance", "Billing", "Open"), 13),
(("Finance", "Billing", "Closed"), 11),
]
totals, radii = prepare_queue_pack(queue_leaves)
assert totals[()] == 60
assert totals[("Operations",)] == 36
assert isclose(radii[("Operations", "Dispatch", "Open")] /
radii[("Operations", "Dispatch", "Closed")], 2)
assert isclose(pi * radii[("Finance", "Billing", "Open")] ** 2, 13)Performance and operating cost
For R leaf rows and fixed depth D, deriving prefix totals takes O(RD) time and O(N) space for N distinct nodes. Computing one target radius per leaf takes O(R) time and space. These are weights, not final coordinates. The packing step depends on the renderer and its padding/layout algorithm; it may alter absolute radii while preserving relative leaf areas. Do not use enclosing-parent pixel area as an exact sum because it includes blank regions. For a large tree, prepare a compact table and allow branch-by-branch exploration rather than drawing all leaves as subpixel circles.
Common Mistakes
- Counting a parent value again after already summing its leaves.
- Comparing enclosing parent-circle area as though empty packing space were data.
- Using equal radius instead of equal area for equal leaf counts.
- Merging leaves with the same short label under different parents.
- Making tiny circles the only path to discover or select small categories.
Validation drill
Prepare a three-level queue tree with 47 terminal categories. Reconcile each branch from leaves, compute target leaf areas, and create a companion table that allows a reader to find every small leaf even when its rendered circle cannot hold a label.
