Pantrix UI

Pie Chart

A pie chart: the parts of a whole as the slices of a disc, each as much of the turn as its value is of their sum. With a hole in its middle it is a ring, a donut, which has room in the hole for a total or a title.

import com.pantrixui.components.chart.pie.PantrixPieChart

Examples

The shares of a whole

At its plainest: the slices, each with its key, its name and its value. The first slice starts at twelve o'clock and the others follow it clockwise; the legend stands under the chart.

Move the pointer over a slice, or touch it: the slice stands out and the tooltip writes the number of visits and its share of the total. Press a name in the legend: the slice goes and the others share the whole anew.

PantrixPieChart(
    slices = VISIT_SOURCES.map { PieSlice(it.key, it.name, it.visits) },
    description = "Visits by source",
    tagKey = "plain"
)

private data class VisitSource(
    val key: String,
    val name: String,
    val visits: Int,
    val before: Int
)

private val VISIT_SOURCES = listOf(
    VisitSource("direct", "Direct", visits = 420, before = 380),
    VisitSource("search", "Search", visits = 310, before = 240),
    VisitSource("social", "Social", visits = 150, before = 260),
    VisitSource("mail", "E-mail", visits = 80, before = 90),
    VisitSource("other", "Other", visits = 40, before = 30)
)

A ring with its total in the middle

A pie with its middle cut out is a ring: six tenths of the radius are cut out (hole) and the total is written in the hole (center).

Take a slice out from the legend: the number in the middle becomes the total of the slices that are left. The screen keeps the chart's state itself (rememberChartState) and reads the hidden slices from it.

val state = rememberChartState()
val strings = PantrixTheme.strings
val total = VISIT_SOURCES.filter { it.key !in state.hidden }.sumOf { it.visits }
PantrixPieChart(
    slices = VISIT_SOURCES.map { PieSlice(it.key, it.name, it.visits) },
    hole = 0.6f,
    center = {
        Column(horizontalAlignment = Alignment.CenterHorizontally) {
            PantrixText(text = strings.decimal(total), style = PantrixTheme.typography.title)
            PantrixText(text = "visits", color = PantrixTheme.colors.textSecondary)
        }
    },
    state = state,
    description = "Visits by source, with their total in the middle",
    tagKey = "ring"
)

private data class VisitSource(
    val key: String,
    val name: String,
    val visits: Int,
    val before: Int
)

private val VISIT_SOURCES = listOf(
    VisitSource("direct", "Direct", visits = 420, before = 380),
    VisitSource("search", "Search", visits = 310, before = 240),
    VisitSource("social", "Social", visits = 150, before = 260),
    VisitSource("mail", "E-mail", visits = 80, before = 90),
    VisitSource("other", "Other", visits = 40, before = 30)
)

The shares on the slices

The shares are on the slices (labels): each slice has its share of the total written on it, in black or white, whichever reads better on the slice's colour. Turn the switch: the labels write the value, not the share (ChartDataLabels(format = …)).

A label that does not fit on its slice is written outside it with the slice's name; a line links it to its slice.

var values by remember { mutableStateOf(false) }
val strings = PantrixTheme.strings
PantrixSwitchButton(
    checked = values,
    onCheckedChange = { values = it },
    label = "Write the values",
    tagKey = "pie_labels_values"
)
PantrixPieChart(
    slices = VISIT_SOURCES.map { PieSlice(it.key, it.name, it.visits) },
    labels = if (values) ChartDataLabels(format = { strings.decimal(it) }) else ChartDataLabels(),
    description = "Visits by source, with their shares",
    tagKey = "labels"
)

private data class VisitSource(
    val key: String,
    val name: String,
    val visits: Int,
    val before: Int
)

private val VISIT_SOURCES = listOf(
    VisitSource("direct", "Direct", visits = 420, before = 380),
    VisitSource("search", "Search", visits = 310, before = 240),
    VisitSource("social", "Social", visits = 150, before = 260),
    VisitSource("mail", "E-mail", visits = 80, before = 90),
    VisitSource("other", "Other", visits = 40, before = 30)
)

Where the labels go

Where the labels go (ChartDataLabels(position = …)). "Automatic" (Auto) writes each share on its slice and the ones that do not fit outside; "Outside" (Outside) writes each slice's name and share beside the pie; "Inside" (Center) leaves out the ones that do not fit. A line in the slice's colour links the words outside to their slice, and the pie makes room for them at its two sides.

