Graph
Nodes and the links between them, laid out by the graph itself, on a ground that is moved and looked at closer by hand: a document's structure, an organisation, what depends on what.
import com.pantrixui.components.data.graph.PantrixGraph
- Layout. Where no node has two links coming to it, the graph is a tree: each parent across from the middle of its children, each subtree as close to the one before it as their outlines allow. Where one has, it is laid out in layers, in the order that crosses the fewest links. A link that closes a circle is drawn backwards, in dashes, and the diagnostics report it.
- Direction.
GraphDirection.StartToEndfrom the start to the end (right to left in Arabic),TopToBottomfrom the top down. - Card. The frame is the graph's, the inside the app's:
GraphNodeScopesays whether the card is chosen, focused or open. A card given a size is laid out without being composed, and composed only while in view; one without is measured once. - Choosing and closing. The chosen card and the open ones are the app's (
selectedKey,expanded).GraphFoldButtoncloses a card and opens it;expandedToLevelopens the cards to a level. The cards glide to their new places; the card that was pressed stays where it is on the screen. - Moving. A drag, the wheel and two fingers; the wheel with Ctrl or ⌘ zooms.
GraphStatezooms, fits and brings a card into view;PantrixGraphZoomputs the same buttons where the screen wants them. - Automation. With
tagKeythe graph isGraph_<tagKey>, and its cardsGraph_<tagKey>.Node_<key>.
Examples
A document's structure
An order's structure, each object and array a card: its name and its count in its head, its values of one line in its rows. An object or an array inside it is a card of its own, linked to it by its key or its index. The chosen card's head is filled; the level buttons open the cards with expandedToLevel, and a card's chevron closes it.
var chosen by remember { mutableStateOf("customer") }
var open by remember { mutableStateOf(expandedToLevel(ORDER_NODES, ORDER_LINKS, level = 3)) }
Column(verticalArrangement = Arrangement.spacedBy(PantrixTheme.spacing.sm)) {
PantrixButtonGroup(
buttons = LEVELS.map { (name, level) ->
GroupButton(
text = name,
onClick = { open = expandedToLevel(ORDER_NODES, ORDER_LINKS, level) },
tagKey = "level_$name"
)
},
size = ButtonSize.Small
)
PantrixGraph(
nodes = ORDER_NODES,
links = ORDER_LINKS,
modifier = Modifier.fillMaxWidth().height(420.dp),
selectedKey = chosen,
onSelect = { chosen = it },
expanded = open,
onExpandedChange = { open = it },
tagKey = "order"
) { node ->
val card = ORDER.getValue(node.key)
val colors = PantrixTheme.colors
Column {
Row(
modifier = Modifier
.fillMaxWidth()
.height(HEAD)
.background(if (selected) colors.primary.container else Color.Transparent)
.padding(start = 10.dp, end = 4.dp),
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.spacedBy(6.dp)
) {
val head = if (selected) colors.primary.onContainer else colors.textPrimary
PantrixText(text = card.title, style = PantrixTheme.typography.code, color = head)
PantrixText(
text = card.count,
modifier = Modifier.weight(1f),
style = PantrixTheme.typography.code,
color = if (selected) head else colors.textSecondary
)
GraphFoldButton()
}
card.rows.forEach { (key, value) ->
Row(modifier = Modifier.height(ROW).padding(horizontal = 10.dp)) {
PantrixText(text = "$key: ", style = PantrixTheme.typography.code, color = colors.primary.color)
PantrixText(
text = value,
style = PantrixTheme.typography.code,
maxLines = 1,
overflow = TextOverflow.Ellipsis
)
}
}
}
}
}
private val HEAD = 32.dp
private val ROW = 24.dp
private val ORDER = linkedMapOf(
"$" to DocumentCard(
"root",
"order.json",
"{11}",
listOf(
"id" to "\"ord_8842\"",
"status" to "\"shipped\"",
"createdAt" to "\"2026-10-28T14:32:00Z\"",
"total" to "129.90",
"currency" to "\"EUR\"",
"paid" to "true",
"coupon" to "null"
)
),
"customer" to DocumentCard(
"customer",
"customer",
"{4}",
listOf("name" to "\"Elif Yılmaz\"", "email" to "\"elif@example.com\"")
),
"theme" to DocumentCard("theme", "theme", "{1}", listOf("accent" to "\"#0F766E\"")),
"items" to DocumentCard("items", "items", "[2]", emptyList()),
"items0" to DocumentCard(
"item_0",
"items[0]",
"{4}",
listOf("sku" to "\"TS-001\"", "qty" to "2", "price" to "39.95")
),
"items1" to DocumentCard(
"item_1",
"items[1]",
"{4}",
listOf("sku" to "\"MG-014\"", "qty" to "1", "price" to "50.00")
),
"shipping" to DocumentCard(
"shipping",
"shipping",
"{3}",
listOf("method" to "\"express\"", "eta" to "\"2026-11-04\"")
),
"address" to DocumentCard("address", "address", "{2}", listOf("city" to "\"İzmir\"", "country" to "\"TR\""))
)
private val ORDER_NODES = ORDER.map { (key, card) ->
val rows = card.rows.size
val height = HEAD + ROW * rows + if (rows > 0) 6.dp else 0.dp
GraphNode(key = key, label = "${card.title}, ${card.count}", size = DpSize(200.dp, height), tagKey = card.id)
}
private val ORDER_LINKS = listOf(
GraphLink("$", "customer", "customer"),
GraphLink("$", "theme", "theme"),
GraphLink("$", "items", "items"),
GraphLink("$", "shipping", "shipping"),
GraphLink("items", "items0", "[0]"),
GraphLink("items", "items1", "[1]"),
GraphLink("shipping", "address", "address")
)
private val LEVELS = listOf("1" to 1, "2" to 2, "3" to 3, "All" to Int.MAX_VALUE)
private class DocumentCard(
val id: String,
val title: String,
val count: String,
val rows: List<Pair<String, String>>
)
From the top down, with buttons of its own
A team from the top down, as on a phone's narrow screen; the zoom buttons stand in the screen's own toolbar, not in the graph's corner: GraphState zooms in, zooms out, goes back to 100% and fits.
val graph = rememberGraphState(initialZoom = 0.8f, zoomRange = 0.5f..1.5f)
Column(verticalArrangement = Arrangement.spacedBy(PantrixTheme.spacing.sm)) {
Row(verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(4.dp)) {
PantrixIconButton(
imageVector = PantrixIcons.ZoomOut,
contentDescription = "Zoom out",
onClick = graph::zoomOut,
size = ButtonSize.Small,
enabled = graph.canZoomOut,
tagKey = "team_out"
)
PantrixText(text = "%${(graph.zoom * 100).roundToInt()}", modifier = Modifier.padding(horizontal = 4.dp))
PantrixIconButton(
imageVector = PantrixIcons.ZoomIn,
contentDescription = "Zoom in",
onClick = graph::zoomIn,
size = ButtonSize.Small,
enabled = graph.canZoomIn,
tagKey = "team_in"
)
PantrixButton(
text = "%100",
onClick = { graph.zoomTo(1f) },
variant = ButtonVariant.Text,
size = ButtonSize.Small,
tagKey = "team_actual"
)
PantrixIconButton(
imageVector = PantrixIcons.FitScreen,
contentDescription = "Fit",
onClick = graph::fit,
size = ButtonSize.Small,
tagKey = "team_fit"
)
PantrixText(
text = "${graph.zoomRange.start.percent()}–${graph.zoomRange.endInclusive.percent()}",
color = PantrixTheme.colors.textSecondary,
style = PantrixTheme.typography.footnote
)
}
PantrixGraph(
nodes = TEAM.map { person -> GraphNode(key = person.id, label = person.name, size = PERSON) },
links = TEAM.mapNotNull { person -> person.lead?.let { GraphLink(from = it, to = person.id) } },
modifier = Modifier.fillMaxWidth().height(360.dp),
state = graph,
direction = GraphDirection.TopToBottom,
zoomButtons = false,
tagKey = "team"
) { node ->
Column(modifier = Modifier.padding(horizontal = 10.dp, vertical = 6.dp)) {
PantrixText(text = node.label, style = PantrixTheme.typography.label)
PantrixText(
text = TEAM.first { it.id == node.key }.role,
style = PantrixTheme.typography.caption,
color = PantrixTheme.colors.textSecondary
)
}
}
}
private fun Float.percent() = "%${(this * 100).roundToInt()}"
private val TEAM = listOf(
Person("lead", "Deniz", "Product lead", null),
Person("design", "Ece", "Design", "lead"),
Person("mobile", "Kerem", "Mobile", "lead"),
Person("web", "Mert", "Web", "lead"),
Person("ui", "Selin", "Interface", "design"),
Person("research", "Bora", "Research", "design"),
Person("android", "Aslı", "Android", "mobile"),
Person("ios", "Can", "iOS", "mobile"),
Person("frontend", "Defne", "Front end", "web")
)
private val PERSON = DpSize(132.dp, 52.dp)
private class Person(
val id: String,
val name: String,
val role: String,
val lead: String?
)
Layers and arrows
A release's steps: some steps have links from two steps, so the graph is laid out in layers, its links with heads at their ends; the link from the build to the signing passes through the tests' layer. The links' shape is chosen; the zoom buttons are the PantrixGraphZoom over the graph. How a link that closes a circle is drawn is on the "Diagnostics" page.
var shape by remember { mutableStateOf(GraphLinkShape.Curve) }
val graph = rememberGraphState()
Column(verticalArrangement = Arrangement.spacedBy(PantrixTheme.spacing.sm)) {
PantrixSegmentButton(
options = LINK_SHAPES.map { (name, value) -> SegmentOption(value, name, tagKey = name.lowercase()) },
selected = shape,
onSelect = { shape = it },
fullWidth = false,
tagKey = "shape"
)
PantrixGraphZoom(state = graph, tagKey = "pipeline")
PantrixGraph(
nodes = STEPS.map { (id, label) -> GraphNode(key = id, label = label, size = STEP) },
links = STEP_LINKS,
modifier = Modifier.fillMaxWidth().height(320.dp),
state = graph,
zoomButtons = false,
style = GraphStyle(linkShape = shape, arrowSize = 7.dp, layerGap = 56.dp),
tagKey = "pipeline"
) { node -> PantrixText(text = node.label, modifier = Modifier.padding(10.dp)) }
}
private val STEPS = listOf(
"code" to "Code",
"build" to "Build",
"unit" to "Unit tests",
"ui" to "UI tests",
"sign" to "Signing",
"release" to "Release"
)
private val STEP_LINKS = listOf(
GraphLink("code", "build"),
GraphLink("build", "unit"),
GraphLink("build", "ui"),
GraphLink("unit", "sign"),
GraphLink("ui", "sign"),
GraphLink("build", "sign", "package"),
GraphLink("sign", "release")
)
private val STEP = DpSize(128.dp, 40.dp)
private val LINK_SHAPES = listOf(
"Curve" to GraphLinkShape.Curve,
"Elbow" to GraphLinkShape.Elbow,
"Straight" to GraphLinkShape.Straight
)
Thousands of cards
Fifteen hundred cards: only those in view and at its edges are composed, and the layout is worked out away from the main thread. The graph is fitted as it opens; the button chooses a card at random and brings it into view.
val graph = rememberGraphState()
var chosen by remember { mutableStateOf<String?>(null) }
LaunchedEffect(Unit) { graph.fit() }
chosen?.let { key -> LaunchedEffect(key) { graph.bringIntoView(key) } }
Column(verticalArrangement = Arrangement.spacedBy(PantrixTheme.spacing.sm)) {
PantrixButton(
text = "Go to a card at random",
onClick = { chosen = LARGE_NODES.random().key },
variant = ButtonVariant.Outline,
size = ButtonSize.Small,
tagKey = "random"
)
PantrixGraph(
nodes = LARGE_NODES,
links = LARGE_LINKS,
modifier = Modifier.fillMaxWidth().height(360.dp),
state = graph,
selectedKey = chosen,
onSelect = { chosen = it },
tagKey = "large"
) { node -> PantrixText(text = node.label, modifier = Modifier.padding(horizontal = 10.dp, vertical = 6.dp)) }
}
private val LARGE_NODES = List(LARGE) { GraphNode(key = "k$it", label = "Card $it", size = DpSize(96.dp, 32.dp)) }
private val LARGE_LINKS = List(LARGE - 1) { GraphLink(from = "k${it / 3}", to = "k${it + 1}") }
private const val LARGE = 1500
Its own colours, and cards that are measured
A graph with colours of its own, a plain ground and links at right angles; its cards are given no size, and each is measured as large as its content. A card makes its own button that closes it with toggle(): closed, it writes how many children it has, and while the keys are on it, it puts a mark at its side.
var open by remember { mutableStateOf(setOf("pantrix", "foundation", "components")) }
val colors = PantrixTheme.colors
PantrixGraph(
nodes = MODULES.map { (id, _) -> GraphNode(key = id, label = id.replaceFirstChar { it.uppercase() }) },
links = MODULES.mapNotNull { (id, parent) -> parent?.let { GraphLink(from = it, to = id) } },
modifier = Modifier.fillMaxWidth().height(300.dp),
expanded = open,
onExpandedChange = { open = it },
ground = GraphGround.Plain,
style = GraphStyle(linkShape = GraphLinkShape.Elbow, layerGap = 48.dp),
colors = GraphColors(
ground = colors.info.container,
node = colors.surface,
border = colors.info.color,
link = colors.info.color,
linkLabel = colors.info.onContainer
),
tagKey = "modules"
) { node ->
val children = MODULES.count { it.second == node.key }
Row(modifier = Modifier.padding(8.dp), verticalAlignment = Alignment.CenterVertically) {
PantrixText(text = if (focused) "▸ ${node.label}" else node.label)
if (hasChildren) {
PantrixButton(
text = if (expanded) "Close" else "Open ($children)",
onClick = { toggle() },
variant = ButtonVariant.Text,
size = ButtonSize.ExtraSmall,
tagKey = "toggle_${node.key}"
)
}
}
}
private val MODULES = listOf(
"pantrix" to null,
"foundation" to "pantrix",
"components" to "pantrix",
"testing" to "pantrix",
"theme" to "foundation",
"tokens" to "foundation",
"a11y" to "foundation",
"elements" to "components",
"data" to "components",
"chart" to "components"
)
API
nodes
Nodes and the links between them, laid out by the graph itself, on a ground that is moved and looked at closer by hand: a document's structure, an organisation, what depends on what.
| Parameter | Type | Default | What it is |
|---|---|---|---|
nodes | List<GraphNode> | the graph's nodes. No two have one key. | |
links | List<GraphLink> | the links between them, by the nodes' keys. A link to a key no node has is refused. | |
modifier | Modifier | Modifier | the graph's modifier. The graph fills the room it is given, which it needs: in a room without a bound it is as large as its cards at the zoom it starts at. |
state | GraphState | rememberGraphState() | where the graph is moved to and how close it is looked at. |
direction | GraphDirection | GraphDirection.StartToEnd | which way the graph goes from a node to its children. |
selectedKey | String? | null | the key of the chosen card, or null for none. |
onSelect | ((String) -> Unit)? | null | called with the key of the card a press or a key chose. Null: no card is chosen by the graph. |
expanded | Set<String>? | null | the keys of the open cards, or null for a graph that does not close: every card open. |
onExpandedChange | ((Set<String>) -> Unit)? | null | called with the keys that are to be open after a card was opened or closed. The graph changes nothing by itself. |
ground | GraphGround | GraphGround.Dots | what the ground shows behind the cards. |
zoomButtons | Boolean | true | whether the graph's own zoom buttons stand in its lower corner at the end, a PantrixGraphZoom. |
style | GraphStyle? | null | this call's own style: over every enclosing one and under the density. Only the fields it sets change, on every platform. |
colors | GraphColors? | null | this graph's own colours, where they are not the theme's: the ones set here, with the theme's for the rest. |
tagKey | String? | null | the graph's automation key: the graph is Graph_<tagKey> and a card in it Graph_<tagKey>.Node_<key>, by its node's own tag key where it has one. What a press or a key does is reported as a Graph event: select, expand and collapse with the node's key, and zoom with where the keys took it, in, out or fit. |
node | @Composable GraphNodeScope.(GraphNode) -> Unit | what a card shows for its node, inside the frame the graph gives it. |
GraphStyle
PantrixGraph's geometry: its cards' frames, the room between its layers and its cards, its links and its ground.
| Parameter | Type | Default | What it is |
|---|---|---|---|
shape | ShapeSpec | ShapeSpec.Unspecified | A card's shape: ShapeRole.Medium. |
elevation | Dp | Dp.Unspecified | How high a card stands off the ground, by its shadow: none on iOS, where cards lie flat. |
borderWidth | Dp | Dp.Unspecified | How wide the edge around a card is. |
selectedBorderWidth | Dp | Dp.Unspecified | How wide the edge around the chosen card is, in the colour that marks it. |
layerGap | Dp | Dp.Unspecified | The room between two layers, along the way the graph goes, which the links cross and their labels are written in. |
siblingGap | Dp | Dp.Unspecified | The room between two cards of one layer, and between two subtrees. |
contentPadding | Dp | Dp.Unspecified | The room around the cards, between them and the ground's edge, when the graph is fitted to its view or scrolled to an end. |
linkWidth | Dp | Dp.Unspecified | How wide a link is drawn. |
linkShape | GraphLinkShape? | null | How a link is drawn: GraphLinkShape.Curve. |
arrowSize | Dp | Dp.Unspecified | How long the head at the end of a link is, which points at the node it goes to: none, as a tree's links need none. A graph whose links go both ways between two layers shows which way with one. |
dotSpacing | Dp | Dp.Unspecified | How far apart the dots of GraphGround.Dots stand, at the graph's own size: they grow and shrink with it, and are drawn twice as far apart where they would stand closer than half of it. |
dotSize | Dp | Dp.Unspecified | How wide a dot of the ground is. |
zoomPadding | Dp | Dp.Unspecified | How far the graph's own zoom buttons stand from the corner of its view. |
labelRole | TypeRole? | null | The type a link's label is written in: TypeRole.Caption. |
GraphColors
What a PantrixGraph is painted with, where it 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 |
|---|---|---|---|
ground | Color | Color.Unspecified | The fill behind the cards: the theme's background, a step from the surface the cards are. |
dots | Color | Color.Unspecified | The dots of GraphGround.Dots: the theme's divider. |
node | Color | Color.Unspecified | A card's fill: the theme's surface. |
content | Color | Color.Unspecified | What a card's content is written in where it sets no colour of its own: what reads on node, the theme's textPrimary on its own surface. |
border | Color | Color.Unspecified | The edge around a card: the theme's border. |
selectedBorder | Color | Color.Unspecified | The edge around the chosen card, which marks it: the primary role's colour. |
link | Color | Color.Unspecified | A link: the theme's textDisabled, quieter than the cards it joins. |
linkLabel | Color | Color.Unspecified | A link's label, written on a patch of ground over the link: the theme's textSecondary. |
focusRing | Color | Color.Unspecified | The ring around the card the keys are on: the theme's focusRing. |
Accessibility
- Keyboard and screen reader. The arrows go from card to card the way the graph goes, Enter chooses, + and − zoom, 0 fits. A screen reader reads a card by its name, its place among its siblings and its level, in the tree's order.