Pantrix UI

Sankey Chart

A sankey chart: what flows from where to where, and how much. Nodes stand in columns, and between them lie the flows as bands, each as wide as what flows: where the visitors of a site come from and what they go on to, what a budget is made of and what it is spent on, where the energy of a plant ends up.

import com.pantrixui.components.chart.sankey.PantrixSankeyChart

Examples

Where visitors come from and where they go

Three columns: the sources visitors come from, what they do on the site, and how they end up. The chart works the columns out from the links itself, and puts the nodes of a column in an order that keeps their flows from crossing.

Move the pointer over a node, or touch the node: the node's flows grow stronger and the tooltip writes each of them with the node at its other end. On a flow, that flow alone grows stronger and the tooltip writes it with its two nodes.

PantrixSankeyChart(
    nodes = VISITOR_NODES,
    links = VISITOR_LINKS,
    modifier = Modifier.height(280.dp),
    description = "The sources visitors come from, what they do on the site, and how they end up",
    tagKey = "plain"
)

private val VISITOR_NODES = listOf(
    SankeyNode("search", "Search"),
    SankeyNode("social", "Social"),
    SankeyNode("direct", "Direct"),
    SankeyNode("signup", "Signed up"),
    SankeyNode("browse", "Browsed"),
    SankeyNode("paid", "Paid"),
    SankeyNode("free", "Stayed free"),
    SankeyNode("left", "Left")
)

private val VISITOR_LINKS = listOf(
    SankeyLink("search", "signup", 300),
    SankeyLink("search", "browse", 200),
    SankeyLink("social", "signup", 100),
    SankeyLink("social", "browse", 200),
    SankeyLink("direct", "signup", 120),
    SankeyLink("direct", "browse", 80),
    SankeyLink("signup", "paid", 220),
    SankeyLink("signup", "free", 300),
    SankeyLink("browse", "free", 180),
    SankeyLink("browse", "left", 300)
)

Income and spending

Two columns: a month's income and where it is spent. The tooltip's values are written short (the format of ChartTooltip). Turn the switch: the nodes' names go away (names = false) and the names stay in the tooltip alone.

var named by remember { mutableStateOf(true) }
val strings = PantrixTheme.strings
PantrixSwitchButton(
    checked = named,
    onCheckedChange = { named = it },
    label = "Node names",
    tagKey = "sankey_names"
)
PantrixSankeyChart(
    nodes = BUDGET_NODES,
    links = BUDGET_LINKS,
    names = named,
    tooltip = ChartTooltip(format = { strings.compact(it) + " TL" }),
    description = "A month's income and where it is spent",
    tagKey = "budget"
)

private val BUDGET_NODES = listOf(
    SankeyNode("salary", "Salary"),
    SankeyNode("extra", "Extra income"),
    SankeyNode("rent", "Rent"),
    SankeyNode("food", "Food"),
    SankeyNode("bills", "Bills"),
    SankeyNode("saved", "Savings")
)

private val BUDGET_LINKS = listOf(
    SankeyLink("salary", "rent", 28_000),
    SankeyLink("salary", "food", 16_000),
    SankeyLink("salary", "bills", 9_000),
    SankeyLink("salary", "saved", 12_000),
    SankeyLink("extra", "saved", 8_000),
    SankeyLink("extra", "food", 4_000)
)

Pressing a node

A press hands over the node: its key in series, its name in name, what it counts for in value. A press on a flow hands over the flow: the key of the node it leaves in series, that of the node it reaches in x. Press a node, or a flow between two columns: the line under the chart writes what was pressed.

var pressed by remember { mutableStateOf<String?>(null) }
PantrixSankeyChart(
    nodes = VISITOR_NODES,
    links = VISITOR_LINKS,
    modifier = Modifier.height(280.dp),
    onSelect = { pressed = "${it.name}: ${it.text} visitors" },
    description = "The sources visitors come from, what they do on the site, and how they end up",
    tagKey = "press"
)
PantrixText(text = pressed ?: "Press a node")

