Treemap
A treemap: a box cut into tiles, each as large as its value is of the whole. It suits a whole of many parts, where a pie would have too many slices to read: the folders of a disk, a budget by its lines, a market by its companies. What is large is seen at once, and the small parts still have their place.
import com.pantrixui.components.chart.treemap.PantrixTreemap
- What is written in a tile. With
labelseach tile has its name written in it and under it its value, in the one of black and white that reads better on its colour. A tile with no room for its name has neither; the tooltip and a screen reader say both. - Groups. An item that gives
childrenis a group: its value is the sum of its tiles, and a value given to it is not read. - Colour. Each item given takes the one of the theme's chart colours that falls to its place; a group's tiles are in the group's colour.
colorgives an item or a tile a colour of its own. - Shaded by value. Given a
scale, every tile is drawn in its value's shade on the scale, as a heatmap's cells are (HeatmapScale): a large tile lies nearer the scale's end. The item's and the group's colours are not read then; the blocks and the groups' names stay. The scale bar stands wherelegendsays. - The tile pointed at. The tile under the pointer, or the one that is tapped, is ringed, and the tooltip writes its group, its name, its value and its share of the whole.
- State and pressing. The tile that is pointed at is kept in
ChartState, as a selection whosexis the item's key.onSelectgives the tile that is pressed. - Direction. In a language read from right to left the largest tile lies at the right.
- Export.
exportputs a button over the chart: the chart is made, as it is on the screen and without the part that is pointed at or its tooltip, into a PNG, an SVG or a PDF file and handed to the app. - Automation.
Treemap_<tagKey>; the tiles.Plot, a tile.Tile_<number>, the scale.Legend, the tooltip.Tooltip.
Examples
What fills a disk
What fills a disk, in gigabytes. Each folder is a tile: the largest comes first.
Move the pointer over a tile or tap it: the tooltip writes the folder's size and its share of the disk.
PantrixTreemap(
items = diskFolders(),
description = "The folders that fill the disk",
tagKey = "plain"
)
internal fun diskFolders(): List<TreemapItem> = listOf(
TreemapItem("photos", "Photos", 148),
TreemapItem("video", "Videos", 96),
TreemapItem("apps", "Apps", 64),
TreemapItem("music", "Music", 41),
TreemapItem("docs", "Documents", 27),
TreemapItem("mail", "Mail", 14),
TreemapItem("other", "Other", 9)
)
Groups
A budget, by department and by the lines of each department. Each department is a group: its lines lie in one block, in the department's colour, with the department's name over them.
PantrixTreemap(
items = budgetByDepartment(),
description = "Budget by department",
tagKey = "grouped"
)
internal fun budgetByDepartment(): List<TreemapItem> = listOf(
TreemapItem(
key = "product",
name = "Product",
children = listOf(
TreemapItem("engineering", "Engineering", 420),
TreemapItem("design", "Design", 160),
TreemapItem("research", "Research", 90)
)
),
TreemapItem(
key = "growth",
name = "Growth",
children = listOf(
TreemapItem("ads", "Ads", 240),
TreemapItem("events", "Events", 110),
TreemapItem("content", "Content", 70)
)
),
TreemapItem(
key = "operations",
name = "Operations",
children = listOf(
TreemapItem("support", "Support", 130),
TreemapItem("office", "Office", 80),
TreemapItem("legal", "Legal", 45)
)
)
)
Shaded by value
The same budget, its tiles shaded by their values (scale): the scale goes from the theme's primary container colour to its primary colour, a large line darker. The scale bar is under the chart; the groups' colours are not read then.
val colors = PantrixTheme.colors
PantrixTreemap(
items = budgetByDepartment(),
scale = HeatmapScale(low = colors.primary.container, color = colors.primary.color),
description = "Budget by department",
tagKey = "shaded"
)
internal fun budgetByDepartment(): List<TreemapItem> = listOf(
TreemapItem(
key = "product",
name = "Product",
children = listOf(
TreemapItem("engineering", "Engineering", 420),
TreemapItem("design", "Design", 160),
TreemapItem("research", "Research", 90)
)
),
TreemapItem(
key = "growth",
name = "Growth",
children = listOf(
TreemapItem("ads", "Ads", 240),
TreemapItem("events", "Events", 110),
TreemapItem("content", "Content", 70)
)
),
TreemapItem(
key = "operations",
name = "Operations",
children = listOf(
TreemapItem("support", "Support", 130),
TreemapItem("office", "Office", 80),
TreemapItem("legal", "Legal", 45)
)
)
)
Pressing a tile
onSelect gives the tile that is pressed: press a tile, or go to one with the keyboard and press Enter, and the line under the card writes what was pressed. The values are written in a way of their own: in the tiles by ChartDataLabels(format), in the tooltip by ChartTooltip(format).
val report = LocalExampleEvents.current
PantrixTreemap(
items = diskFolders(),
labels = ChartDataLabels(format = { "${it.toInt()} GB" }),
tooltip = ChartTooltip(format = { "${it.toInt()} GB" }),
onSelect = { tile -> report("${tile.name}: ${tile.text}") },
description = "The folders that fill the disk",
tagKey = "pressed"
)
internal fun diskFolders(): List<TreemapItem> = listOf(
TreemapItem("photos", "Photos", 148),
TreemapItem("video", "Videos", 96),
TreemapItem("apps", "Apps", 64),
TreemapItem("music", "Music", 41),
TreemapItem("docs", "Documents", 27),
TreemapItem("mail", "Mail", 14),
TreemapItem("other", "Other", 9)
)
Export
export = ChartExport(...) puts an export button over the chart: press it and choose the kind (PNG, SVG or PDF). The chart makes the file and hands it to onExport; below, its name and size show, and a PNG's picture, and "Save the file" hands it over in the platform's way.
The file has the tiles and their names; the tooltip of the tile that is pointed at is not in it.
var file by remember { mutableStateOf<ChartExportFile?>(null) }
PantrixTreemap(
items = diskFolders(),
description = "The folders that fill the disk",
tagKey = "exported",
export = ChartExport(fileName = "disk") { file = it }
)
ExportedFile(file, tagKey = "exported")
internal fun diskFolders(): List<TreemapItem> = listOf(
TreemapItem("photos", "Photos", 148),
TreemapItem("video", "Videos", 96),
TreemapItem("apps", "Apps", 64),
TreemapItem("music", "Music", 41),
TreemapItem("docs", "Documents", 27),
TreemapItem("mail", "Mail", 14),
TreemapItem("other", "Other", 9)
)
API
items
A treemap: a box cut into tiles, each as large as its value is of the whole. It suits a whole of many parts, where a pie would have too many slices to read: the folders of a disk, a budget by its lines, a market by its companies. What is large is seen at once, and the small parts still have their place.
| Parameter | Type | Default | What it is |
|---|---|---|---|
items | List<TreemapItem> | the tiles and the groups of tiles. Their keys tell the tiles apart, and two tiles with one key are refused. | |
modifier | Modifier | Modifier | the chart's modifier. A height here is the chart's. |
labels | ChartDataLabels? | ChartDataLabels() | what is written in the tiles: each tile's name, and under it its value, written as format says or short where it says nothing. null writes nothing in them. |
tooltip | ChartTooltip? | ChartTooltip() | what is shown for the tile that is pointed at: its group as the heading, where it lies in one, and under it its name and its value with its share of the whole. A tile has no place to share and no line through it: shared and crosshair are not read. null shows none. |
empty | (@Composable () -> Unit)? | null | what the chart shows while it has no value to draw, in the place of the theme's words for it ("No data"), which a screen reader is then not told either. null writes the words. |
state | ChartState | rememberChartState() | what the reader points at: the chart's own where none is given. |
description | String? | null | what the chart shows, in a few words, for a screen reader: the first thing it says of the chart. Left out, the chart is announced by the names of the items it was given. |
onSelect | ((ChartPoint) -> Unit)? | null | called with the tile that is pressed: its key is its series and its x, its name its name and its label, and its text its value with its share of the whole after it. |
style | ChartStyle? | null | this chart's own geometry: the fields set here, with the theme's for the rest. Two tiles are a sliceGap apart, and their corners as round as barRadius says. |
colors | ChartColors? | null | this chart's own colours, where they are not the theme's. |
tagKey | String? | null | the chart's automation key: the chart is Treemap_<tagKey>, its tiles together Treemap_<tagKey>.Plot, a tile Treemap_<tagKey>.Tile_<number>, counted from one as the tiles are laid out, the scale Treemap_<tagKey>.Legend and the tooltip Treemap_<tagKey>.Tooltip. A press on a tile is reported as a Treemap select event with the item's key. Nothing of the values is reported. |
scale | HeatmapScale? | null | what shades the tiles by their values, as a heatmap's scale does its cells: where it names no ends, they are the least and the most of the tiles' values. null, each tile has its own colour or its group's. |
legend | ChartLegend? | ChartLegend() | where the scale of shades stands, where the treemap has a scale: without one there is nothing for it to show. null shows none. |
export | ChartExport? | null | what lets the reader take the chart out of the app as a PNG, an SVG or a PDF: a button over the chart makes the file of it as it is and hands it over (see ChartExport). null, the chart has no such button. The scale of shades stands under the tiles in the file. |
ChartStyle
A chart's geometry: how tall it is where nothing says, how thick its lines and how large its marks are, and the room between its parts; and whether its fills have textures besides their colours.
| Parameter | Type | Default | What it is |
|---|---|---|---|
height | Dp | Dp.Unspecified | How tall a chart's plot is, with its axes, where the chart's modifier gives it no height. A legend above or under the plot adds its own. |
lineWidth | Dp | Dp.Unspecified | The width of a series' line. |
markerSize | Dp | Dp.Unspecified | The width and height of the mark on a point. |
selectedMarkerSize | Dp | Dp.Unspecified | The same of the mark on the point that is pointed at. |
gridWidth | Dp | Dp.Unspecified | The width of the grid's lines, of the lines along the axes and of the line that marks the place pointed at. Zero draws none. |
labelGap | Dp | Dp.Unspecified | Between the plot and what is written beside it and under it, and between that and an axis' title. |
tickSpacing | Dp | Dp.Unspecified | The least room between two values marked along the axis of values: what decides how many are marked where the axis does not say. |
legendGap | Dp | Dp.Unspecified | Between the legend and the chart it stands beside. |
legendItemGap | Dp | Dp.Unspecified | Between one entry of the legend and the next. |
swatchSize | Dp | Dp.Unspecified | How wide the sample of a series is in the legend and in the tooltip: a short run of its line, or a patch of its fill. |
swatchGap | Dp | Dp.Unspecified | Between that sample and the name after it. |
tooltipShape | ShapeSpec | ShapeSpec.Unspecified | The tooltip's corners: a step of the brand's shape scale, or a fixed shape. |
tooltipHorizontalPadding | Dp | Dp.Unspecified | Between the tooltip's sides and what it holds. |
tooltipVerticalPadding | Dp | Dp.Unspecified | Between its top and bottom and what it holds, and between its heading and rows. |
tooltipGap | Dp | Dp.Unspecified | Between the place pointed at and the tooltip beside it. |
tooltipElevation | Dp | Dp.Unspecified | The tooltip's shadow. |
barGap | Dp | Dp.Unspecified | Between two bars that stand side by side at a place. Bars too thin for it stand closer. |
barMaxWidth | Dp | Dp.Unspecified | The widest a bar is, however much room its place has: a chart of three places is not three slabs. |
barRadius | Dp | Dp.Unspecified | How round the corners of a bar's end are, the end away from nought. |
sliceGap | Dp | Dp.Unspecified | Between two slices of a pie that lie side by side, the same from the pie's middle to its rim. |
selectedSliceOffset | Dp | Dp.Unspecified | How far the slice of a pie that is pointed at stands out past the pie's rim. The pie keeps this much room about itself, so that the slice has where to stand. |
gaugeWidth | Dp | Dp.Unspecified | How thick the arc of a gauge is. An arc is no thicker than half its radius. |
meterHeight | Dp | Dp.Unspecified | How tall the bar of a meter group is. |
trackerHeight | Dp | Dp.Unspecified | How tall the blocks of a status tracker are. |
bubbleMinSize | Dp | Dp.Unspecified | How far across the smallest bubble of a scatter chart is: a point whose size is nothing, or too little to be seen as an area, is still a point. |
bubbleMaxSize | Dp | Dp.Unspecified | How far across its largest bubble is: the one whose size is the most of the chart's. |
navigatorHeight | Dp | Dp.Unspecified | How tall the strip under a chart's plot is, on which the part of the chart that is in view is marked and moved, in a chart whose zoom has one: how wide, beside a plot whose places go down its side. |
barBandShare | Float | Float.NaN | How much of a place's band its bars take, more than nought and one at the most: the rest is the room between one place's bars and the next's. Bars are no wider than barMaxWidth all the same. |
markLineWidth | Dp | Dp.Unspecified | The width of a mark's line: a target, a limit, a day. Zero draws none, and the mark's words are written all the same. |
markBandAlpha | Float | Float.NaN | How much of its colour a mark's band is drawn in, from nought to one: a band lies behind the series, and is a tint. |
patterns | Boolean? | null | Whether the fills of a chart's series, slices and tiles are drawn with textures besides their colours, and its lines broken in turn (ChartPattern): what tells them apart for a reader who cannot tell the colours apart. Left out, as the system is set: on where it is set to tell things apart without colour, iOS's "Differentiate Without Color" and Android's colour correction, and off otherwise. |
nodeWidth | Dp | Dp.Unspecified | How wide a node of a sankey is. |
nodeGap | Dp | Dp.Unspecified | Between two nodes of a column of a sankey. |
ChartColors
What a chart is painted with, for a chart that does not take the theme's colours: a colour that is set is used, and one that is left out stays the theme's.
| Parameter | Type | Default | What it is |
|---|---|---|---|
series | List<Color> | emptyList() | The colours the series are drawn in, in the order they are handed out, for the series that name none of their own. Empty, they are the theme's chart colours. |
grid | Color | Color.Unspecified | The lines drawn across the chart at the marks of an axis. The theme's divider. |
axis | Color | Color.Unspecified | The line along an axis. The theme's border. |
label | Color | Color.Unspecified | What is written beside the axes: the values, the places and the axes' titles, and the names in the legend. The theme's textSecondary. |
crosshair | Color | Color.Unspecified | The line that marks the place pointed at. The theme's borderHover: stronger than the grid it crosses, and quieter than the series it stands behind. |
tooltipContainer | Color | Color.Unspecified | The tooltip's fill. The theme's surface. |
tooltipBorder | Color | Color.Unspecified | The tooltip's edge. The theme's border. |
tooltipText | Color | Color.Unspecified | The tooltip's heading and its values. The theme's textPrimary. |
tooltipLabel | Color | Color.Unspecified | The names of the series in the tooltip. The theme's textSecondary. |
rising | Color | Color.Unspecified | What a value that goes up is drawn in: a step of a waterfall that adds. The theme's success. |
falling | Color | Color.Unspecified | What a value that goes down is drawn in: a step of a waterfall that takes away. The theme's danger. |
Accessibility
- Keyboard. The chart is one stop for Tab: an arrow goes to the nearest tile the way it points; Home and End go to the largest tile and the last, Escape lets go, Enter and Space press the tile that is pointed at.
- Screen reader. The chart says its
description, then each tile from the largest block on, with its group, its name, its value and its share. A chart of more than two hundred tiles is told by its description alone.