Treemap
Treemap in UIToolbox shows values as boxes in a rectangle: the area of each box is its share of the total. An item with items of its own is a group, which shows them in its box under its name and its total. Use it for the disk use of a game build, a budget by department and team, the damage of each skill of each hero, or any total that splits into parts and parts of parts.


Video tutorial: Treemap. All videos: Video tutorials.
<tb:Treemap>
<tb:TreemapItem text="Textures">
<tb:TreemapItem text="Characters" value="820" />
<tb:TreemapItem text="Environment" value="1450" />
</tb:TreemapItem>
<tb:TreemapItem text="Audio" value="1850" color="#E07A3F" />
<tb:TreemapItem text="Scripts" value="45" />
</tb:Treemap>
<tb:Treemap format-string="0 MB" show-values="false" zoomable="false" />
var map = root.Q<Treemap>("storage");
// a group: its value is the sum of its items
var audio = map.AddItem("Audio");
audio.AddItem("Music", 640);
var voices = audio.AddItem("Voices", 980);
map.AddItem("Scripts", 45);
map.ItemClicked += item =>
Debug.Log($"{item.Text}: {item.Total} ({item.Percentage:0.#}%)");
// the boxes move to their new places
voices.Value = 1020;
The boxes go in rows that keep each one close to a square, the largest first. The treemap grows to fill its parent and is at least 120 pixels high.
Settings
TreemapItem, one box:
| Setting (UXML) | Type | Default | Does |
|---|---|---|---|
text |
string | "" | The name of the item, on its box and in its tooltip. |
value |
float | 0 | The size of the item: its box has the share of the area that its value has of the total. An item of 0 does not show, and a negative value counts as 0. A group ignores it and takes the sum of its items. |
color |
Color |
clear | The fill. Clear takes the next theme chart color (--tb-chart-1 to --tb-chart-8) for an item at the top, and a lighter shade of the color of its group for an item in a group. A group shows a darker shade of its color, so its items stand out on it. |
Treemap:
| Setting (UXML) | Type | Default | Does |
|---|---|---|---|
show-values |
bool | true | Shows the value under the name on each box that is at least 36 × 30 pixels, and the total in the header of each group. |
format-string |
string | 0.## |
The number format of the values on the boxes and in the tooltips. |
interactive |
bool | true | Lets the pointer and the keys highlight, select and open items. |
zoomable |
bool | true | Lets a double click and Enter zoom in on a group, which then fills the treemap. Code can zoom while it is off. |
animate |
bool | true | Moves the boxes to their new places in a short ease when a value changes or the treemap zooms. A zoom in grows the group to fill the treemap. A zoom out moves the view back: the group shrinks to its place while the boxes around it come in from past the edges. A new size of the treemap places them at once. |
A box shows its name when it is at least 36 × 16 pixels. A group shows its header when its box is at least twice as high as the header and 36 pixels wide; a smaller group shows only its items.
Properties and methods
| Member | Description |
|---|---|
ShowValues, FormatString, Interactive, Zoomable, Animate |
The settings of the treemap above. |
Treemap.AddItem(text, value) |
Adds an item at the top and returns it. |
Total |
The sum of the values of the items. |
SelectedItem |
The selected item, or null. An item of another treemap throws. |
HoveredItem |
The item under the pointer, or null. |
ZoomedItem, ZoomTo(item), ZoomOut() |
The group that fills the treemap, or null while it shows all the items. ZoomTo on an item that is not a group zooms in on its group, and null shows all the items. ZoomOut goes out one level. |
Text, Value, Color |
The settings of an item above. |
TreemapItem.AddItem(text, value) |
Adds an item to the item, which makes it a group, and returns the new item. |
TreemapItem.Total |
The value of the item, or for a group the sum of the values of its items. |
Percentage |
The share of the item of the total of the treemap, from 0 to 100. |
--tb-item-color |
In the style of an item: the fill of an item with a clear Color. The order is Color, then --tb-item-color, then the palette color or the shade. The property inherits, so an item in a group takes the value of its group unless a rule sets its own. |
DisplayColor |
The color the treemap gives the item: Color, or the palette color or the shade when it is clear. |
IsGroup, ParentItem, Treemap |
Whether the item has items, the group that holds it, and the treemap. |
Setting Value to the value it has changes nothing, so a binding that sets it each frame is free. The layout follows a change once a frame, so a loop that sets many values lays out once. A selection or a zoom stays while its item is in the treemap and has a value.
Events
| Event | Sent when |
|---|---|
ItemSelected(item) |
A click, a key or SelectedItem selects an item, or the selection is cleared (null). |
ItemHovered(item) |
The pointer moves onto an item, or off all of them (null). |
ItemClicked(item) |
An item is clicked, or Enter, Space or the submit button of a gamepad is pressed while an item is selected. |
ZoomChanged(item) |
The treemap zooms in on a group, or out to all the items (null). |
Mouse and keyboard
| Input | Does |
|---|---|
| Pointer over a box | Draws a thin frame around the box, and its tooltip shows its name, its value and its share of the total. |
| Click | Selects the box, or clears the selection when the box is selected. Sends ItemClicked. |
| Double click | Zooms in one level, on the group under the pointer, which then fills the treemap. A double click on the zoomed group, such as on its header, zooms out a level. |
| Arrow keys, D-pad, left stick | The first press selects the largest box; then the selection moves to the nearest box in that direction in the same group. At the edge the focus moves on. |
| Enter, Space, submit | Sends ItemClicked for the selected box. On a group, it also selects the largest item of the group, and zooms in on the group when zoomable is on. |
| Escape, cancel, Backspace | Steps out a level: from a box to its group, and out of the zoom when that group fills the treemap. On a box at the top it clears the selection; with nothing selected it zooms out. |
The treemap takes the focus; its border takes the accent color while it has it.
Performance
- Each box is an element with a header and two labels, which suits up to a few hundred boxes. For more, put the small values in groups or in an Other item.
- At rest the treemap does no work, and no scheduled item runs. The ease runs only while boxes move, and it allocates nothing, a zoom out included.
- A layout allocates nothing: the items sort in kept lists, and the text of a value is made once and kept while the value stays. A value that changes each frame makes one string for its new text while
show-valuesis on.
Styling
| Class | Element |
|---|---|
tb-treemap |
The treemap. It grows, is at least 120 pixels high, and its border shows the focus. |
tb-treemap--empty |
The treemap while its total is 0. |
tb-treemap__plot |
The area of the boxes. |
tb-treemap__item |
A box. tb-treemap__item--group is a group, and tb-treemap__item--on-light is a box with a light color, whose texts are dark. |
tb-treemap__item-header |
The name and the value of a box, or the header of a group. |
tb-treemap__item-label, tb-treemap__item-value |
The name and the value, in --tb-chart-text (white by default). |
tb-treemap__item-children |
The area of the items of a group. |
tb-treemap__hover, tb-treemap__selection |
The frame of the box under the pointer and the frame of the selected box. |
| Variable | On | Description |
|---|---|---|
--tb-treemap-header-height |
tb-treemap |
The height of the header of a group in pixels, written without a unit. 0 hides the headers. Default 18. |
--tb-treemap-spacing |
tb-treemap |
The gap between two boxes, and between a group and its items, in pixels, written without a unit. Default 2. |
Boxes that touch, with taller headers and a gold selection:
.storage-map {
--tb-treemap-spacing: 0;
--tb-treemap-header-height: 22;
}
.storage-map .tb-treemap__selection {
border-color: rgb(255, 210, 60);
}
In the UI Builder, a treemap without items shows a sample. The sample does not show in Play mode or in a build, and the UI Builder does not save it to the UXML file.
The TreemapDemo scene shows the items of a guild bank by category, the size of a game build with a breadcrumb of the zoom, the damage of each skill of a hero by element, and four looks. A status line under the treemap tells the selected box and the box under the pointer.