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
| Frame | Pie chart |
|---|---|
| legend (slice toggles), tooltip, keyboard, data table, animation | the 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).
| Field | Type | Default | What it's for |
|---|---|---|---|
donutThickness | Float | 0.35 | ring width as a share of the radius (donut) |
sliceGap | Dp | 2 | the gap between slices (a line in the surface color) |
selectedOffset | Dp | 6 | how far a selected slice moves out |
labelThreshold | Float | 0.05 | the 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
- The angle function (pure), the sectors, the hole and center, selection, the canvas.
CamoPieCharton the frame, the presets.- 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.
- 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
valueFormatparameter. - 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 leastlabelThresholdand 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, ornullwhen it was already selected. The selected slice moves out byselectedOffset. - 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.
startAnglehas a public defaultPIE_TOP/pieTop(-90).
5. Edge cases
| Case | Decided behavior |
|---|---|
| Case | Decided behavior |
| --- | --- |
| Zero values | The slice has no angle but stays in the legend. |
| Negative values | Rejected in debug builds; treated as zero in release. |
| One slice | A full circle (a full ring for the donut), without a gap. |
| All zero | The empty state. |
| RTL | Clockwise 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
- Angles, sectors, the hole and center, selection, interaction.
- Tests: angles, hit testing, selection, summary.
Done when
- Works by touch, mouse, keyboard and screen reader.
- 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
valueFormatparameter. - 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 leastlabelThresholdand 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, ornullwhen it was already selected. The selected slice moves out byselectedOffset. - 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.
startAnglehas a public defaultPIE_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.
plotHeightandstyleparameters added. ChartRenderer.drawPieCharthas a default implementation, like the line and bar marks. A tap is read throughrememberUpdatedState, because the scene is rebuilt on every hover and would restart the gesture.
5. Edge cases
| Case | Decided behavior |
|---|---|
| Center content | A Box over the canvas, so it is a normal composable (text, an icon, a number). |
- None specific.