# SegmentedBar (/docs/charts/segmented-bar)

SegmentedBar splits one bar into adjacent segments, one per labeled category, that always sum to the whole: the read is
what a thing is made of, and in what proportions. Segments keep your data order by default, because sequences like
funnel stages and weekday mixes carry meaning that ranking destroys; `order="desc"` ranks them when they have none. Past
`maxSegments` the tail rolls into a labeled "Other" rather than being dropped, and summary percents use
largest-remainder rounding so they sum to exactly 100.

```tsx
<SegmentedBar
  data={[
    { label: "Chrome", value: 620 },
    { label: "Safari", value: 240 },
    { label: "Firefox", value: 90 },
    { label: "Edge", value: 30 },
    { label: "Arc", value: 20 },
  ]}
  title="Browser share"
  width={220}
  height={20}
/>
```

## Install

```tsx
import { SegmentedBar } from "@microcharts/react/segmented-bar";

<SegmentedBar data={mix} title="Browser share" />
```

Setup (package + stylesheet): [Quickstart](/docs/quickstart#set-up-with-an-ai-agent) or paste [`/agent-setup.md`](/agent-setup.md) into your agent.

## Try it

```tsx
import { SegmentedBar } from "@microcharts/react/segmented-bar/interactive";

<SegmentedBar
  data={mix}
/>
```

## When to use it

Use it for a traffic mix per table row or composition in a KPI card. For precise cross-row comparison use MiniBar; for
negative parts use Waterfall. This is also the replacement for the pie chart the catalog doesn't ship — see
[Design notes](/docs/design-notes).

## Sizing

**table cell**

```tsx
<SegmentedBar data={row.mix} width={60} height={10} />
```

**value labels**

```tsx
<SegmentedBar
  data={[
    { label: "Chrome", value: 620 },
    { label: "Safari", value: 240 },
    { label: "Firefox", value: 90 },
    { label: "Edge", value: 30 },
    { label: "Arc", value: 20 },
  ]}
  label="value"
  style={{ width: 160 }}
/>
```

## Variants

```tsx
// label="percent" is the default — segment shares when they fit.
<SegmentedBar
  data={[
    { label: "Chrome", value: 620 },
    { label: "Safari", value: 240 },
    { label: "Firefox", value: 140 },
  ]}
/>
```

```tsx
<SegmentedBar
  data={[
    { label: "Firefox", value: 90 },
    { label: "Chrome", value: 620 },
    { label: "Safari", value: 240 },
  ]}
  order="desc"
/>
```

```tsx
<SegmentedBar
  data={[
    { label: "Chrome", value: 12400 },
    { label: "Safari", value: 6200 },
  ]}
  label="value"
  locale="de-DE"
/>
```

`label="value"` follows `format`/`locale` — 12400 reads "12.400" under `de-DE` rather than the English "12,400".
`label="percent"` is unaffected: the largest-remainder shares are always plain integers, never locale-formatted, because
a percent point is the same everywhere.

## Edge cases

```tsx
<SegmentedBar data={[
  { label: "Chrome", value: 620 },
  { label: "Safari", value: 240 },
  { label: "Firefox", value: 90 },
  { label: "Edge", value: 30 },
  { label: "Arc", value: 12 },
  { label: "Brave", value: 8 },
]} label="percent" />
```

```tsx
<SegmentedBar data={[{ label: "Chrome", value: 1 }]} />
```

Past `maxSegments` (default 5) the smallest categories merge into a labeled "Other". Nothing is dropped, only rolled up,
and the interactive entry announces its member count. A single category still renders as one full-width, 100% segment
rather than a special-cased bare bar. A part-to-whole cannot contain negative parts, so negative values are excluded
from the bar entirely (not clamped to zero) and the component logs a dev warning steering toward Waterfall.

## Four homes

**In a sentence**

```tsx
<p>
  This week's sessions skew Chrome{" "}
  <span className="mc-inline">
    <SegmentedBar data={mix} width={90} height={14} summary={false} />
  </span>{" "}
  — 62% Chrome, 24% Safari, the rest long tail.
</p>
```

**In a table cell**

```tsx
<td>
  <SegmentedBar data={row.mix} width={90} height={12} />
</td>
```

**In a KPI card**

```tsx
<div className="kpi">
  <span className="figure">62%</span>
  <span className="unit">sessions on Chrome</span>
  <SegmentedBar data={mix} width={200} height={20} />
</div>
```

**In a tab header**

```tsx
<button className="tab">
  Marketing <SegmentedBar data={mix} width={54} height={12} />
</button>
```

## Accessibility

The accessible name is the full composition: **"Chrome 62%, Safari 24%, Firefox 9%, Edge 3%, Other 2%."** The
interactive entry roves the segments, announcing each one's share and value. The Other segment adds the total it stands
for and how many categories were folded into it (**"Other: 2%, 20 over 2 categories."**).

The interactive entry follows the shared [interaction contract](/docs/accessibility#one-interaction-contract):
arrow keys rove between units on both axes, `Home` and `End` jump to the ends, and a click, tap, `Enter` or
`Space` selects a unit — pinning its readout so it survives blur, until you select it again, press `Escape`, or
press outside the chart. On touch, a tap pins and a drag scrubs.

## Props

| Prop | Type | Description |
| --- | --- | --- |
| `data` (required) | `{ label; value }[]` | Parts of the whole. |
| `maxSegments` | `number` | Rollup threshold — the tail becomes a labeled Other. |
| `order` | `"data" \| "desc"` | Preserve inherent sequences or rank the composition. |
| `label` | `"none" \| "percent" \| "value"` | Centered per segment (deterministic drop-out; default percent). |
| `colors` | `string[]` | Per-segment colours, cycled; overrides --mc-cat-N. Other stays neutral. |
| `animate` | `boolean` | (interactive) Opt-in entrance motion when the chart mounts client-side — add `import "@microcharts/react/motion"` once. Inert on the server, on hydrated server HTML, and under `prefers-reduced-motion`. |

Plus the shared grammar — `data`, `domain`, `color`, `title`, `summary`, `format` — and the layout props (`width`, `height`, `className`, `style`) that every chart accepts. Interactive entries also share `animate` and `live`, and — wherever a chart has more than one navigable unit — `onActive`, `onSelect`, `selectedIndex` and `defaultSelectedIndex`; and — wherever the chart shows a hover value — `readout`. See [the shared grammar](/docs/quickstart#the-shared-grammar).
