# Histogram chart

A distribution that answers how many and how bad the tail is: round bins with a width slider, percentile lines and a target with the share past it.

> chatcn Pro: needs a Pro license (https://chatcn.dev/pro). Install from the `@chatcn-pro` registry with your license token; setup at https://chatcn.dev/docs/chatcn-pro.

Example: Histogram chart. A day of checkout API response times against an 800 ms target. Drag the slider to change the bin width. Preview: https://chatcn.dev/preview/histogram-chart-demo

## Installation

```bash
npx shadcn@latest add @chatcn-pro/histogram-chart
```

## Usage

```tsx
import { HistogramChart } from "@/components/ui/chatcn/pro/histogram-chart"

<HistogramChart
  title="Checkout API response time"
  values={responseTimes}
  unit="ms"
  valueFormat="integer"
  markers={["median", "p90", "p99"]}
  headline="p99"
  target={{ value: 800, label: "the 800 ms target" }}
/>
```

Pass the raw values and the chart bins them. Bin widths are always round, 1, 2, 2.5 or 5 times a power of ten, starting near the Freedman Diaconis rule; the slider steps through the round widths from coarse to fine, so a bin never reads 37.4 to 74.8. Bins are rounded with small gaps and a soft vertical gradient; they sit as a quiet tint and the bin you point at takes the full color. Median, mean and percentile lines carry their values in pills above the plot, sliding aside or stepping to a second row when two would touch. A target draws a red line with its own pill and hatches the bad side (above by default, `bad: "below"` for things like conversion), and the readout says what share is past it. `headline` sets one statistic large under the title, with the share past the target in a chip beside it. `variant="density"` steps the bins back and lays a smoothed density curve over them, with a soft fill, for reading the shape. Point at a bin, or walk them with the arrow keys, for a card with its range, count, share and the share at or below it. The table lists every bin. `kernelDensity` is exported for drawing the same curve elsewhere.

## Examples

### States

Example: States. Order values in a narrow frame with fixed bins, loading, and a failed load. Preview: https://chatcn.dev/preview/histogram-chart-states

## Props

| Prop | Type | Notes |
| --- | --- | --- |
| `values` | `number[]` | Raw values; non finite ones are dropped |
| `markers` | `HistogramMarker[]` | `mean`, `median`, `p90`, `p95`, `p99`. Default median and p90 |
| `target` | `{ value, label?, bad? }` | A limit with its bad side hatched |
| `variant` | `"bars" | "density"` | Density lays a smoothed curve over quiet bins |
| `headline` | `HistogramMarker` | One statistic set large, with the share past the target |
| `binWidth`, `defaultBinWidth`, `onBinWidthChange` | `number` | Default a round width near the Freedman Diaconis rule |
| `fixedBins` | `boolean` | Hides the slider |
| `unit`, `valueFormat`, `currency`, `locale` |  | Formatting |
| `height` | `number` | Default 240 |
| `loading`, `error`, `onRetry` |  |  |
| `labels` | `Partial<HistogramChartLabels>` | Every string, for translation |

## Keyboard

| Key | Action |
| --- | --- |
| Tab | Focus the chart, then the slider |
| Arrow left, right | Previous or next bin |
| Home, End | First or last bin |

## Accessibility

- The chart is summarized in words with the count, the range and the median, with a table of every bin.
- The slider reads the bin width with its unit.
- The zone past the target is hatched as well as tinted, and the share is in words.

## Motion

Bars grow from the baseline once over 760 ms with a short left to right stagger, the density curve draws in, and the pills fade in with them. Under reduced motion they appear drawn.

Source: https://chatcn.dev/docs/components/histogram-chart