Skip to content

Commit 91bbc2d

Browse files
authored
Merge pull request #43 from corepunch/claude/infallible-einstein-783a57
Stop treemap group labels overlapping their children
2 parents 91269fd + c630527 commit 91bbc2d

6 files changed

Lines changed: 153 additions & 26 deletions

File tree

‎docs/PROJECT_REFERENCE.md‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1006,7 +1006,10 @@ other sectors. Hit testing uses the same geometry that drew the arcs
10061006
`<Treemap>` draws a squarified treemap from `<TreemapNode id parent value
10071007
color label detail hatched />` records (`lua/ui/treemap.lua` lays it out; the
10081008
native `LuaTreemapView` only paints). Children nest inside their parent below
1009-
a label strip; colors are muted semantic colors that lighten with depth,
1009+
a one-line header band holding the parent's label and right-aligned detail; a
1010+
parent too short for the band shows no label. The layout emits each cell's
1011+
`labelFrame`/`detailFrame` and omits labels that would not fit, so labels never
1012+
overlap. Colors are muted semantic colors that lighten with depth,
10101013
`hatched` marks reclaimable space with diagonal lines, and the selected or
10111014
hovered cell gets an accent outline. `onSelect(id, clickCount)` and
10121015
`onHover(id)` report the deepest cell under the pointer. Give it a width and

‎lua/embedded/AppKit.lua‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2053,7 +2053,7 @@ end
20532053

20542054
--- Draws a squarified treemap from `TreemapNode` records.
20552055
---
2056-
--- Nested nodes (`parent`) are drawn inside their parent below a label strip.
2056+
--- Nested nodes (`parent`) are drawn inside their parent below a header band holding their label.
20572057
--- Colors are muted semantic colors that lighten with depth; `hatched` marks
20582058
--- reclaimable space. Clicking selects a cell and hovering outlines it.
20592059
--- @prop onSelect function optional. `onSelect(id, clickCount)`.

‎lua/ui/treemap.lua‎

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,22 @@
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

1214
local 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

1823
local function worst(row, sum, side)
1924
local largest, smallest = 0, math.huge
@@ -85,6 +90,28 @@ local function childrenOf(nodes)
8590
return children, roots
8691
end
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.
90117
function 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

