Skip to content

Treemap API

Hierarchical tiles sized by value, each optionally split into two named parts (e.g. realized vs untapped), with a mobile-friendly stack fallback - see the Treemap demo.

Import

ts
import "@michi-vz/wc/treemap-chart";
// <michi-vz-treemap-chart> is now defined
ts
import { mountTreemapChart } from "@michi-vz/core";

const chart = mountTreemapChart(el, props);

Props

PropTypeDefaultDescription
dataSet*TreemapNode[]Forest of nodes (each a leaf or a parent with children) sized by value and tiled to fill the area
titlestringOptional chart title rendered above the plot
paddingInnernumber1Gap between sibling tiles in px (default 1).
paddingTopnumber18Header strip height (px) reserved on parent tiles for their label in nested mode (default 18).
layout"squarify" | "stack" | "auto""squarify"Layout algorithm: "squarify" (treemap, default), "stack" (single-column, mobile-friendly), or "auto" (stack below `stackBreakpoint` width).
stackBreakpointnumberWidth (px) below which `layout: "auto"` switches to the stack layout (default 480).
splitLabels[string, string]["Filled", "Remaining"]Names of the two split parts (default ["Filled","Remaining"]).
splitOpacitynumber0.35Apparent colour strength of the remainder/untapped segment in [0,1] (default 0.35). Rendered as the solid colour under a white veil, so it reads as a lighter tint of the same hue on any background (light or dark) rather than depending on the backdrop.
showSplitbooleanRender the primary/remainder split. Defaults to auto-on when any leaf carries `partial`.
showLegendbooleanfalseRender a 2-swatch split legend (uses splitLabels).
minTileSharenumberFloor each leaf's tiling area to this percent of the largest leaf, so tiny tiles stay visible.
filter{ limit: number; sortingDir: "asc" | "desc" }Keep only the top-N leaves by value.
valueFormatter(n: number) => stringFormats numeric values shown in the default tooltip (defaults to a locale number formatter)
tooltipFormatter(leaf: TreemapLeafContext) => stringReturns custom tooltip HTML for a hovered datum (sanitized before it is inserted)
onHighlightItem(labels: string[]) => voidCalled when the hovered/highlighted label(s) change
Common props — shared by every chart (14)
PropTypeDefaultDescription
widthnumber900Chart width in pixels
heightnumber520Chart height in pixels
marginMargin{ top: 36, right: 6, bottom: 6, left: 6 }Inner margins (top/right/bottom/left, in px) reserved for axes, titles, and labels
colorsstring[]Categorical palette for series/labels without an explicit colour or colorsMapping entry
colorsMappingRecord<string, string>Explicit label -> colour map; takes precedence over the palette and per-item colours
highlightItemsstring[]Labels to emphasise; all other marks dim
disabledItemsstring[]Labels to hide and exclude from scales/stacks
renderer"svg" | "canvas""svg"Render as inline SVG (default) or to a canvas (faster for large datasets); getContext() is identical either way
localestringBCP-47 locale used for number and date formatting
skipColorMappingDispatchbooleanfalseExternal-CSS mode: unmapped labels resolve to transparent and onColorMappingGenerated is not emitted, so mark colours come from your CSS via the data-label-safe contract
enableTransitionsbooleantrueAnimate updates with CSS transitions (default true)
onColorMappingGenerated(mapping: Record<string, string>) => voidCalled with the resolved label -> colour map after the chart assigns colours
onChartDataProcessed(context: ChartContext) => voidCalled with the renderer-agnostic ChartContext whenever the data is (re)processed
onDataWarning(warnings: DataWarning[]) => voidCalled with any non-fatal data warnings (duplicate labels, non-finite values, gaps, ...)

Events

The web component dispatches these bubbling CustomEvents (the engine exposes the same via the on* callbacks in the table above):

EventDetailFires when
michi-vz:highlightstring[]hover highlight changes
michi-vz:colormappingRecord<string, string>a color mapping is generated
michi-vz:dataprocessedChartContextdata is (re)processed
michi-vz:datawarningDataWarning[]input warnings are detected

getContext()

mountTreemapChart(el, props).getContext() returns a renderer-agnostic TreemapChartContext: the flat leaves (value / partial / remainder / percent / path), the resolved layout, splitLabels, nesting depth, summary stats (grand total, totals per part, largest leaf, largest remainder), a deterministic natural-language summary, and an a11y table. See LLM context.

Source

Props are typed as TreemapChartProps in @michi-vz/core.

Free and open source. MIT licensed.