private val VISITOR_NODES = listOf(
    SankeyNode("search", "Search"),
    SankeyNode("social", "Social"),
    SankeyNode("direct", "Direct"),
    SankeyNode("signup", "Signed up"),
    SankeyNode("browse", "Browsed"),
    SankeyNode("paid", "Paid"),
    SankeyNode("free", "Stayed free"),
    SankeyNode("left", "Left")
)

private val VISITOR_LINKS = listOf(
    SankeyLink("search", "signup", 300),
    SankeyLink("search", "browse", 200),
    SankeyLink("social", "signup", 100),
    SankeyLink("social", "browse", 200),
    SankeyLink("direct", "signup", 120),
    SankeyLink("direct", "browse", 80),
    SankeyLink("signup", "paid", 220),
    SankeyLink("signup", "free", 300),
    SankeyLink("browse", "free", 180),
    SankeyLink("browse", "left", 300)
)

Column alignment and order

Column alignment (align). Those from ads flow straight to a payment, and some of those from search leave at once: these two nodes could stand in more than one column. "Justify" puts a node nothing flows out of in the last column, "Left" as early as it can be, "Right" a node nothing flows into as late as it can be, and "Center" a column before the first node it flows into. Turn the switch: the nodes stand in a column in the order they were given (order = SankeyOrder.Given).

var align by remember { mutableStateOf(SankeyAlign.Justify) }
var given by remember { mutableStateOf(false) }
PantrixSegmentButton(
    options = SANKEY_ALIGNS,
    selected = align,
    onSelect = { align = it },
    fullWidth = false,
    tagKey = "sankey_align"
)
PantrixSwitchButton(
    checked = given,
    onCheckedChange = { given = it },
    label = "In the given order",
    tagKey = "sankey_given"
)
PantrixSankeyChart(
    nodes = ALIGN_NODES,
    links = ALIGN_LINKS,
    modifier = Modifier.height(260.dp),
    align = align,
    order = if (given) SankeyOrder.Given else SankeyOrder.Untangled,
    description = "Where visitors come from and where they end up",
    tagKey = "align"
)

private val ALIGN_NODES = listOf(
    SankeyNode("search", "Search"),
    SankeyNode("ads", "Ads"),
    SankeyNode("signup", "Signed up"),
    SankeyNode("browse", "Browsed"),
    SankeyNode("left", "Left"),
    SankeyNode("paid", "Paid"),
    SankeyNode("bounce", "Left at once")
)

private val ALIGN_LINKS = listOf(
    SankeyLink("search", "signup", 300),
    SankeyLink("search", "browse", 200),
    SankeyLink("search", "bounce", 100),
    SankeyLink("ads", "paid", 60),
    SankeyLink("signup", "paid", 120),
    SankeyLink("signup", "left", 180),
    SankeyLink("browse", "left", 200)
)

private val SANKEY_ALIGNS = listOf(
    SegmentOption(value = SankeyAlign.Justify, label = "Justify", tagKey = "justify"),
    SegmentOption(value = SankeyAlign.Left, label = "Left", tagKey = "left"),
    SegmentOption(value = SankeyAlign.Right, label = "Right", tagKey = "right"),
    SegmentOption(value = SankeyAlign.Center, label = "Center", tagKey = "center")
)

Your own sizes

The nodes' own sizes: wider nodes (nodeWidth) and more room between the nodes of a column (nodeGap). A field that is not given stays the theme's.

PantrixSankeyChart(
    nodes = BUDGET_NODES,
    links = BUDGET_LINKS,
    description = "A month's income and where it is spent",
    style = ChartStyle(nodeWidth = 24.dp, nodeGap = 16.dp),
    tagKey = "own_sizes"
)

private val BUDGET_NODES = listOf(
    SankeyNode("salary", "Salary"),
    SankeyNode("extra", "Extra income"),
    SankeyNode("rent", "Rent"),
    SankeyNode("food", "Food"),
    SankeyNode("bills", "Bills"),
    SankeyNode("saved", "Savings")
)

