# Charts

Line, area, bar, pie and sparkline charts that share one set of colours.

```tsx
import { BarChart, LineChart, AreaChart, PieChart } from '@halo-ds/react';
```

## When to use it
Charts turn numbers into a shape people can read at a glance. Every chart uses the chart/series tokens, a light grid and the same axis labels, so a dashboard full of them still feels calm. Pick the chart by the question it answers: change over time, comparing amounts, or parts of a whole.

## Do
- Give every chart a title that says what it shows, like Revenue this week.
- Show the key number above the chart; the chart explains it.
- Start bar charts at zero so bars compare fairly.
- Use series 1 for the main data and series 6 for Other or last period.

## Don't
- Use more than six colours; group the rest as Other.
- Use purple for chart data; it belongs to the main button.
- Use pie charts to compare similar amounts; use bars.
- Rely on colour alone; label lines and show values in the tooltip.

## Props
### BarChart props
| Prop | Type | What it does |
| --- | --- | --- |
| `data` (required) | `ChartDatum[]` | One object per category, like { month: 'Jan', card: 120, transfer: 80 }. |
| `xKey` (required) | `string` | The field that names each category. |
| `series` (required) | `ChartSeries[]` |  |
| `label` (required) | `string` | What the chart shows. Read by screen readers. |
| `orientation` | `"vertical" \| "horizontal" \| undefined` | Vertical for months and short labels; horizontal for long category names and phones. Figma: Orientation. |
| `mode` | `"grouped" \| "stacked" \| undefined` | Side by side, or parts of a total. Only matters with two or more series. Figma: Type. |
| `height` | `number \| undefined` | Plot height for vertical bars. Horizontal bars grow with the number of rows. |
| `legend` | `boolean \| undefined` |  |
| `formatValue` | `((n: number) => string) \| undefined` |  |
| `formatX` | `((v: unknown) => ReactNode) \| undefined` |  |
| `categoryWidth` | `number \| undefined` | Width kept for category names in horizontal bars. |
| `classNames` | `Partial<Record<ChartPart, string>> \| undefined` | Extra class names for the parts inside the component, by part name, like `{ header: 'my-header' }`. |
| `styles` | `Partial<Record<ChartPart, CSSProperties>> \| undefined` | Inline styles for the parts inside the component, by part name, like `{ footer: { gap: 4 } }`. |
It also takes the usual HTML attributes for its element.

### LineChart props
| Prop | Type | What it does |
| --- | --- | --- |
| `data` (required) | `ChartDatum[]` | One object per point on the x axis, like { day: 'Mon', revenue: 420 }. |
| `xKey` (required) | `string` | The field that labels the x axis. |
| `series` (required) | `ChartSeries[]` | One or two lines usually read best. |
| `label` (required) | `string` | What the chart shows, like "Revenue this week". Read by screen readers. |
| `area` | `boolean \| undefined` | Fill under each line to show volume. Figma: Type=Area. |
| `curve` | `"smooth" \| "linear" \| undefined` | Curved or straight lines. |
| `height` | `number \| undefined` | Height of the plot in px. |
| `legend` | `boolean \| undefined` | Show the legend above the chart. Figma: Legend. |
| `formatValue` | `((n: number) => string) \| undefined` | Format numbers on the y axis and in the tooltip. |
| `formatX` | `((v: unknown) => ReactNode) \| undefined` | Format x labels. |
| `classNames` | `Partial<Record<ChartPart, string>> \| undefined` | Extra class names for the parts inside the component, by part name, like `{ header: 'my-header' }`. |
| `styles` | `Partial<Record<ChartPart, CSSProperties>> \| undefined` | Inline styles for the parts inside the component, by part name, like `{ footer: { gap: 4 } }`. |
It also takes the usual HTML attributes for its element.

### AreaChart props
| Prop | Type | What it does |
| --- | --- | --- |
| `legend` | `boolean \| undefined` | Show the legend above the chart. Figma: Legend. |
| `height` | `number \| undefined` | Height of the plot in px. |
| `label` (required) | `string` | What the chart shows, like "Revenue this week". Read by screen readers. |
| `data` (required) | `ChartDatum[]` | One object per point on the x axis, like { day: 'Mon', revenue: 420 }. |
| `xKey` (required) | `string` | The field that labels the x axis. |
| `series` (required) | `ChartSeries[]` | One or two lines usually read best. |
| `curve` | `"smooth" \| "linear" \| undefined` | Curved or straight lines. |
| `formatValue` | `((n: number) => string) \| undefined` | Format numbers on the y axis and in the tooltip. |
| `formatX` | `((v: unknown) => ReactNode) \| undefined` | Format x labels. |
| `classNames` | `Partial<Record<ChartPart, string>> \| undefined` | Extra class names for the parts inside the component, by part name, like `{ header: 'my-header' }`. |
| `styles` | `Partial<Record<ChartPart, CSSProperties>> \| undefined` | Inline styles for the parts inside the component, by part name, like `{ footer: { gap: 4 } }`. |
It also takes the usual HTML attributes for its element.