var place by remember { mutableStateOf(ChartDataLabelPosition.Outside) }
PantrixSegmentButton(
    options = PIE_LABEL_PLACES,
    selected = place,
    onSelect = { place = it },
    fullWidth = false,
    tagKey = "pie_label_place"
)
PantrixPieChart(
    slices = VISIT_SOURCES.map { PieSlice(it.key, it.name, it.visits) },
    labels = ChartDataLabels(position = place),
    legend = null,
    description = "Visits by source, with their shares",
    tagKey = "label_place"
)

private data class VisitSource(
    val key: String,
    val name: String,
    val visits: Int,
    val before: Int
)

private val VISIT_SOURCES = listOf(
    VisitSource("direct", "Direct", visits = 420, before = 380),
    VisitSource("search", "Search", visits = 310, before = 240),
    VisitSource("social", "Social", visits = 150, before = 260),
    VisitSource("mail", "E-mail", visits = 80, before = 90),
    VisitSource("other", "Other", visits = 40, before = 30)
)

private val PIE_LABEL_PLACES = listOf(
    SegmentOption(value = ChartDataLabelPosition.Auto, label = "Automatic", tagKey = "auto"),
    SegmentOption(value = ChartDataLabelPosition.Outside, label = "Outside", tagKey = "outside"),
    SegmentOption(value = ChartDataLabelPosition.Center, label = "Inside", tagKey = "inside")
)

A half ring

A half ring: the slices begin at nine o'clock (startAngle = 270f) and take half a turn (sweepAngle = 180f). The ring is as wide as its box allows, and the total stands in the hole of the half that is drawn.

val total = PantrixTheme.strings.decimal(VISIT_SOURCES.sumOf { it.visits })
PantrixPieChart(
    slices = VISIT_SOURCES.map { PieSlice(it.key, it.name, it.visits) },
    hole = 0.6f,
    startAngle = 270f,
    sweepAngle = 180f,
    center = { PantrixText(text = total, style = PantrixTheme.typography.title) },
    description = "Visits by source, a half ring",
    tagKey = "half"
)

private data class VisitSource(
    val key: String,
    val name: String,
    val visits: Int,
    val before: Int
)

private val VISIT_SOURCES = listOf(
    VisitSource("direct", "Direct", visits = 420, before = 380),
    VisitSource("search", "Search", visits = 310, before = 240),
    VisitSource("social", "Social", visits = 150, before = 260),
    VisitSource("mail", "E-mail", visits = 80, before = 90),
    VisitSource("other", "Other", visits = 40, before = 30)
)

Order and small slices

Visits by browser, largest first (sort = PieSort.Descending). The slices smaller than five hundredths of the whole are gathered into one slice at the end (others = PieOthers(share = 0.05f)); its name is the theme's word. Move the pointer over the gathered slice, or touch it: the tooltip names each one it holds. Turn the switch off: every slice shows.

var gathered by remember { mutableStateOf(true) }
PantrixSwitchButton(
    checked = gathered,
    onCheckedChange = { gathered = it },
    label = "Gather the small ones",
    tagKey = "pie_gathered"
)
PantrixPieChart(
    slices = BROWSER_VISITS.map { PieSlice(it.key, it.name, it.visits) },
    sort = PieSort.Descending,
    others = if (gathered) PieOthers(share = 0.05f) else null,
    labels = ChartDataLabels(),
    legend = ChartLegend(position = ChartLegendPosition.End),
    description = "Visits by browser",
    tagKey = "gathered"
)

private data class Browser(
    val key: String,
    val name: String,
    val visits: Int
)

private val BROWSER_VISITS = listOf(
    Browser("safari", "Safari", 198),
    Browser("firefox", "Firefox", 42),
    Browser("chrome", "Chrome", 612),
    Browser("opera", "Opera", 24),
    Browser("edge", "Edge", 54),
    Browser("uc", "UC", 17),
    Browser("samsung", "Samsung", 31),
    Browser("yandex", "Yandex", 22)
)

Where the legend stands

The legend stands on one of the chart's four sides. Above and under it the entries are set in a row, which goes on to another line where they do not fit; at the sides they stand under one another and take no more than four tenths of the width. The pie is the largest circle that fits in the middle of what the legend leaves.

var place by remember { mutableStateOf(ChartLegendPosition.End) }
PantrixSegmentButton(
    options = PIE_LEGEND_PLACES,
    selected = place,
    onSelect = { place = it },
    fullWidth = false,
    tagKey = "pie_legend_place"
)
PantrixPieChart(
    slices = VISIT_SOURCES.map { PieSlice(it.key, it.name, it.visits) },
    hole = 0.6f,
    legend = ChartLegend(position = place),
    description = "Visits by source",
    tagKey = "legend"
)