‎src/appkit/charts.m‎

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,12 @@ static CGFloat treemap_number(NSDictionary *cell, NSString *key) {
199199
id value = cell[key];
200200
return [value respondsToSelector:@selector(doubleValue)] ? [value doubleValue] : 0;
201201
}
202+
static NSRect treemap_frame(NSDictionary *cell, NSString *key) {
203+
NSDictionary *frame = [cell[key] isKindOfClass:NSDictionary.class] ? cell[key] : nil;
204+
if (!frame) return NSZeroRect;
205+
return NSMakeRect(treemap_number(frame, @"x"), treemap_number(frame, @"y"),
206+
treemap_number(frame, @"w"), treemap_number(frame, @"h"));
207+
}
202208
- (void)drawRect:(NSRect)dirty {
203209
NSDictionary *nameAttributes = @{
204210
NSFontAttributeName: [NSFont systemFontOfSize:kTreemapLabelFontSize weight:NSFontWeightSemibold],
@@ -243,17 +249,29 @@ - (void)drawRect:(NSRect)dirty {
243249
[NSColor.controlAccentColor setStroke];
244250
[outline stroke];
245251
}
252+
// Label frames come from the Lua layout, which reserves a header band
253+
// for groups and omits labels that would not fit.
246254
NSString *label = [cell[@"label"] isKindOfClass:NSString.class] ? cell[@"label"] : nil;
247-
if (label.length && rect.size.width >= kTreemapLabelMinWidth && rect.size.height >= kTreemapLabelMinHeight) {
248-
NSRect text = NSInsetRect(rect, kTreemapLabelInset, kTreemapLabelInset);
249-
NSStringDrawingOptions options = NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingTruncatesLastVisibleLine;
250-
[label drawWithRect:NSMakeRect(NSMinX(text), NSMinY(text), text.size.width, kTreemapLabelLineHeight)
251-
options:options attributes:nameAttributes context:nil];
252-
NSString *detail = [cell[@"detail"] isKindOfClass:NSString.class] ? cell[@"detail"] : nil;
253-
if (detail.length && text.size.height >= 2 * kTreemapLabelLineHeight) {
254-
[detail drawWithRect:NSMakeRect(NSMinX(text), NSMinY(text) + kTreemapLabelLineHeight,
255-
text.size.width, kTreemapLabelLineHeight) options:options attributes:detailAttributes context:nil];
255+
NSString *detail = [cell[@"detail"] isKindOfClass:NSString.class] ? cell[@"detail"] : nil;
256+
NSRect labelFrame = treemap_frame(cell, @"labelFrame");
257+
NSRect detailFrame = treemap_frame(cell, @"detailFrame");
258+
NSStringDrawingOptions options = NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingTruncatesLastVisibleLine;
259+
if (label.length && !NSIsEmptyRect(labelFrame)) {
260+
// A group header shares its line: detail right-aligned, the name
261+
// truncating first so the size stays readable.
262+
if ([cell[@"header"] respondsToSelector:@selector(boolValue)] && [cell[@"header"] boolValue] && detail.length) {
263+
CGFloat detailWidth = ceil([detail sizeWithAttributes:detailAttributes].width);
264+
CGFloat nameWidth = [label sizeWithAttributes:nameAttributes].width;
265+
if (detailWidth + kTreemapHeaderDetailSpacing + MIN(nameWidth, labelFrame.size.width / 2) <= labelFrame.size.width) {
266+
[detail drawWithRect:NSMakeRect(NSMaxX(labelFrame) - detailWidth, NSMinY(labelFrame), detailWidth, labelFrame.size.height)
267+
options:options attributes:detailAttributes context:nil];
268+
labelFrame.size.width -= detailWidth + kTreemapHeaderDetailSpacing;
269+
}
256270
}
271+
[label drawWithRect:labelFrame options:options attributes:nameAttributes context:nil];
272+
}
273+
if (detail.length && !NSIsEmptyRect(detailFrame)) {
274+
[detail drawWithRect:detailFrame options:options attributes:detailAttributes context:nil];
257275
}
258276
}
259277
}

‎src/main.m‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,7 @@
131131
#define kTreemapHatchAlpha 0.22
132132
#define kTreemapSelectionWidth 2
133133
#define kTreemapLabelFontSize 11
134-
#define kTreemapLabelLineHeight 14
135-
#define kTreemapLabelInset 5
136-
#define kTreemapLabelMinWidth 56
137-
#define kTreemapLabelMinHeight 24
134+
#define kTreemapHeaderDetailSpacing 6
138135
#define kTableCellLoadingGap 4
139136
#define kTableCellLineSpacing 2
140137
#define kTableCellLevelTextWidth 38

‎tests/treemap.test.lua‎

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,78 @@ t.assertEqual(Treemap.hit(cells, 1000, 1000), nil, "points outside hit nothing")
5050
local tiny = Treemap.layout(nodes, 40, 30)
5151
for _, cell in ipairs(tiny) do t.expect(cell.depth == 0, "small parents do not nest children") end
5252

53+
-- Labels never overlap: a group's header label sits in the band above its
54+
-- children, and every label stays inside its own tile.
55+
local function intersects(a, b)
56+
return a.x < b.x + b.w and b.x < a.x + a.w and a.y < b.y + b.h and b.y < a.y + a.h
57+
end
58+
local function inside(frame, cell)
59+
return frame.x >= cell.x - 1e-9 and frame.y >= cell.y - 1e-9
60+
and frame.x + frame.w <= cell.x + cell.w + 1e-9 and frame.y + frame.h <= cell.y + cell.h + 1e-9
61+
end
62+
local nested = {
63+
{id = "dev", value = 45, label = "Developer", detail = "45.4 GB"},
64+
{id = "ios", value = 22, label = "iOS Files", detail = "21.8 GB"},
65+
{id = "xcode", parent = "dev", value = 30, label = "Xcode", detail = "28.3 GB"},
66+
{id = "pkg", parent = "dev", value = 15, label = "Package managers", detail = "3.6 GB"},
67+
{id = "support", parent = "xcode", value = 12, label = "Device support", detail = "11.5 GB"},
68+
{id = "derived", parent = "xcode", value = 8, label = "DerivedData", detail = "8.0 GB"},
69+
{id = "sims", parent = "xcode", value = 10, label = "Simulator devices", detail = "11.2 GB"},
70+
{id = "backups", parent = "ios", value = 16, label = "Backups", detail = "16.4 GB"},
71+
{id = "updates", parent = "ios", value = 5, label = "Updates", detail = "5.4 GB"},
72+
{id = "npm", parent = "pkg", value = 1, label = "npm", detail = "1.4 GB"},
73+
}
74+
local function checkLabels(width, height)
75+
local laid = Treemap.layout(nested, width, height)
76+
local cellById, parentOf = {}, {}
77+
for _, node in ipairs(nested) do parentOf[node.id] = node.parent end
78+
for _, cell in ipairs(laid) do cellById[cell.id] = cell end
79+
local headers = 0
80+
for _, cell in ipairs(laid) do
81+
for _, key in ipairs({"labelFrame", "detailFrame"}) do
82+
if cell[key] then
83+
t.expect(inside(cell[key], cell), cell.id .. " " .. key .. " stays inside its tile at " .. width .. "x" .. height)
84+
end
85+
end
86+
if cell.header then headers = headers + 1 end
87+
-- Walk every ancestor: no descendant label may cross a header label.
88+
local ancestor = cellById[parentOf[cell.id]]
89+
while ancestor do
90+
t.expect(ancestor.labelFrame == nil or ancestor.header, ancestor.id .. " draws a header, not leaf lines, above children")
91+
t.expect(intersects(cell, ancestor) and cell.y >= ancestor.y, cell.id .. " nests inside " .. ancestor.id)
92+
if ancestor.labelFrame then
93+
t.expect(not intersects(cell, ancestor.labelFrame),
94+
cell.id .. " tile stays below " .. ancestor.id .. " header at " .. width .. "x" .. height)
95+
for _, key in ipairs({"labelFrame", "detailFrame"}) do
96+
if cell[key] then
97+
t.expect(not intersects(cell[key], ancestor.labelFrame),
98+
cell.id .. " " .. key .. " clears " .. ancestor.id .. " header at " .. width .. "x" .. height)
99+
end
100+
end
101+
end
102+
ancestor = cellById[parentOf[ancestor.id]]
103+
end
104+
end
105+
return laid, cellById, headers
106+
end
107+
local _, big, headers = checkLabels(900, 540)
108+
t.expect(headers >= 2, "large groups draw header labels")
109+
t.expect(big.xcode.header and big.support.labelFrame, "nested headers and their children are both labelled")
110+
t.expect(big.support.y >= big.xcode.labelFrame.y + big.xcode.labelFrame.h, "children start below the header band")
111+
t.expect(big.support.detailFrame ~= nil, "roomy leaves show their detail line")
112+
t.assertEqual(big.xcode.detailFrame, nil, "a header draws its detail on its own line, not below it")
113+
checkLabels(400, 260)
114+
checkLabels(160, 90)
115+
local _, cramped = checkLabels(60, 60)
116+
for _, cell in pairs(cramped) do
117+
if cell.depth > 0 then
118+
t.assertEqual(cell.labelFrame, nil, "children of an unbanded parent are too small to label")
119+
end
120+
end
121+
for _, cell in ipairs(Treemap.layout(nested, 50, 20)) do
122+
t.assertEqual(cell.labelFrame, nil, "tiles too small for a label line show none")
123+
end
124+
53125
-- The native view lays out on resize and reports clicks and hovers.
54126
local selected, hovered
55127
local view, refs = xml.render([[<Treemap id="map" width="400" height="300">

0 commit comments

Comments
 (0)