### PieChart props
| Prop | Type | What it does |
| --- | --- | --- |
| `data` (required) | `PieSlice[]` | Up to six slices read well; group the rest as "Other". |
| `label` (required) | `string` | What the chart shows. Read by screen readers. |
| `variant` | `"pie" \| "donut" \| undefined` | Donut leaves room for a total in the middle. Figma: Type. |
| `size` | `number \| undefined` | Diameter in px. |
| `centerLabel` | `ReactNode` | Small text in the middle of a donut, like "Total". |
| `centerValue` | `ReactNode` | Big text in the middle of a donut. Defaults to the total. |
| `legend` | `false \| "side" \| "bottom" \| "auto" \| undefined` | Legend beside the chart, or below it. "auto" moves it below on narrow screens. Figma: Layout. |
| `showPercent` | `boolean \| undefined` | Show percentages in the legend. |
| `formatValue` | `((n: number) => string) \| undefined` |  |
| `classNames` | `Partial<Record<ChartPart, string>> \| undefined` | Extra class names for the parts inside the component, by part name, like `{ header: 'my-header' }`. |
| `styles` | `Partial<Record<ChartPart, CSSProperties>> \| undefined` | Inline styles for the parts inside the component, by part name, like `{ footer: { gap: 4 } }`. |
It also takes the usual HTML attributes for its element.

## Examples
### Overview
```tsx
import { BarChart } from '@halo-ds/react';

const data = [
  { month: 'Jan', online: 120, store: 80 },
  { month: 'Feb', online: 150, store: 90 },
  { month: 'Mar', online: 170, store: 70 },
  { month: 'Apr', online: 210, store: 95 },
  { month: 'May', online: 190, store: 110 },
  { month: 'Jun', online: 240, store: 105 },
];

export default function Example() {
  return (
    <div style={{ width: '100%', maxWidth: 640 }}>
      <BarChart
        label="Sales by channel"
        data={data}
        xKey="month"
        series={[
          { key: 'online', label: 'Online' },
          { key: 'store', label: 'In store' },
        ]}
      />
    </div>
  );
}
```

### Line and area
Lines for change over time. A light fill under the line shows volume.
```tsx
import { AreaChart, LineChart } from '@halo-ds/react';

const week = [
  { day: 'Mon', users: 320 },
  { day: 'Tue', users: 410 },
  { day: 'Wed', users: 380 },
  { day: 'Thu', users: 520 },
  { day: 'Fri', users: 490 },
  { day: 'Sat', users: 300 },
  { day: 'Sun', users: 280 },
];

export default function Demo() {
  return (
    <div className="demo-stack" style={{ width: '100%', maxWidth: 560 }}>
      <LineChart label="Active users" data={week} xKey="day" series={[{ key: 'users', label: 'Active users' }]} height={200} />
      <AreaChart label="Active users" data={week} xKey="day" series={[{ key: 'users', label: 'Active users' }]} height={200} />
    </div>
  );
}
```

### Stacked and horizontal bars
Stacked bars show parts of a total. Horizontal bars fit long names and phones.
```tsx
import { BarChart } from '@halo-ds/react';

const months = [
  { month: 'Jan', card: 40, transfer: 24, cash: 12 },
  { month: 'Feb', card: 46, transfer: 28, cash: 10 },
  { month: 'Mar', card: 52, transfer: 30, cash: 9 },
];
const sources = [
  { source: 'Search', visits: 4200 },
  { source: 'Social media', visits: 2600 },
  { source: 'Newsletter', visits: 1500 },
];

export default function Demo() {
  return (
    <div className="demo-stack" style={{ width: '100%', maxWidth: 560 }}>
      <BarChart
        label="Payments by method"
        mode="stacked"
        data={months}
        xKey="month"
        series={[
          { key: 'card', label: 'Card' },
          { key: 'transfer', label: 'Transfer' },
          { key: 'cash', label: 'Cash' },
        ]}
      />
      <BarChart label="Visits by source" orientation="horizontal" data={sources} xKey="source" series={[{ key: 'visits', label: 'Visits' }]} />
    </div>
  );
}
```

### Donut and pie
Parts of a whole. The donut shows a total in the middle.
```tsx
import { PieChart } from '@halo-ds/react';

const spending = [
  { label: 'Bills', value: 194 },
  { label: 'Food', value: 122 },
  { label: 'Transport', value: 97 },
  { label: 'Shopping', value: 73 },
];

export default function Demo() {
  return (
    <div className="demo-row" style={{ gap: 40 }}>
      <PieChart label="Spending by category" centerLabel="Spent" data={spending} />
      <PieChart label="Spending by category" variant="pie" data={spending} />
    </div>
  );
}
```

More: https://halodesignsystem.dev/components/charts
