# Bar chart

Values per category or period as bars, grouped or stacked, vertical or horizontal.

- Family: charts
- Import: `import { BarChart } from "@senvenli/react/charts";`
- Page: https://design.senvenli.com/docs/components/bar-chart
- JSON: https://design.senvenli.com/docs/components/bar-chart.json

## Example: Revenue by quarter

One series against last year's, side by side.

```tsx
import { BarChart } from "@senvenli/react/charts";

const REVENUE = [
  { quarter: "Q1", revenue: 184000, lastYear: 162000 },
  { quarter: "Q2", revenue: 212000, lastYear: 171000 },
  { quarter: "Q3", revenue: 198000, lastYear: 189000 },
  { quarter: "Q4", revenue: 246000, lastYear: 205000 },
];
const usd = new Intl.NumberFormat("en", { style: "currency", currency: "USD", notation: "compact" });

export default function Example() {
  return (
    <BarChart
      title="Revenue"
      period="2026 by quarter"
      data={REVENUE}
      index="quarter"
      categories={["revenue"]}
      comparison="lastYear"
      labels={{ revenue: "2026", lastYear: "2025" }}
      valueFormatter={usd.format}
    />
  );
}
```

## Examples

### Stacked

Parts of each period's total; at most five parts per stack.

```tsx
import { BarChart } from "@senvenli/react/charts";

const ORDERS = [
  { month: "Jan", web: 420, app: 260, phone: 90 },
  { month: "Feb", web: 455, app: 300, phone: 84 },
  { month: "Mar", web: 510, app: 365, phone: 78 },
  { month: "Apr", web: 480, app: 410, phone: 70 },
  { month: "May", web: 540, app: 470, phone: 66 },
  { month: "Jun", web: 590, app: 520, phone: 61 },
];

export default function Example() {
  return (
    <BarChart
      type="stacked"
      title="Orders by channel"
      period="Jan–Jun 2026"
      data={ORDERS}
      index="month"
      categories={["web", "app", "phone"]}
      labels={{ web: "Website", app: "Mobile app", phone: "Phone" }}
      allowDecimals={false}
    />
  );
}
```

### Horizontal

Long category names, ranked by value with the figure at the bar's end.

```tsx
import { BarChart } from "@senvenli/react/charts";

const PRODUCTS = [
  { product: "Standing desk, oak", revenue: 48200 },
  { product: "Ergonomic chair", revenue: 61900 },
  { product: "Monitor arm, dual", revenue: 23400 },
  { product: "Desk lamp", revenue: 12800 },
  { product: "Cable tray", revenue: 7600 },
];
const usd = new Intl.NumberFormat("en", { style: "currency", currency: "USD", maximumFractionDigits: 0 });

export default function Example() {
  // Horizontal bars sort themselves by value; pass sortBy="none" when the categories have their own order.
  return (
    <BarChart
      layout="horizontal"
      title="Revenue by product"
      period="March 2026"
      data={PRODUCTS}
      index="product"
      categories={["revenue"]}
      labels={{ revenue: "Revenue" }}
      indexLabel="Product"
      valueFormatter={usd.format}
    />
  );
}
```

### With a target line

Whole counts on the axis and a reference line to read them against.

```tsx
import { BarChart } from "@senvenli/react/charts";

const SIGNUPS = [
  { week: "Week 1", customers: 14 },
  { week: "Week 2", customers: 22 },
  { week: "Week 3", customers: 17 },
  { week: "Week 4", customers: 26 },
  { week: "Week 5", customers: 19 },
];

export default function Example() {
  return (
    <BarChart
      title="New customers"
      period="March 2026"
      data={SIGNUPS}
      index="week"
      categories={["customers"]}
      labels={{ customers: "New customers" }}
      indexLabel="Week"
      allowDecimals={false}
      referenceLines={[{ value: 20, label: "Target" }]}
    />
  );
}
```

### Waterfall: From revenue to profit

An opening total, the changes in order, and the closing total.

```tsx
import { BarChart } from "@senvenli/react/charts";

const usd = new Intl.NumberFormat("en", { style: "currency", currency: "USD", notation: "compact" });

export default function Example() {
  return (
    <BarChart type="waterfall"
      title="From revenue to profit"
      period="Q1 2026"
      start={{ name: "Revenue", value: 248000 }}
      steps={[
        { name: "Cost of goods", value: -96000 },
        { name: "Salaries", value: -64000 },
        { name: "Marketing", value: -21000 },
        { name: "Other income", value: 8000 },
      ]}
      endLabel="Net profit"
      valueFormatter={usd.format}
    />
  );
}
```

### Waterfall: A net change

Without a start, the running total begins at zero.

```tsx
import { BarChart } from "@senvenli/react/charts";

export default function Example() {
  return (
    <BarChart type="waterfall"
      title="Customer movement"
      period="March 2026"
      steps={[
        { name: "New", value: 184 },
        { name: "Reactivated", value: 23 },
        { name: "Downgraded to free", value: -37 },
        { name: "Canceled", value: -61 },
      ]}
      endLabel="Net change"
    />
  );
}
```

