# Breakdown chart

A measure split across slices and stacked by segments, the records it could not count named, and every figure repeated in a table.

Live variants: https://design.arcadia.is/components/breakdown-chart

```tsx
import { BreakdownChart } from "@arcadia/ui/charts";
```

## When to use

- A total for each of a few named groups, split into its parts
- One measure divided by two things, such as spend by provider and model
- A view where some records have no figure for the measure, and the lack must show

## When not to use

- One measure over time: use `Line`
- How much of a limit is left: use `Meter`
- Values a person looks up or copies, with nothing to compare: use `Table`

## In context

Compare providers and see how much each model used.

Switch to Cost to see which records lack a price.

```tsx
import { BreakdownChart } from "@arcadia/ui/charts";

<BreakdownChart
  title="Usage by provider"
  records={USAGE}
  dimensions={USAGE_DIMENSIONS}
  measures={USAGE_MEASURES}
  defaultMeasure="tokens"
  defaultSlice="provider"
  defaultSegment="model"
/>
```

## Usage

```tsx
import { BreakdownChart } from "@arcadia/ui/charts";

<BreakdownChart
  title="Usage by provider"
  records={USAGE}
  dimensions={USAGE_DIMENSIONS}
  measures={USAGE_MEASURES}
  defaultMeasure="tokens"
  defaultSlice="provider"
  defaultSegment="model"
/>
```

Pass individual records. The chart adds them by group.

A dash marks an unrecorded amount. Zero means a measured zero.

Set `segment="none"` to show one series without a legend.

## Anatomy

- Bar: one per slice, a button named with its slice and total. Hover, focus or tap reads its parts and marks its row in the table.
- Segment: the parts of a bar, in the legend's order, in the chart's six colours and Other.
- Share: a row’s portion of the recorded total, recalculated when a segment is hidden. Credits instead extend from zero in the opposite direction.
- Reading: the selected slice, its total and each segment’s amount, below the bars.
- Legend: a toggle for each segment, its swatch hollow when off. A segment turned off leaves the bars, the table and the sums, and the last one stays on.
- Table: every slice as a row, every segment and the total as columns, with the row it is reading lit at its edge.

### A workload

Counts of tasks by team, stacked by their state: nothing here is missing, so every cell holds a number.

```tsx
import { BreakdownChart } from "@arcadia/ui/charts";

<BreakdownChart
  title="Tasks by team"
  records={TASKS}
  dimensions={TASK_DIMENSIONS}
  measures={TASK_MEASURES}
  defaultSlice="team"
  defaultSegment="state"
/>
```

### One series

A slice with no segment takes one bar, in the first series colour, and the legend goes.

```tsx
import { BreakdownChart } from "@arcadia/ui/charts";

<BreakdownChart
  title="Tasks by team"
  records={TASKS}
  dimensions={TASK_DIMENSIONS}
  measures={TASK_MEASURES}
  defaultSlice="team"
  defaultSegment="none"
/>
```

## Missing amounts

Compare recorded spend with tokens from every record.

- Spend, as recorded
- Tokens, every record

Totals exclude unrecorded amounts.

## Reading

The pointer, a finger and the keys read the same bar, and the table marks the row it is on.

- Tab: Moves through the controls, into the plot and legend, then to the table if it scrolls.
- ↑ + ↓: Move between bars or through an open picker. Enter chooses the focused option.
- Esc: Closes the picker.
- A picker's trigger names itself and its value, such as Measure: Cost.
- Each bar's name states its slice and its total, and how many records have no amount.
- The table repeats every number, so nothing depends on colour or hover.

## States

### Nothing recorded

Every record has tokens, but none has a cost.

```tsx
import { BreakdownChart } from "@arcadia/ui/charts";

<BreakdownChart
  title="Spend by provider"
  records={USAGE.map((record) => ({ id: record.id, dimensions: record.dimensions, measures: { tokens: record.measures.tokens, cost: undefined } }))}
  dimensions={USAGE_DIMENSIONS}
  measures={USAGE_MEASURES}
  defaultMeasure="cost"
  defaultSlice="provider"
  defaultSegment="model"
  order="given"
/>
```

### No records

Nothing to break down, in the part's words or the caller's.

## API reference

### BreakdownChart

- `title` (`string`, required): The chart's name, over its plot: "Usage by provider".
- `records` (`readonly BreakdownRecord[]`, required): The rows to break down, one amount each: usage records, not summed totals.
- `dimensions` (`readonly BreakdownDimension[]`, required): The names a slice and its segments are picked from.
- `measures` (`readonly BreakdownMeasure[]`, required): The amounts a person can weigh. A record without one is never counted as zero.
- `titleAs` (`"h2" | "h3" | "h4"`, default `"h3"`): The heading level the title takes.
- `defaultMeasure` (`string`): The measure it opens on. Default: the first.
- `defaultSlice` (`string`): The dimension its bars are sliced by. Default: the first.
- `defaultSegment` (`string`): The dimension its bars stack by, or `"none"` for one series. Default: the first dimension the slice leaves.
- `measure` (`string`): The measure in force, held by the caller.
- `slice` (`string`): The slice in force, held by the caller.
- `segment` (`string`): The segment in force, held by the caller, or `"none"`.
- `onMeasureChange` (`(measure: string) => void`)
- `onSliceChange` (`(slice: string) => void`)
- `onSegmentChange` (`(segment: string) => void`)
- `empty` (`string`, default `"Nothing to break down"`): The empty state's words: what there is none of.
- `order` (`"total" | "given"`, default `"total"`): The order the slices run in: by recorded total, the largest first, or as the records first name them.
- `colorFor` (`(segment: string) => string`): A segment's paint, from `chartColor`. Default: its slot in the legend's order.
- `rootStyle` (`RootStyle<ChartKnobs>`): Typed StyleX override: where the chart sits, and its `--chart-h` minimum plot height.

Rest props go to `HTMLAttributes<HTMLDivElement>`.