private val BUDGET_LINKS = listOf(
    SankeyLink("salary", "rent", 28_000),
    SankeyLink("salary", "food", 16_000),
    SankeyLink("salary", "bills", 9_000),
    SankeyLink("salary", "saved", 12_000),
    SankeyLink("extra", "saved", 8_000),
    SankeyLink("extra", "food", 4_000)
)

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 nodes, the flows and the names; a flow or a node that is pointed at does not stand out in it.

var file by remember { mutableStateOf<ChartExportFile?>(null) }
PantrixSankeyChart(
    nodes = VISITOR_NODES,
    links = VISITOR_LINKS,
    modifier = Modifier.height(280.dp),
    description = "The sources visitors come from, what they do on the site, and how they end up",
    tagKey = "exported",
    export = ChartExport(fileName = "visitors") { file = it }
)
ExportedFile(file, tagKey = "exported")

private val VISITOR_NODES = listOf(
    SankeyNode("search", "Search"),
    SankeyNode("social", "Social"),
    SankeyNode("direct", "Direct"),
    SankeyNode("signup", "Signed up"),
    SankeyNode("browse", "Browsed"),
    SankeyNode("paid", "Paid"),
    SankeyNode("free", "Stayed free"),
    SankeyNode("left", "Left")
)

private val VISITOR_LINKS = listOf(
    SankeyLink("search", "signup", 300),
    SankeyLink("search", "browse", 200),
    SankeyLink("social", "signup", 100),
    SankeyLink("social", "browse", 200),
    SankeyLink("direct", "signup", 120),
    SankeyLink("direct", "browse", 80),
    SankeyLink("signup", "paid", 220),
    SankeyLink("signup", "free", 300),
    SankeyLink("browse", "free", 180),
    SankeyLink("browse", "left", 300)
)

API

nodes

A sankey chart: what flows from where to where, and how much. Nodes stand in columns, and between them lie the flows as bands, each as wide as what flows: where the visitors of a site come from and what they go on to, what a budget is made of and what it is spent on, where the energy of a plant ends up.

ParameterTypeDefaultWhat it is
nodesList<SankeyNode>the nodes. Their keys tell them apart, and two with one key are refused.
linksList<SankeyLink>what flows between the nodes: each from one node to another, by their keys.
modifierModifierModifierthe chart's modifier. A height here is the chart's.
namesBooleantruewhether each node's name is written beside it, where it has room.
tooltipChartTooltip?ChartTooltip()what is shown for the node that is pointed at: its name and what it counts for, and under them each flow into it and out of it, with the node at its other end. Its format writes the values; shared and crosshair are not read. null shows none.
empty(@Composable () -> Unit)?nullwhat 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.
stateChartStaterememberChartState()what the reader points at: the chart's own where none is given.
descriptionString?nullwhat 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 its nodes.
onSelect((ChartPoint) -> Unit)?nullcalled with the node that is pressed: its key is its series and its x, its name its name and its label, and its value what it counts for; and with the flow that is pressed: the key of the node it leaves is its series and of the node it reaches its x, its name the two nodes' names, and its value how much flows.
styleChartStyle?nullthis chart's own geometry: the fields set here, with the theme's for the rest. A node is nodeWidth wide, two nodes of a column are a nodeGap apart, and a node's corners are as round as barRadius says.
colorsChartColors?nullthis chart's own colours, where they are not the theme's.
tagKeyString?nullthe chart's automation key: the chart is Sankey_<tagKey>, its nodes and flows together Sankey_<tagKey>.Plot, a node Sankey_<tagKey>.Node_<number>, counted from one, column by column and in each from the top down, and the tooltip Sankey_<tagKey>.Tooltip. A press on a node is reported as a Sankey select event with the node's key, and a press on a flow with the keys of its two nodes, from>to. Nothing of the values is reported.
alignSankeyAlignSankeyAlign.Justifywhich column each node stands in, of those it could stand in.
orderSankeyOrderSankeyOrder.Untangledthe order the nodes of a column stand in: the one that keeps the flows from crossing, or as given.
exportChartExport?nullwhat 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.

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.