## Guidelines

### When to use it

- Comparing values across categories or periods.
- Explaining a change between two totals.

### Use something else when

- [bar-list](https://design.senvenli.com/docs/components/bar-list.md): A ranked list of names with values reads better.

## Accessibility

Keyboard:

- Arrow left / right: Moves between data points; the tooltip follows.

Built in:

- A "Show as table" button shows the same data as a table, which screen readers get by default.

You provide:

- A title that says what the figure is.

## API reference

### BarChart

| Prop | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| title | `string` |  | no | What the chart shows ("Income and expenses"), over it; leave it out when a card or a heading already says so. |
| aria-label | `string` |  | no | Names the chart when it has no visible `title`. |
| period | `string` |  | no | The period it covers ("September 2026"). |
| isLoading | `boolean` |  | no |  |
| emptyState | `React.ReactNode` |  | no | Shown instead of the plot when there is no data: an `EmptyState size="small"` or text; "No data" by default. |
| start | `WaterfallChartStep` |  | no | The opening total ("Opening balance"), a total bar from zero; the running total starts at 0 without it. A start without a finite value (NaN, ±Infinity) is left out the same way, with a warning in the console: no start bar, the running total starts at 0, and the end total is the finite steps' sum (never NaN). |
| steps | `readonly WaterfallChartStep[]` |  | no | The changes, in order: a positive value raises the running total, a negative one lowers it, and a zero leaves it (a neutral step: a flat 1.5px line at the running total, labeled "€ 0" with no sign, and no legend key of its own). Steps without a finite value are left out, with a warning in the console. When nothing moves off zero (no step, or every figure zero, start included), the chart counts it as nothing measured and shows the empty state. |
| endLabel | `string` |  | no | The closing total's name ("Net"): a total bar from zero to the start plus every step. |
| valueFormatter | `ChartValueFormatter` |  | no | Shared by the axis, the labels, the tooltip and the table ("€ 48.920"). Changes and a negative total are formatted by their size with a `+` or `−` before them, so the formatter sees non-negative values there. |
| height | `number` |  | no | The plot's height in px; 240 by default. |
| type | `"waterfall" \| "percent" \| "grouped" \| "stacked"` |  | no | `waterfall`: a start, the steps up and down, and the end, each bar starting where the last one ended. |
| total | `React.ReactNode` |  | no | The headline figure, in `metric-medium` ("€ 184,2k"). |
| data | `readonly T[]` |  | no |  |
| index | `keyof T & string` |  | no | The x key: the period or category of each row. |
| categories | `readonly (keyof T & string)[]` |  | no | The series keys, in order; the first is the product's own figure (`ds-chart-1`). |
| labels | `Readonly<Partial<Record<string, string>>>` |  | no | Series names by key ("web" → "Website"); the key by default. |
| indexFormatter | `(value: unknown) => string` |  | no | The index label in the tooltip and the table ("August 2026"). |
| indexLabel | `string` |  | no | The index column's header in the data table; "Period" for time charts, "Category" for BarChart. |
| allowDecimals | `boolean` |  | no | `false` for whole counts (orders, new customers): the value axis steps by whole numbers, so a small count reads 0 / 1 / 2 / 3, never 0.7 / 1.4 / 2.1. Fractional steps by default. Percent bars keep their 0–100% steps. |
| layout | `"horizontal" \| "vertical"` |  | no | `horizontal` for long category names, ranked; `vertical` columns by default. |
| comparison | `keyof T & string` |  | no | A comparison series (last year): `ds-chart-neutral`. |
| sortBy | `"none" \| "value"` |  | no | Horizontal bars are sorted by their total unless the categories have a natural order (`none`). |
| referenceLines | `readonly ChartReferenceLine[]` |  | no | An average or target line. |

### Tokens it reads

`--ds-background-brand-subtle`, `--ds-background-neutral`, `--ds-background-neutral-pressed`, `--ds-background-selected-hovered`, `--ds-background-selected-pressed`, `--ds-border`, `--ds-border-bold`, `--ds-border-focused`, `--ds-chart-1`, `--ds-chart-2`, `--ds-chart-3`, `--ds-chart-4`, `--ds-chart-5`, `--ds-chart-neutral`, `--ds-icon-danger`, `--ds-icon-success`, `--ds-icon-warning`, `--ds-link`, `--ds-radius-medium`, `--ds-radius-small`, `--ds-radius-xsmall`, `--ds-shadow-overlay`, `--ds-space-050`, `--ds-space-075`, `--ds-space-100`, `--ds-space-150`, `--ds-space-200`, `--ds-space-300`, `--ds-space-400`, `--ds-surface-overlay`, `--ds-surface-raised`, `--ds-text`, `--ds-text-danger`, `--ds-text-subtle`, `--ds-text-subtlest`, `--ds-text-success`

## Related

- [bar-list](https://design.senvenli.com/docs/components/bar-list.md)
