Skip to Content
Onevra

Treemap

Nested sizes as tiles, such as disk usage, drilled into by clicking.

Loading the live example…

treemap.go
import (
	"fmt"

	"github.com/egoist/mygo/ui"
	"github.com/oneo-me/onevra/source/components/treemap"
)

type diskUsage struct {
	state treemap.State // the drilled branch and active tile
}

var diskNodes = []treemap.Node{
	{ID: "photos", Label: "Photos", Children: []treemap.Node{
		{ID: "photos/2025", Label: "2025", Value: 962},
		{ID: "photos/2026", Label: "2026", Value: 382},
		{ID: "photos/raw", Label: "RAW", Value: 1480},
	}},
	{ID: "music", Label: "Music", Value: 640},
	{ID: "video", Label: "Video", Children: []treemap.Node{
		{ID: "video/films", Label: "Films", Value: 2210},
		{ID: "video/clips", Label: "Clips", Value: 410},
	}},
	{ID: "apps", Label: "Applications", Value: 880},
	{ID: "docs", Label: "Documents", Value: 120},
}

func (d *diskUsage) View(c *ui.Context) {
	ui.Column(c).Fill().Padding(c.Theme().Space(4)).Children(func() {
		// Click a tile to drill into it; the breadcrumbs lead back.
		treemap.View(c, &d.state, diskNodes, treemap.Options{
			Label:       "Disk usage",
			FormatValue: func(v float64) string { return fmt.Sprintf("%.1f GB", v/1000) },
		})
	})
}
Source

Each node is a tile sized by its value, or by the sum of its children. Clicking a tile drills into it, and the breadcrumbs lead back; data changes morph the tiles. The arrow keys move between tiles, Enter drills in and Escape goes up.

API

View

func View(c *ui.Context, state *State, nodes []Node, opts Options) ui.Element

View builds the treemap.

pkg.go.dev

Node

Node is one rectangle. ID must be nonempty, unique and stable across frames. A leaf's Value sizes it; a branch takes the total of its children, or its Value when larger, leaving the difference as unlabeled space. Nodes without a finite positive size are omitted. Caption is secondary text a large leaf shows below its size.

Measure is a second quantity, such as growth, that shades leaves when Options.MeasureLabel is set; branches report their leaves' size-weighted mean. A nonzero Color tints the node and its descendants; other nodes inherit their parent's, and top-level nodes take a palette color by order.

Style overrides how the node's own tile paints, and HoverStyle how it paints while the pointer is on it rather than on a descendant.

Prop

Type

pkg.go.dev

Options

Options supplies presentation and application-owned content.

Label names the chart and the first breadcrumb; Description guides assistive technology, such as through the keys. Height sets a fixed height for the breadcrumbs and tiles; zero fills the parent. HideHeader omits the breadcrumbs and total, so a host can drive State.Root itself. FormatValue formats sizes (nil uses numbers).

MeasureLabel, when set, names Node.Measure and shades leaves by it within MeasureDomain, or the leaves' range when that is empty. FormatMeasure formats it (nil uses numbers).

Clicking a tile of the shown level drills into it, and breadcrumbs lead back. Arrow keys move to the nearest tile in their direction, Home and End to the largest and smallest; Enter or Space drills in, and Escape or Backspace goes up a level. Opening a leaf calls Open on the UI thread.

Menu, when set, returns the builder of a node's context menu, or nil for a node without one. The menu opens where the deepest tile is right-clicked, without drilling or choosing it, and for the active tile from the menu key or Shift+F10.

As in charts, a tooltip follows the pointer for the deepest hovered node, or sits beside the active tile the keyboard chose while the view has the focus. Tip replaces its content, which by default shows the node's path, size, share, measure and caption. Empty replaces the tiles when nothing has a positive size (EmptyText, default "—", otherwise). AnimationDuration controls drilling and data transitions: zero uses 400 ms, negative disables them, and Reduced Motion and ui.Context.DisableAnimations always skip them.

Prop

Type

pkg.go.dev

State

State retains navigation between frames. Root is the branch drilled into, "" for the whole tree; an unknown Root, or one without children, resets to "". Active is the tile of the shown level the keyboard is on, "" for none. Keep one per view; its zero value is ready. Nil state uses element-local storage.

Prop

Type

pkg.go.dev

Path

func Path(nodes []Node, id string) []Node

Path returns the nodes from the top level down to the node with id, or nil when it is absent.

pkg.go.dev
Edit this page on GitHub

On this page