Treemap
A treemap shows how a total splits into parts by area, when there are too many parts for a pie. Each item is a rectangle sized by its value, nested inside its group.
From paths
Give TreemapSeries each item's path from the outermost group as XValue, here
"Asia/India". A group's value is the sum of its items, and its name sits in a header when there is room. Colors and
legend items follow the top-level groups; click one in the legend to leave it out. GroupSmallItems gathers the
smallest countries of each continent into "Other". Hover a rectangle for its share of the continent and of the world.
Click anywhere in a continent, even tiny Oceania, to drill into it: it fills the chart, its countries get their own colors,
and the path bar above (All › Asia) leads back up. (When items take clicks themselves, with OnPointClick
or Selection="ChartSelectionMode.Points", a group's header or frame drills instead.)
Code
<RioChart TItem="Country" Items="countries">
<TreemapSeries Name="Population" XValue="@(c => $"{c.Continent}/{c.Name}")" YValue="c => c.Millions"
Format="0 M" GroupSmallItems="0.03" />
</RioChart>
@code {
record Country(string Continent, string Name, double Millions);
}From a parent-child list
With ParentValue, XValue is the item's name and the parent's name builds the path: folders on a disk,
an organisation, a bill of materials. Names must then be unique. Only items with a value get a rectangle; a folder's own size is
the sum of what is in it.
Deep trees get cramped, so VisibleLevels="2" draws two levels at a time: the top folders and what is in them.
Photos shows as one cell with its total; open Home and its years appear. The data keeps every level.
Code
<RioChart TItem="Folder" Items="folders">
<TreemapSeries Name="Disk usage" XValue="f => f.Name" ParentValue="f => f.Parent" YValue="f => f.Gigabytes" Format="0 GB"
VisibleLevels="2" />
</RioChart>
@code {
record Folder(string Name, string? Parent, double? Gigabytes); // Parent null: a top-level folder
}Good to know
- Zero and negative values are left out (the chart's screen reader text says how many); a treemap cannot show them as areas.
- A flat list (no separator in the paths) is a one-level treemap; each item gets its own color and the legend stays hidden.
- Names and values are shortened with "…" or hidden when a rectangle is too small; the tooltip always has them in full.
- Use
PathSeparatorwhen names contain "/", e.g.PathSeparator=" > ". - Drill down from code with
DrillDownAsync("Asia")(nullfor the whole tree), bind the group shown with@bind-TreemapPath(e.g. to keep it in the URL), and handleOnDrillDownto follow clicks, e.g. to load more detail. - Treemaps cannot be mixed with other series types, and only one treemap series is drawn.
In JSON definitions
"type": "treemap" with "path": ["continent", "country"] (the groups' fields, outermost first) and the value as
y; or a single x field that holds the path. Rows with the same path are added up.