DwsChartTreemap
Shows how a whole splits into parts as rectangles, each as large as its value; groups hold their children's rectangles. The rectangles are laid out for the size the chart really has, so they stay close to square on any screen, their colors are theme colors, and hovering, focusing or tapping one shows its value and its share of the total and of its group.
API ReferenceBasic
Only ChartData is needed: one item per part - here the used space of a drive per folder. The largest goes top left and each rectangle shows its label and value where they fit. A single level needs no legend: the labels are on the rectangles (WithLegend null).
- Videos: 182, 41.3% of the total
- Photos: 96, 21.8% of the total
- Games: 74, 16.8% of the total
- Documents: 31, 7% of the total
- Music: 22, 5% of the total
- Applications: 18, 4.1% of the total
- Downloads: 12, 2.7% of the total
- Other: 6, 1.4% of the total
Show Code
TreemapChart.razor
<DwsChartTreemap Name='TreemapChartDemo_Basic' ChartData='@_storage' Title='Disk usage (GB)' />TreemapChart.razor.cs
private readonly DwsChartTreemapData _storage = new DwsChartTreemapData()
.Add("Videos", 182)
.Add("Photos", 96)
.Add("Games", 74)
.Add("Documents", 31)
.Add("Music", 22)
.Add("Applications", 18)
.Add("Downloads", 12)
.Add("Other", 6);Groups
AddGroup adds an item with children: its rectangle holds theirs, under a header with its label and total. Children take their group's color. GroupPadding sets the space around them.
- Asia: 3,687, 64.8% of the total
- India: 1,441, 25.3% of the total, 39.1% of Asia
- China: 1,425, 25.1% of the total, 38.6% of Asia
- Indonesia: 279, 4.9% of the total, 7.6% of Asia
- Pakistan: 245, 4.3% of the total, 6.6% of Asia
- Bangladesh: 174, 3.1% of the total, 4.7% of Asia
- Japan: 123, 2.2% of the total, 3.3% of Asia
- Americas: 825, 14.5% of the total
- United States: 342, 6% of the total, 41.5% of Americas
- Brazil: 217, 3.8% of the total, 26.3% of Americas
- Mexico: 129, 2.3% of the total, 15.6% of Americas
- Colombia: 52, 0.9% of the total, 6.3% of Americas
- Argentina: 46, 0.8% of the total, 5.6% of Americas
- Canada: 39, 0.7% of the total, 4.7% of Americas
- Africa: 708, 12.4% of the total
- Nigeria: 229, 4% of the total, 32.3% of Africa
- Ethiopia: 129, 2.3% of the total, 18.2% of Africa
- Egypt: 114, 2% of the total, 16.1% of Africa
- DR Congo: 106, 1.9% of the total, 15% of Africa
- Tanzania: 69, 1.2% of the total, 9.7% of Africa
- South Africa: 61, 1.1% of the total, 8.6% of Africa
- Europe: 468, 8.2% of the total
- Russia: 144, 2.5% of the total, 30.8% of Europe
- Germany: 84, 1.5% of the total, 17.9% of Europe
- United Kingdom: 68, 1.2% of the total, 14.5% of Europe
- France: 65, 1.1% of the total, 13.9% of Europe
- Italy: 59, 1% of the total, 12.6% of Europe
- Spain: 48, 0.8% of the total, 10.3% of Europe
Show Code
TreemapChart.razor
<DwsChartTreemap Name='TreemapChartDemo_Groups' ChartData='@_population' Title='Population by country (millions, approx.)' Height='440' />TreemapChart.razor.cs
// Approximate figures for 2024, in millions.
private readonly DwsChartTreemapData _population = new DwsChartTreemapData()
.AddGroup("Asia", group => group
.Add("India", 1441).Add("China", 1425).Add("Indonesia", 279)
.Add("Pakistan", 245).Add("Bangladesh", 174).Add("Japan", 123))
.AddGroup("Americas", group => group
.Add("United States", 342).Add("Brazil", 217).Add("Mexico", 129)
.Add("Colombia", 52).Add("Argentina", 46).Add("Canada", 39))
.AddGroup("Africa", group => group
.Add("Nigeria", 229).Add("Ethiopia", 129).Add("Egypt", 114)
.Add("DR Congo", 106).Add("Tanzania", 69).Add("South Africa", 61))
.AddGroup("Europe", group => group
.Add("Russia", 144).Add("Germany", 84).Add("United Kingdom", 68)
.Add("France", 65).Add("Italy", 59).Add("Spain", 48));Color by value
With ColorMode Value the color of a rectangle says a number instead of the item it belongs to: its ColorValue (here the growth of a market in percent; its Value when it has none) on a scale from the lowest to the highest one drawn. ColorScale takes the theme colors the scale runs through - one color runs from a pale tint to the color itself, several are blended evenly -, ColorMin and ColorMax fix its ends, ColorTitle and ColorValueFormat name and format the number. The legend becomes the scale (a gradient with its two ends); the tooltip and the accessible text of a rectangle name its color value; groups are neutral panels. Text stays black or white, whichever reads better on the rectangle.
- Europe: 1,030, 42.6% of the total
- Germany: 420, Growth in %: +2.1, 17.4% of the total, 40.8% of Europe
- France: 310, Growth in %: -1.4, 12.8% of the total, 30.1% of Europe
- Spain: 180, Growth in %: +6.3, 7.4% of the total, 17.5% of Europe
- Poland: 120, Growth in %: +9.8, 5% of the total, 11.7% of Europe
- Americas: 920, 38% of the total
- USA: 640, Growth in %: +3.7, 26.4% of the total, 69.6% of Americas
- Brazil: 150, Growth in %: -6.2, 6.2% of the total, 16.3% of Americas
- Canada: 130, Growth in %: +0.4, 5.4% of the total, 14.1% of Americas
- Japan: 260, Growth in %: -3.0, 10.7% of the total
- India: 210, Growth in %: +8.5, 8.7% of the total
Show Code
Razor
<div class='d-flex flex-wrap align-items-center gap-3 mb-3'>
<DwsSwitch Name='TreemapChartDemo_Color_Switch' @bind-Value='_colorByValue' Label='ColorMode Value' />
<DwsSwitch Name='TreemapChartDemo_Color_Fixed' @bind-Value='_colorFixedEnds' Label='ColorMin -10, ColorMax 10' />
<DwsSwitch Name='TreemapChartDemo_Color_Single' @bind-Value='_colorSingle' Label='One color (Primary)' />
</div>
<DwsChartTreemap Name='TreemapChartDemo_Color' ChartData='@_markets' Title='Revenue by market, colored by growth' Height='380'
ColorMode='@(_colorByValue ? DwsChartTreemapColorMode.Value : DwsChartTreemapColorMode.Palette)'
ColorScale='@(_colorSingle ? null : _growthScale)'
ColorMin='@(_colorFixedEnds ? -10 : null)' ColorMax='@(_colorFixedEnds ? 10 : null)'
ColorTitle='Growth in %' ColorValueFormat='+0.0;-0.0;0' />C#
private bool _colorByValue = true;
private bool _colorFixedEnds;
private bool _colorSingle;
private static readonly DwsColor[] _growthScale = [DwsColor.Error, DwsColor.Warning, DwsColor.Success];
// Value is the size of a rectangle, ColorValue the number its color says.
private readonly DwsChartTreemapData _markets = new()
{
Items =
[
new DwsChartTreemapDataItem("Europe", 0)
{
Children =
[
new DwsChartTreemapDataItem("Germany", 420) { ColorValue = 2.1 },
new DwsChartTreemapDataItem("France", 310) { ColorValue = -1.4 },
],
},
new DwsChartTreemapDataItem("Japan", 260) { ColorValue = -3.0 },
],
};Loading, caption and empty
IsLoading shows a spinner in place of the drawing while the data is on its way. Caption puts a sentence under the chart - what it shows, the source - and screen readers describe the drawing with it. EmptyContent replaces the empty text when there is nothing to draw; without it NoDataText or the localized default is shown, as in the last example.
- Videos: 182, 41.3% of the total
- Photos: 96, 21.8% of the total
- Games: 74, 16.8% of the total
- Documents: 31, 7% of the total
- Music: 22, 5% of the total
- Applications: 18, 4.1% of the total
- Downloads: 12, 2.7% of the total
- Other: 6, 1.4% of the total
Used disk space per folder, in GB.
Show Code
TreemapChart.razor
<DwsSwitch Name='TreemapChartDemo_States_Loading' @bind-Value='_statesLoading' Label='IsLoading' Class='mb-2' />
<DwsChartTreemap Name='TreemapChartDemo_States_Chart' ChartData='@_storage' IsLoading='@_statesLoading' Caption='Used disk space per folder, in GB.' />
<!-- Nothing to draw: your own content -->
<DwsChartTreemap Name='TreemapChartDemo_States_EmptyContent' ChartData='null' Class='mt-3'>
<EmptyContent>
<span>This drive is empty.</span>
</EmptyContent>
</DwsChartTreemap>
<!-- Nothing to draw, no EmptyContent: NoDataText or the localized default -->
<DwsChartTreemap Name='TreemapChartDemo_Empty' ChartData='null' />TreemapChart.razor.cs
private readonly DwsChartTreemapData _storage = new DwsChartTreemapData()
.Add("Videos", 182)
.Add("Photos", 96)
.Add("Games", 74)
.Add("Documents", 31)
.Add("Music", 22)
.Add("Applications", 18)
.Add("Downloads", 12)
.Add("Other", 6);
private bool _statesLoading;Width and height
Width, Height, MinWidth, MaxWidth, MinHeight and MaxHeight size the chart in pixels, caption and legend included; the rectangles share what is left. Without a height the drawing is 360px high. TileGap sets the space between rectangles. Drag the corner of the box: this one is 320px high and keeps between 240px and 720px wide, and the rectangles are laid out again for every size.
- Videos: 182, 41.3% of the total
- Photos: 96, 21.8% of the total
- Games: 74, 16.8% of the total
- Documents: 31, 7% of the total
- Music: 22, 5% of the total
- Applications: 18, 4.1% of the total
- Downloads: 12, 2.7% of the total
- Other: 6, 1.4% of the total
Show Code
TreemapChart.razor
<div class='treemap-demo-resizable'>
<DwsChartTreemap Name='TreemapChartDemo_Size' ChartData='@_storage' Height='320' MinWidth='240' MaxWidth='720' TileGap='4' WithLegend='false' />
</div>
<style>
.treemap-demo-resizable {
resize: horizontal;
overflow: auto;
width: 100%;
min-width: 240px;
max-width: 100%;
padding: 0.5rem;
border: 1px dashed var(--secondary);
}
</style>TreemapChart.razor.cs
private readonly DwsChartTreemapData _storage = new DwsChartTreemapData()
.Add("Videos", 182)
.Add("Photos", 96)
.Add("Games", 74)
.Add("Documents", 31)
.Add("Music", 22)
.Add("Applications", 18)
.Add("Downloads", 12)
.Add("Other", 6);Interface-Description
| Name | Typ | Parameters | Description |
|---|---|---|---|
| IsItemVisible | bool | int topIndex | Whether the top-level item at this position (of the level shown) is drawn (not hidden through the legend). |
| DrillIntoAsync | Task | DwsChartTreemapDataItem item | Makes an item of ChartData that has children the root, whatever DrillDown says. Keyboard focus stays where it is. Does nothing for an item without children or one that is not in the data. |
| DrillUpAsync | Task | Goes one level up, to the parent of CurrentRoot. Does nothing at the top. | |
| DrillToRootAsync | Task | Shows the whole data again. | |
| CurrentRoot | DwsChartTreemapDataItem? | Property: the item the chart is drilled into, null at the top. CurrentRootPath lists the items from the top level down to it. | |
| GetDataAsCsv | string | CultureInfo? culture | The data the chart was given as CSV text (RFC 4180, CRLF, a header line): one row per mark in data order, the values as passed in. Null writes numbers the invariant way with commas between the cells; a culture writes them its way with its list separator. Text a spreadsheet would run as a formula gets an apostrophe in front. |
| ExportDataAsync | Task | string? fileName, CultureInfo? culture | Downloads GetDataAsCsv() as a .csv file (UTF-8 with a byte order mark). Null names the file after the chart's title, else its Name. |
| Name | Typ | Default | Description |
|---|---|---|---|
| ChartData | DwsChartTreemapData? | null | The items: Items (each with a Label, a Value, an optional Color, Tag and Children). Add(label, value, color, tag) chains items, AddGroup(label, group => group.Add(...), color, tag) adds a group. A group's value is the sum of its children's; two levels are drawn, deeper children count towards their parent. A negative value, NaN or infinity is drawn as 0 with a warning; items of 0 are not drawn. Null, or nothing with a value, shows NoDataText. |
| Title | string? | null | Caption above the chart, also its accessible name. |
| NoDataText | string? | null | Text shown when there is nothing to draw, also when every item is hidden through the legend. Null uses the localized "No data to display". |
| EmptyContent | RenderFragment? | null | Shown instead of the drawing when there is nothing to draw; null shows the localized empty text. |
| IsLoading | bool | false | The data is still loading: a spinner instead of the drawing. |
| Caption | string? | null | A sentence under the drawing (what it shows, the source); the drawing is described by it (aria-describedby). |
| Width | int? | null | Width in px. Never wider than the container unless MinWidth asks for more. Null fills the container. |
| Height | int? | null | Height in px, caption and legend included; the rectangles share what is left. Null gives the drawing 360px. |
| MinWidth | int? | null | Smallest width in px; the only size that may make the chart wider than its container. |
| MaxWidth | int? | null | Largest width in px, and never more than the container. |
| MinHeight | int? | null | Smallest height in px; the drawing grows to fill it. |
| MaxHeight | int? | null | Largest height in px; the drawing shrinks to fit it, down to 120px. |
| TileGap | int | 2 | Space between the rectangles in px. |
| GroupPadding | int | 3 | Space between a group's border and its children in px. |
| Colors | IReadOnlyList<DwsColor>? | null | Theme colors of the top-level items, in the order of the data, repeated when there are more; children take their group's color and an item's own Color wins. Null or empty uses Primary, Info, Success, Warning, Accent and Error. Undefined entries are skipped. Text on a rectangle is black or white, whichever reads better. |
| ShowLabels | bool | true | Shows each rectangle's label in it, where it fits (at least 36 x 20 px); longer labels are cut off with an ellipsis. |
| ShowValues | bool | true | Shows each rectangle's value below its label where it fits, and each group's total in its header. |
| ShowGroupLabels | bool | true | Shows a header with each group's label above its children, where the group is tall enough. |
| ValueFormat | string | "#,0.##" | .NET number format for the values, in the current UI culture. An invalid format falls back to the default with a warning. |
| MinTileSize | int? | null | Smallest side of a rectangle in px. Items whose rectangle would be smaller - at least two among the children of one group, or among the top-level items without children - are drawn as one rectangle (OtherLabel) instead, which stays reachable by pointer, keyboard and tooltip. Groups are never merged; top-level items that were merged keep their legend entries. Null draws every item. |
| OtherLabel | string? | null | Label of the rectangle that stands for the items below MinTileSize. Null uses the localized "Other (5)" with the number of items. The item handed to OnItemClick, OnItemHover and TooltipTemplate has IsOther = true, Index -1 and the merged items in MergedItems (largest first). |
| ColorMode | DwsChartTreemapColorMode | DwsChartTreemapColorMode.Palette | What the color of a rectangle says. Palette: which top-level item it belongs to (Colors). Value: a number on a color scale from the lowest to the highest one drawn - the item's ColorValue (DwsChartTreemapDataItem), or its Value when it has none; an "Other" rectangle takes the mean of the merged items' color values weighted by their size. The legend then is the scale, so items cannot be hidden through it, and groups are neutral panels. |
| ColorScale | IReadOnlyList<DwsColor>? | null | Theme colors the scale of ColorMode Value runs through, low to high. One color runs from a pale tint of it to the color itself; two or more are blended evenly. Null or empty uses Primary. |
| ColorMin | double? | null | Value at the low end of the color scale. Null uses the lowest color value drawn. |
| ColorMax | double? | null | Value at the high end of the color scale. Null uses the highest color value drawn. |
| ColorValueFormat | string? | null | .NET number format of the color values in the legend, the tooltip and the accessible text. Null uses ValueFormat. |
| ColorTitle | string? | null | What the color values are ("Growth"), shown above the scale and before the value in the tooltip. Null uses the localized "Color". |
| WithLegend | bool? | null | Shows a legend with each top-level item's (or group's) color and label. Hovering an entry fades the others (with HighlightOnHover). |
| LegendPosition | DwsChartLegendPosition | DwsChartLegendPosition.Bottom | Bottom, Top, Right or Left of the chart. Right and Left move below it on screens narrower than 576px. |
| ToggleOnLegendClick | bool | true | Clicking a legend entry hides its item or group, and the others fill the space; clicking it again shows it. The entries are toggle buttons (aria-pressed). |
| ShowTooltip | bool | true | Shows the label, group, value, share of the total and share of the group of the hovered or focused rectangle. Escape hides it until the next rectangle. |
| TooltipTemplate | RenderFragment<DwsChartTreemapItem>? | null | Content of the tooltip. Null shows the default detail. |
| HighlightOnHover | bool | true | Fades the other rectangles while one is hovered or focused, and the other items while the pointer is on a legend entry. |
| DrillDown | bool? | null | Clicking a rectangle that has children of its own (or Enter / Space on it), or a group's header, makes it the root: the chart shows its children and theirs, to any depth. Backspace goes one level up; Escape closes the tooltip first and goes up when none is open. Null switches it on when the data has more than two levels (charts with one or two levels stay as they were), true also lets the headers of two-level data drill, false never drills. |
| ShowBreadcrumb | bool | true | Shows the breadcrumb above the chart while it is drilled into an item: every level above the current one is a button. |
| RootLabel | string? | null | Text of the breadcrumb's first entry, which leads back to the whole data. Null uses Title, or the localized "All". |
| Name | Typ | Description |
|---|---|---|
| OnRootChanged | EventCallback<DwsChartTreemapRootChangedEventArgs> | Raised when the chart is drilled into an item or back out, by the user or a method: Root (null at the top) and Path (the breadcrumb). Not raised when new ChartData resets the chart to its top. |
| OnItemClick | EventCallback<DwsChartTreemapItem> | Raised when a rectangle is clicked, tapped or activated with Enter/Space. With a handler the rectangles become buttons in the tab order, largest first. Not raised while disabled or in permission select mode (a click then selects the chart). |
| OnItemHover | EventCallback<DwsChartTreemapItem?> | Raised when the pointer or focus enters a rectangle, and with null when neither is on one any more: also when the chart stops reacting (disabled, permission select mode) or new data or the legend no longer draws it. |
| OnLegendItemClick | EventCallback<DwsChartTreemapItem> | Raised when a legend entry is clicked, after its item was hidden or shown (with ToggleOnLegendClick). With a handler the entries are buttons even without ToggleOnLegendClick. |
| Name | Typ | Default | Description |
|---|---|---|---|
| Element | ElementReference | The associated web component. May be null if accessed before the component is rendered. | |
| ComponentGuid | Guid | Unique identifier for the component. | |
| TypeString | string | The name of the component type, obtained from reflection. | |
| Authorized_RegisterEnabled | bool | false | Indicates whether this component is enabled through a permission registry. |
| Authorized_RegisterSelected | bool | false | Indicates whether this component is selected in permission selection mode. |
| GetInitializedState | bool | false | Returns whether the component is initialized. |
| GetRenderModeState | DwsRenderMode | Returns the current render mode. | |
| GetComponentState | DwsComponentStateType | Returns the current component state. | |
| GetIsInteractiveState | bool | false | Returns whether the component is interactive. |
| GetIsPrerenderingState | bool | false | Returns whether the component is prerendering. |
| GetComponentLoadingState | bool | false | Returns whether the component is loading. |
| IsFormBusy | bool | false | Whether the DwsForm (or DwsFormBusyRegion) around the component is locked because its submit handler is running. The state itself is the cascaded FormBusyState (IDwsFormBusyState). |
| Name | Typ | Parameters | Description |
|---|---|---|---|
| DebugInfo | void | Logs debug information about the component. | |
| UpdateState | void | Updates the component's state and triggers StateHasChanged. | |
| UpdateStateAsync | Task | Asynchronously updates the component's state and triggers StateHasChanged. | |
| Dispose | void | Disposes the component and releases resources. | |
| FocusAsync | ValueTask | bool containedInScrollableList | Sets focus to the component's element. |
| Name | Typ | Default | Description |
|---|---|---|---|
| Name | string | Required unique identifier for the component within its container. | |
| Visible | bool | true | Determines if the component is visible in the UI. |
| Disabled | bool | false | Disables the form control, ensuring it doesn't participate in form submission. |
| ComponentNameSpace | string | The namespace for permission-based access control. | |
| PermissionKey | string? | null | A stable key that takes the place of Name in the generated permission namespace (Page.PermissionKey): the stored permission survives a rename, and components that share a key share one permission entry. Null: keyed by Name. Read once, when the component registers; an explicit ComponentNameSpace wins. |
| Class | string | Optional CSS class names. | |
| Style | string | Optional in-line styles. | |
| Tag | object | An optional user-defined object to store any additional data with the component. | |
| BackReference | DwsReference | A reference to the enclosing component. | |
| ID | string | The HTML ID attribute for the component. | |
| AdditionalAttributes | Dictionary<string, object> | Additional HTML attributes for the component's root element. | |
| IgnorePermissionChecks | bool | false | Bypasses all permission checks for this component. |
| IgnorePermissionSelection | bool | false | Ignore permission selection mode for this component. |
| IgnoreFormBusy | bool | false | Takes the component out of the submit lock of the DwsForm (or DwsFormBusyRegion) around it: IsFormBusy stays false for it. |
| Name | Typ | Description |
|---|---|---|
| OnInitializedCallbackStarted | EventCallback | Callback when OnInitialized starts. |
| OnInitializedCallback | EventCallback | Callback during OnInitialized. |
| OnInitializedCallbackCompleted | EventCallback | Callback when OnInitialized completes. |
| OnInitializedAsyncCallbackStarted | EventCallback | Callback when OnInitializedAsync starts. |
| OnInitializedAsyncCallback | EventCallback | Callback during OnInitializedAsync. |
| OnInitializedAsyncCallbackCompleted | EventCallback | Callback when OnInitializedAsync completes. |
| OnIsComponentLoadingChanged | EventCallback<bool> | Callback when IsComponentLoading changes. |
| OnCurrentRenderModeChanged | EventCallback<DwsRenderMode> | Callback when CurrentRenderMode changes. |
| OnComponentStateChanged | EventCallback<DwsComponentStateType> | Callback when ComponentState changes. |