ParameterTypeDefaultWhat it is
heightDpDp.UnspecifiedHow 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.
lineWidthDpDp.UnspecifiedThe width of a series' line.
markerSizeDpDp.UnspecifiedThe width and height of the mark on a point.
selectedMarkerSizeDpDp.UnspecifiedThe same of the mark on the point that is pointed at.
gridWidthDpDp.UnspecifiedThe 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.
labelGapDpDp.UnspecifiedBetween the plot and what is written beside it and under it, and between that and an axis' title.
tickSpacingDpDp.UnspecifiedThe least room between two values marked along the axis of values: what decides how many are marked where the axis does not say.
legendGapDpDp.UnspecifiedBetween the legend and the chart it stands beside.
legendItemGapDpDp.UnspecifiedBetween one entry of the legend and the next.
swatchSizeDpDp.UnspecifiedHow 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.
swatchGapDpDp.UnspecifiedBetween that sample and the name after it.
tooltipShapeShapeSpecShapeSpec.UnspecifiedThe tooltip's corners: a step of the brand's shape scale, or a fixed shape.
tooltipHorizontalPaddingDpDp.UnspecifiedBetween the tooltip's sides and what it holds.
tooltipVerticalPaddingDpDp.UnspecifiedBetween its top and bottom and what it holds, and between its heading and rows.
tooltipGapDpDp.UnspecifiedBetween the place pointed at and the tooltip beside it.
tooltipElevationDpDp.UnspecifiedThe tooltip's shadow.
barGapDpDp.UnspecifiedBetween two bars that stand side by side at a place. Bars too thin for it stand closer.
barMaxWidthDpDp.UnspecifiedThe widest a bar is, however much room its place has: a chart of three places is not three slabs.
barRadiusDpDp.UnspecifiedHow round the corners of a bar's end are, the end away from nought.
sliceGapDpDp.UnspecifiedBetween two slices of a pie that lie side by side, the same from the pie's middle to its rim.
selectedSliceOffsetDpDp.UnspecifiedHow 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.
gaugeWidthDpDp.UnspecifiedHow thick the arc of a gauge is. An arc is no thicker than half its radius.
meterHeightDpDp.UnspecifiedHow tall the bar of a meter group is.
trackerHeightDpDp.UnspecifiedHow tall the blocks of a status tracker are.
bubbleMinSizeDpDp.UnspecifiedHow 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.
bubbleMaxSizeDpDp.UnspecifiedHow far across its largest bubble is: the one whose size is the most of the chart's.
navigatorHeightDpDp.UnspecifiedHow 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.
barBandShareFloatFloat.NaNHow 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.
markLineWidthDpDp.UnspecifiedThe 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.
markBandAlphaFloatFloat.NaNHow 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.
patternsBoolean?nullWhether 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.
nodeWidthDpDp.UnspecifiedHow wide a node of a sankey is.
nodeGapDpDp.UnspecifiedBetween 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.

ParameterTypeDefaultWhat it is
seriesList<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.
gridColorColor.UnspecifiedThe lines drawn across the chart at the marks of an axis. The theme's divider.
axisColorColor.UnspecifiedThe line along an axis. The theme's border.
labelColorColor.UnspecifiedWhat is written beside the axes: the values, the places and the axes' titles, and the names in the legend. The theme's textSecondary.
crosshairColorColor.UnspecifiedThe 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.
tooltipContainerColorColor.UnspecifiedThe tooltip's fill. The theme's surface.
tooltipBorderColorColor.UnspecifiedThe tooltip's edge. The theme's border.
tooltipTextColorColor.UnspecifiedThe tooltip's heading and its values. The theme's textPrimary.
tooltipLabelColorColor.UnspecifiedThe names of the series in the tooltip. The theme's textSecondary.
risingColorColor.UnspecifiedWhat a value that goes up is drawn in: a step of a waterfall that adds. The theme's success.
fallingColorColor.UnspecifiedWhat a value that goes down is drawn in: a step of a waterfall that takes away. The theme's danger.

Accessibility