Skip to main content

CamoPieChart

Concept​

1. Why​

A pie chart shows parts of a whole: share of traffic by source, budget by category (Swift Charts' sector marks, Carbon's pie and donut charts). It has two forms: pie and donut (with a label in the hole). It is for a few slices; more than about six belong in a bar chart.

2. Shape​

Split of duties

FramePie chart
legend (slice toggles), tooltip, keyboard, data table, animationthe angles and radii, the donut hole, the slice labels (percent), the selected slice's offset, the center content

Other libraries' mapping: Swift Charts sector marks, Carbon pie and donut charts.

3. API​

Contract​

data class PieSlice(val label: String, val value: Double, val color: Color? = null)

fun CamoPieChart(
slices: List<PieSlice>,
modifier: Modifier = Modifier,
donut: Boolean = false,
center: Slot? = null, // in the hole (a total, a title)
selected: Int? = null, onSelect: ((Int?) -> Unit)? = null,
startAngle: Float = -90f, // degrees; 12 o'clock
title: String, loading: Boolean = false, empty: Slot? = null, showTable: Boolean = false,
)

Labels: each slice shows its share when it is wide enough (at least 5%); the legend always lists label, value and share. Selection is the app's (selected, onSelect): the slice moves out a little.

Tokens used​

  • the frame's palette; ChartStyle.donutThickness, sliceGap, selectedOffset

Chart style​

ChartStyle fields this chart reads (see Chart Frame for how the style is resolved: theme-derived defaults, the skin's preset, ChartTokens, then this chart's parameters).

FieldTypeDefaultWhat it's for
donutThicknessFloat0.35ring width as a share of the radius (donut)
sliceGapDp2the gap between slices (a line in the surface color)
selectedOffsetDp6how far a selected slice moves out
labelThresholdFloat0.05the smallest share that gets a label

Accessibility​

  • The frame's summary ("Pie chart, 4 slices, largest: Search 45%"); per slice "Search, 45%, 4,500" in keyboard traversal (Left and Right or Up and Down move along the slices clockwise; Enter or Space selects).
  • The legend lists every slice with its value and share, so a slice too thin to label is still readable.
  • Slices are separated by a gap, not by color alone.

4. Build steps​

  1. The angle function (pure), the sectors, the hole and center, selection, the canvas.
  2. CamoPieChart on the frame, the presets.
  3. Tests: angles sum to a full turn, thresholds, a single slice, zero and negative values, hit testing by angle and radius, selection, RTL.

Done when

  • The dashboard's traffic donut shows a total in its center and selects slices by touch and keys.
Changed in the M11 build (2026-11-04)
  • The legend lists every slice as "Search: 4500, 45%" (label, value, share) and toggles slices; a hidden slice leaves the pie and the angles recompute. The value format is a valueFormat parameter.
  • Slices are separated by an angular gap (sliceGap), not by a line in the surface color; a single visible slice has no gap and fills the circle. Shares under 1% read <1%; share labels sit on slices of at least labelThreshold and use black or white by the slice color's luminance.
  • Selection is the app's: a tap or click, or Enter or Space on the focused slice, passes the slice's index (original list position) to onSelect / onSelected, or null when it was already selected. The selected slice moves out by selectedOffset.
  • Keyboard: all four arrows walk the slices clockwise (a pie has one direction), Home and End jump; the announcement reads "Search, 45%, 4500", with ", selected" when it is.
  • Negative values count as zero in release and debug alike (no debug-only rejection was built); the table shows them as zero. startAngle has a public default PIE_TOP / pieTop (-90).

5. Edge cases​

CaseDecided behavior
CaseDecided behavior
------
Zero valuesThe slice has no angle but stays in the legend.
Negative valuesRejected in debug builds; treated as zero in release.
One sliceA full circle (a full ring for the donut), without a gap.
All zeroThe empty state.
RTLClockwise is kept (angles don't mirror); the legend mirrors.
  • Semi-circle gauges are later (a gauge chart type).

Implementation​

1. Why​

One Canvas draws the sectors with drawArc; the angles come from a pure function and hit testing converts the pointer to an angle and a radius.

2. Shape​

3. API​

@Composable public fun CamoPieChart(slices: List<PieSlice>, title: String, modifier: Modifier = Modifier, donut: Boolean = false,
center: (@Composable () -> Unit)? = null, selected: Int? = null, onSelect: ((Int?) -> Unit)? = null, startAngle: Float = -90f,
loading: Boolean = false, empty: (@Composable () -> Unit)? = null, showTable: Boolean = false)
internal fun sliceAngles(values: List<Double>, startAngle: Float): List<Pair<Float, Float>> // pure

4. Build steps​

  1. Angles, sectors, the hole and center, selection, interaction.
  2. Tests: angles, hit testing, selection, summary.

Done when

  • Works by touch, mouse, keyboard and screen reader.
Changed in the M11 build (2026-11-04)
  • The legend lists every slice as "Search: 4500, 45%" (label, value, share) and toggles slices; a hidden slice leaves the pie and the angles recompute. The value format is a valueFormat parameter.
  • Slices are separated by an angular gap (sliceGap), not by a line in the surface color; a single visible slice has no gap and fills the circle. Shares under 1% read <1%; share labels sit on slices of at least labelThreshold and use black or white by the slice color's luminance.
  • Selection is the app's: a tap or click, or Enter or Space on the focused slice, passes the slice's index (original list position) to onSelect / onSelected, or null when it was already selected. The selected slice moves out by selectedOffset.
  • Keyboard: all four arrows walk the slices clockwise (a pie has one direction), Home and End jump; the announcement reads "Search, 45%, 4500", with ", selected" when it is.
  • Negative values count as zero in release and debug alike (no debug-only rejection was built); the table shows them as zero. startAngle has a public default PIE_TOP / pieTop (-90).
  • The center content sits in a box the size of the hole, so a donut's total can fill it; it is a separate node for a screen reader, not part of the image's text. plotHeight and style parameters added.
  • ChartRenderer.drawPieChart has a default implementation, like the line and bar marks. A tap is read through rememberUpdatedState, because the scene is rebuilt on every hover and would restart the gesture.

5. Edge cases​

CaseDecided behavior
Center contentA Box over the canvas, so it is a normal composable (text, an icon, a number).
  • None specific.