private data class VisitSource(
    val key: String,
    val name: String,
    val visits: Int,
    val before: Int
)

private val VISIT_SOURCES = listOf(
    VisitSource("direct", "Direct", visits = 420, before = 380),
    VisitSource("search", "Search", visits = 310, before = 240),
    VisitSource("social", "Social", visits = 150, before = 260),
    VisitSource("mail", "E-mail", visits = 80, before = 90),
    VisitSource("other", "Other", visits = 40, before = 30)
)

private val PIE_LEGEND_PLACES = listOf(
    SegmentOption(value = ChartLegendPosition.Top, label = "Top", tagKey = "top"),
    SegmentOption(value = ChartLegendPosition.Bottom, label = "Bottom", tagKey = "bottom"),
    SegmentOption(value = ChartLegendPosition.Start, label = "Start", tagKey = "start"),
    SegmentOption(value = ChartLegendPosition.End, label = "End", tagKey = "end")
)

Pressing a slice

onSelect gives the slice that is pressed: its key, its name, its value and how the chart writes it. Press a slice, or go to one with the keyboard and press Enter: the line under the card writes what was pressed.

val report = LocalExampleEvents.current
PantrixPieChart(
    slices = VISIT_SOURCES.map { PieSlice(it.key, it.name, it.visits) },
    description = "Visits by source",
    onSelect = { point -> report("${point.name}: ${point.text}") },
    tagKey = "pressed"
)

private data class VisitSource(
    val key: String,
    val name: String,
    val visits: Int,
    val before: Int
)

private val VISIT_SOURCES = listOf(
    VisitSource("direct", "Direct", visits = 420, before = 380),
    VisitSource("search", "Search", visits = 310, before = 240),
    VisitSource("social", "Social", visits = 150, before = 260),
    VisitSource("mail", "E-mail", visits = 80, before = 90),
    VisitSource("other", "Other", visits = 40, before = 30)
)

Values that change

When the values change the slices go from where they were to where they are now. Turn the switch: the chart shows last year's visits. The slices are matched by their keys: a slice whose name or value changes is the same slice, and keeps its colour and its place in the legend.

var before by remember { mutableStateOf(false) }
PantrixSwitchButton(
    checked = before,
    onCheckedChange = { before = it },
    label = "Last year",
    tagKey = "pie_before"
)
PantrixPieChart(
    slices = VISIT_SOURCES.map { PieSlice(it.key, it.name, if (before) it.before else it.visits) },
    hole = 0.6f,
    labels = ChartDataLabels(),
    description = if (before) "Visits by source last year" else "Visits by source this year",
    tagKey = "changing"
)

private data class VisitSource(
    val key: String,
    val name: String,
    val visits: Int,
    val before: Int
)

private val VISIT_SOURCES = listOf(
    VisitSource("direct", "Direct", visits = 420, before = 380),
    VisitSource("search", "Search", visits = 310, before = 240),
    VisitSource("social", "Social", visits = 150, before = 260),
    VisitSource("mail", "E-mail", visits = 80, before = 90),
    VisitSource("other", "Other", visits = 40, before = 30)
)

Your own colours and sizes

A chart's own look. Each slice names its own colour (PieSlice(color = …)): shades of one violet from the palette in place of the theme's chart colours. ChartStyle gives the sizes: no gap between the slices (sliceGap) and a lower chart (height). A field that is not given stays the theme's.

val dark = LocalDemoDarkTheme.current
val violet = PantrixPalette.Violet
val tones = if (dark) {
    listOf(violet.shade300, violet.shade400, violet.shade500, violet.shade600, violet.shade700)
} else {
    listOf(violet.shade800, violet.shade600, violet.shade500, violet.shade400, violet.shade300)
}
PantrixPieChart(
    slices = VISIT_SOURCES.mapIndexed { index, source ->
        PieSlice(source.key, source.name, source.visits, color = tones[index])
    },
    labels = ChartDataLabels(),
    description = "Visits by source",
    style = ChartStyle(sliceGap = 0.dp, height = 180.dp),
    tagKey = "own_look"
)

private data class VisitSource(
    val key: String,
    val name: String,
    val visits: Int,
    val before: Int
)

