# ShiftHistogram (/docs/charts/shift-histogram)

ShiftHistogram mirrors two histograms around a center line: **before** upward (muted), **after** downward (accent), on
**shared bin edges**. Bar heights are per-side proportions (each side's counts over its own n) on one shared scale, so a
bigger sample on one side cannot fake a shift. The mirror carries **identity**, not valence — up is "before", not
"good". The median shift is the precise takeaway, and the medians are never smoothed or trimmed.

```tsx
<ShiftHistogram
  data={{
    before: Array.from({ length: 100 }, (_, i) => 120 + (i % 40) - 20),
    after: Array.from({ length: 100 }, (_, i) => 96 + (i % 40) - 20),
  }}
  format={{ style: "unit", unit: "millisecond", unitDisplay: "short", maximumFractionDigits: 0 }}
  title="The fix"
  width={260}
  height={30}
/>
```

## Install

```tsx
import { ShiftHistogram } from "@microcharts/react/shift-histogram";

<ShiftHistogram data={{ before, after }} title="The fix" />
```

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 { ShiftHistogram } from "@microcharts/react/shift-histogram/interactive";

<ShiftHistogram
  data={{ before, after }}
/>
```

## When to use it

Use it for a "the fix, proven" read in a KPI card, before/after distributions in an experiments table, and showing that
a change is more than a mean move. For a single distribution use HistogramStrip; for two labelled arms use ABStrips.

## Sizing

**overlay for similar shapes**

```tsx
const before = Array.from({ length: 100 }, (_, i) => 120 + (i % 40) - 20);
const after = Array.from({ length: 100 }, (_, i) => 96 + (i % 40) - 20);

<ShiftHistogram data={{ before, after }} mode="overlay" />
```

**no real shift**

```tsx
const before = Array.from({ length: 100 }, (_, i) => 120 + (i % 40) - 20);

<ShiftHistogram data={{ before, after: before }} />
```

## Variants

```tsx
const before = Array.from({ length: 100 }, (_, i) => 120 + (i % 40) - 20);
const after = Array.from({ length: 100 }, (_, i) => 96 + (i % 40) - 20);

<ShiftHistogram data={{ before, after }} mode="overlay" />
<ShiftHistogram data={{ before, after: before }} />
```

```tsx
<ShiftHistogram
  data={{ before: [12000, 12400, 12800], after: [9600, 10000, 10400] }}
  locale="de-DE"
/>
```

The median shift label and accessible summary follow `format`/`locale`. Under `de-DE` the before median above reads
"12.400" rather than the English "12,400". `mode="overlay"` draws the after bins above the axis as an outline over the
before fill instead of mirroring them below it; the bins stay shared either way.

## Edge cases

```tsx
// the after sample hasn't landed yet — before renders alone
const before = Array.from({ length: 100 }, (_, i) => 120 + (i % 40) - 20);

<ShiftHistogram
  data={{ before, after: [] }}
  format={(n) => Math.round(n) + " ms"}
  title="Awaiting after sample"
/>
```

```tsx
<ShiftHistogram data={{ before: [], after: [] }} title="No data yet" />
```

With one side empty the chart still renders the populated side's bins alone, and the summary names the gap: **"Median
116 ms; no after sample."** It never reports a fabricated shift. With both sides empty nothing is plottable and the
accessible name says **"No data."**

## Four homes

**In a sentence**

```tsx
<p>
  The fix, proven{" "}
  <span className="mc-inline">
    <ShiftHistogram data={{ before, after }} summary={false} />
  </span>{" "}
  — latency distribution shifted left after deploy.
</p>
```

**In a table cell**

```tsx
<td>
  <ShiftHistogram data={{ before, after }} />
</td>
```

**In a KPI card**

```tsx
<div className="kpi">
  <span className="figure">−20ms</span>
  <span className="unit">after fix</span>
  <ShiftHistogram data={{ before, after }} />
</div>
```

**In a tab header**

```tsx
<button className="tab">
  checkout <ShiftHistogram data={{ before, after }} />
</button>
```

## Accessibility

Side identity rides on position (up is before, down is after), so it survives grayscale and forced-colors. The
accessible name states the median shift: **"Median fell from 116 ms to 92 ms."** It appends "On N / M samples" when the
sample sizes differ. The interactive entry steps the bins and announces each bin's before/after proportions; **M** jumps
to the two median bins.

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) | `{ before: number[]; after: number[] }` | The two samples — raw observations, shared bin edges are derived. |
| `bins` | `number` | Shared bin count (default auto, Sturges capped at 12). |
| `mode` | `"mirror" \| "overlay"` | Mirror (default) or after-as-outline over before fill. |
| `seriesLabels` | `[string, string]` | Side identities for the summary (default ['before', 'after']). |
| `label` | `"shift" \| "none"` | Signed median shift in a right gutter. |
| `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).
