33
44 Nodes are a flat list: { id, parent, value, color, label, detail, hatched }.
55 A node without `parent` is top level. Children are laid out inside their
6- parent's rectangle below a label strip, so every level stays readable, the
7- way Finder-like treemaps nest folders. Layout is the Bruls–Huizing–van Wijk
6+ parent's rectangle below a one-line header band that holds the parent's
7+ label, so every level stays readable, the way Finder-like treemaps nest
8+ folders. Label placement is computed here, not in the native view, so the
9+ no-overlap contract between a header and its children is unit tested. Layout is the Bruls–Huizing–van Wijk
810 squarified algorithm: rows along the shorter side, keeping aspect ratios
911 near 1. Everything is plain arithmetic so it is unit tested without views.
1012]]
1113
1214local Treemap = {}
1315
14- -- Nested levels get a label strip and a small inset only when there is room
15- -- for both; smaller parents show their children edge to edge.
16- Treemap .metrics = { labelStrip = 18 , inset = 2 , minNested = 36 , maxDepth = 3 , minCell = 2 }
16+ -- Nested levels get a header band and a small inset only when there is room
17+ -- for both; smaller parents show their children edge to edge and no label.
18+ -- `labelLine` matches the native 11 pt label line; the band is one line plus
19+ -- a small margin so the parent's text never reaches its first child.
20+ Treemap .metrics = { labelStrip = 20 , inset = 2 , minNested = 36 , maxDepth = 3 , minCell = 2 ,
21+ labelInset = 5 , labelLine = 14 , labelMinWidth = 56 }
1722
1823local function worst (row , sum , side )
1924 local largest , smallest = 0 , math.huge
@@ -85,6 +90,28 @@ local function childrenOf(nodes)
8590 return children , roots
8691end
8792
93+ -- A leaf shows its name on the first line and its detail on the second, each
94+ -- only when the tile has room for the line plus its insets.
95+ local function leafLabels (cell , metrics )
96+ local width = cell .w - 2 * metrics .labelInset
97+ if not cell .label or cell .w < metrics .labelMinWidth
98+ or cell .h < 2 * metrics .labelInset + metrics .labelLine then return end
99+ local x , y = cell .x + metrics .labelInset , cell .y + metrics .labelInset
100+ cell .labelFrame = { x = x , y = y , w = width , h = metrics .labelLine }
101+ if cell .detail and cell .h >= 2 * metrics .labelInset + 2 * metrics .labelLine then
102+ cell .detailFrame = { x = x , y = y + metrics .labelLine , w = width , h = metrics .labelLine }
103+ end
104+ end
105+
106+ -- A group with a header band draws name and detail on one line inside it
107+ -- (the native view right-aligns the detail); children start below the band.
108+ local function headerLabels (cell , metrics )
109+ if not cell .label or cell .w < metrics .labelMinWidth then return end
110+ cell .header = true
111+ cell .labelFrame = { x = cell .x + metrics .labelInset , y = cell .y + (metrics .labelStrip - metrics .labelLine ) / 2 ,
112+ w = cell .w - 2 * metrics .labelInset , h = metrics .labelLine }
113+ end
114+
88115-- Returns drawable cells, parents before their children, for a canvas of
89116-- `width` × `height` points.
90117function Treemap .layout (nodes , width , height , metrics )
@@ -96,22 +123,32 @@ function Treemap.layout(nodes, width, height, metrics)
96123 if a .value ~= b .value then return a .value > b .value end
97124 return tostring (a .id ) < tostring (b .id )
98125 end )
99- local values = {}
126+ local values , placed = {}, 0
100127 for index , node in ipairs (list ) do values [index ] = node .value end
101128 for index , rect in ipairs (Treemap .squarify (values , x , y , w , h )) do
102129 local node = list [index ]
103130 if rect .w >= metrics .minCell and rect .h >= metrics .minCell then
104- table.insert (cells , { id = node .id , x = rect .x , y = rect .y , w = rect .w , h = rect .h , depth = depth ,
105- color = node .color , label = node .label , detail = node .detail , hatched = node .hatched == true })
106- local kids = children [node .id ]
131+ local cell = { id = node .id , x = rect .x , y = rect .y , w = rect .w , h = rect .h , depth = depth ,
132+ color = node .color , label = node .label , detail = node .detail , hatched = node .hatched == true }
133+ table.insert (cells , cell )
134+ placed = placed + 1
135+ local kids , nested = children [node .id ], 0
136+ local banded = rect .h >= metrics .minNested + metrics .labelStrip
107137 if kids and depth + 1 < metrics .maxDepth
108138 and rect .w >= metrics .minNested and rect .h >= metrics .minNested then
109- local top = rect . h >= metrics . minNested + metrics . labelStrip and metrics .labelStrip or metrics .inset
110- place (kids , rect .x + metrics .inset , rect .y + top ,
139+ local top = banded and metrics .labelStrip or metrics .inset
140+ nested = place (kids , rect .x + metrics .inset , rect .y + top ,
111141 rect .w - 2 * metrics .inset , rect .h - top - metrics .inset , depth + 1 )
112142 end
143+ -- Children cover an unbanded parent, so it shows no label at all.
144+ if nested == 0 then
145+ leafLabels (cell , metrics )
146+ elseif banded then
147+ headerLabels (cell , metrics )
148+ end
113149 end
114150 end
151+ return placed
115152 end
116153 place (roots , 0 , 0 , width , height , 0 )
117154 return cells
0 commit comments