private val VISIT_SOURCES = listOf(
    VisitSource("direct", "Direct", visits = 420, before = 380),
    VisitSource("search", "Search", visits = 310, before = 240),
    VisitSource("social", "Social", visits = 150, before = 260),
    VisitSource("mail", "E-mail", visits = 80, before = 90),
    VisitSource("other", "Other", visits = 40, before = 30)
)

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 pie as it is on the screen, the legend under it; the slice that is pointed at and its tooltip are not in it. What stands in the hole by center is not in the file either.

var file by remember { mutableStateOf<ChartExportFile?>(null) }
PantrixPieChart(
    slices = VISIT_SOURCES.map { PieSlice(it.key, it.name, it.visits) },
    hole = 0.6f,
    center = { PantrixText(text = "Visits", color = PantrixTheme.colors.textSecondary) },
    description = "Visits by source",
    tagKey = "exported",
    export = ChartExport(fileName = "visit-sources") { file = it }
)
ExportedFile(file, tagKey = "exported")

private data class VisitSource(
    val key: String,
    val name: String,
    val visits: Int,
    val before: Int
)

private val VISIT_SOURCES = listOf(
    VisitSource("direct", "Direct", visits = 420, before = 380),
    VisitSource("search", "Search", visits = 310, before = 240),
    VisitSource("social", "Social", visits = 150, before = 260),
    VisitSource("mail", "E-mail", visits = 80, before = 90),
    VisitSource("other", "Other", visits = 40, before = 30)
)

API

slices

A pie chart: the parts of a whole as the slices of a disc, each as much of the turn as its value is of their sum. With a hole in its middle it is a ring, a donut, which has room in the hole for a total or a title.

ParameterTypeDefaultWhat it is
slicesList<PieSlice>the slices, in the order they lie in the pie where sort keeps it, clockwise from startAngle. Their keys tell them apart, and two with one key are refused.
modifierModifierModifierthe chart's modifier. A height here is the chart's, the legend's with it.
holeFloat0fhow much of the pie's radius is cut out of its middle: nought for a whole pie, and six tenths for a ring with room to write in. No more than nine tenths.
center(@Composable () -> Unit)?nullwhat stands in the hole of a ring: a total, a title. It has the largest box the hole holds, of a half ring in the half that is drawn. A whole pie has no place for it.
labelsChartDataLabels?nullwhat is written about the slices: each slice's share of the whole, or its value as the labels' format writes it. On a slice it is written in black or white, whichever reads on the slice; outside it, the slice's name comes before it, with a line in the slice's colour leading to it. Their position says where: Outside outside every slice, Auto on each slice that holds its words and outside the others, and any other on the slices only, where a slice too small to hold its words has none. Words outside stand in a column at each side; where a side has no room for all of them, one under another, the smallest slices there have none. null writes nothing about them.
legendChartLegend?ChartLegend()where the names of the slices stand. null shows none.
tooltipChartTooltip?ChartTooltip()what is shown for the slice that is pointed at: its value, as the tooltip's format writes it or in the theme's language, and its share of the whole. null shows none. Whether a tooltip is shared and whether it has a crosshair say nothing to a pie.
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 has hidden and what they point at: the chart's own where none is given. A slice is hidden by its key, and pointed at by a selection whose x is its key.
descriptionString?nullwhat the pie 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 slices.
onSelect((ChartPoint) -> Unit)?nullcalled with the slice that is pressed: its key as the point's series and its x, its name, its value, and what the pie writes for it.
styleChartStyle?nullthis chart's own geometry: the fields set here, with the theme's for the rest.
colorsChartColors?nullthis chart's own colours, where they are not the theme's.
tagKeyString?nullthe chart's automation key: the chart is PieChart_<tagKey>, the pie PieChart_<tagKey>.Plot, a slice PieChart_<tagKey>.Slice_<number> counted from one, the legend's entry of a slice PieChart_<tagKey>.Legend_<slice's key> and the tooltip PieChart_<tagKey>.Tooltip. A press on a slice is reported as a PieChart select event with the slice's key, and a press on a legend's entry as a toggle event with the key and whether the slice is shown. Nothing of the values is reported.
startAngleFloat0fwhere the first slice begins, in degrees clockwise from twelve o'clock: 270 begins at nine o'clock.
sweepAngleFloat360fhow many degrees the slices take together: a whole turn, 360, or 180 for a half pie, from over nought up to a whole turn.
sortPieSortPieSort.Giventhe order the slices lie in: as given, the largest first or the smallest first.
othersPieOthers?nullhow the smallest slices are gathered into one. null gathers none.
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. What stands in the hole, center, is not 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.

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