# Axes

> Category, linear, logarithmic and time axes, how the x axis type is inferred, multiple value axes, and label formatting.

Cartesian charts (line, area, bar, scatter and bubble, and their combinations) have one x axis and one or more value axes. Configure them under `axes`:

```ts
createChart(container, {
  data,
  series,
  axes: {
    x: { title: 'Month', labelRotation: 0 },
    y: { title: 'Revenue (USD)', min: 0, tickCount: 6 },
  },
});
```

`axes.x` is one `AxisOptions` object. `axes.y` is one object or an array of them, one per value axis. Pie, donut and heatmap charts ignore most axis options; heatmaps use the x and y axes for their two category dimensions.

**`AxisOptions`** (interface from `@birchui/charts`, full reference: https://docs.birchui.com/api/charts.md)

```ts
interface AxisOptions
```

| Property | Type | Description |
| --- | --- | --- |
| `id?` | `string` |  |
| `type?` | `AxisType` |  |
| `position?` | `AxisPosition` |  |
| `title?` | `string` |  |
| `min?` | `number \| Date` |  |
| `max?` | `number \| Date` |  |
| `nice?` | `boolean` |  |
| `gridLines?` | `boolean` |  |
| `tickCount?` | `number` |  |
| `labelRotation?` | `number \| 'auto'` |  |
| `labelFormatter?` | `(value: AxisValue) => string` |  |
| `utc?` | `boolean` |  |
| `visible?` | `boolean` |  |
| `logBase?` | `number` |  |

## Axis types

| `type` | Positions values by | Typical data |
| --- | --- | --- |
| `category` | Evenly spaced bands, in the order values first appear | Months as labels, regions, product names |
| `linear` | Numeric value | Revenue, temperature, counts |
| `log` | Logarithm of the value, base `logBase` (default 10) | Values spanning several orders of magnitude |
| `time` | Timestamp | Daily sessions, sensor readings, order dates |

### How the x axis type is chosen

When `axes.x.type` is not set, the chart infers it from the first series' x values:

1. If any series is a bar or column series, the x axis is `category`, so every bar gets an evenly spaced slot, including dates.
2. `Date` objects give a `time` axis.
3. Numbers give a `linear` axis.
4. ISO date strings such as `'2026-03-01'` or `'2026-03-01T08:30:00Z'` give a `time` axis.
5. Anything else gives a `category` axis.

Set `type` explicitly to override the inference, for example to put columns on a time scale or to treat numeric fiscal years as categories:

```ts
axes: { x: { type: 'time' } }
axes: { x: { type: 'category' } }
```

Value axes are linear unless you set `type: 'log'`.

## Range and ticks

| Option | Meaning | Default |
| --- | --- | --- |
| `min`, `max` | Fix the ends of the axis. Numbers, or `Date` objects on time axes. | From the data |
| `nice` | Extend the range to round tick values | `true` for value axes; on the x axis only for charts with nothing but scatter or bubble series |
| `tickCount` | Approximate number of ticks | One tick per 52 pixels on vertical axes and per 96 pixels on horizontal axes, at least 2 |
| `gridLines` | Draw grid lines across the plot | On for the first value axis, off for the others and the x axis |
| `visible` | `false` hides the axis line, ticks and labels but keeps the scale | `true` |
| `title` | Axis title | None |
| `position` | `'left'`, `'right'`, `'top'` or `'bottom'` | Left for the first value axis, right for the others, bottom for the x axis |

```ts
axes: {
  y: { min: 0, max: 100, tickCount: 5, title: 'Uptime (%)' },
  x: { title: 'Week', gridLines: true },
}
```

## Category axes

Categories keep the order in which they first appear in the data. Dense category labels never overlap: by default (`labelRotation: 'auto'`) they rotate first, then are thinned to every second, third or nth label at a regular interval. Set `labelRotation` to a number of degrees to fix the angle, or to `0` to keep labels horizontal and rely on thinning alone.

```ts
axes: { x: { labelRotation: -45 } }
```

## Time axes

Time axes pick tick intervals from milliseconds to decades, and format each tick with `Intl.DateTimeFormat` in the chart's `locale`, so labels read "Mar 2026" for monthly ticks or "14:30" for half-hourly ones. The tooltip shows the full date and time at the granularity of the data.

```ts title="sensor-telemetry.ts"
const readings = await fetchReadings({ sensor: 'cold-store-3', hours: 48 });

createChart(document.querySelector<HTMLElement>('#telemetry')!, {
  title: 'Cold store 3 temperature',
  locale: 'de-DE',
  data: readings,
  series: [{ type: 'line', xKey: 'recordedAt', yKey: 'celsius', name: 'Temperature' }],
  axes: {
    x: { type: 'time', utc: true },
    y: { title: '°C', labelFormatter: (v) => `${Number(v).toFixed(1)} °C` },
  },
});
```

`utc` controls whether ticks and labels use UTC or local time. It defaults to `true` for date-only ISO strings such as `'2026-03-01'`, so a monthly series never shifts to the previous day in time zones west of UTC, and to local time otherwise.

## Logarithmic axes

```ts
axes: { y: { type: 'log', logBase: 10, title: 'Requests per minute' } }
```

Log axes label ticks in compact notation (1K, 10K, 100K). Zero and negative values cannot be shown on a log scale; when the data includes them, the axis starts at 1 or one decade below the maximum, whichever is smaller.

## Multiple value axes

Pass an array to `axes.y` to define several value axes, and point series at them with `yAxis`:

```ts title="revenue-and-orders.ts"
createChart(container, {
  title: 'Revenue and order count',
  data: monthly,
  series: [
    { type: 'column', xKey: 'month', yKey: 'revenue', name: 'Revenue' },
    { type: 'line', xKey: 'month', yKey: 'orders', name: 'Orders', yAxis: 'orders', markers: true },
  ],
  axes: {
    y: [
      { id: 'revenue', title: 'Revenue (USD)' },
      { id: 'orders', title: 'Orders', position: 'right' },
    ],
  },
});
```

- Axes without an `id` are named `'left'` (the first), `'right'` (the second) and `'y2'`, `'y3'` and so on.
- Series without `yAxis` use the first value axis.
- A `yAxis` that names an axis you did not declare creates one with default options, so `yAxis: 'right'` alone is enough for a quick secondary axis.
- Only the first value axis draws grid lines by default, which keeps the plot readable.
- The accessible summary describes each axis, for example "Y axis: $0 to $1.2M. Secondary Y axis: 0 to 3,000."

## Formatting labels

`labelFormatter(value)` returns the text for a tick. It receives:

| Axis | `value` |
| --- | --- |
| `linear`, `log` | The tick number |
| `time` | A `Date` |
| `category` | The original category value (string, number or `Date`) |

```ts
const usd = new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD', notation: 'compact' });
const month = new Intl.DateTimeFormat('en-GB', { month: 'short', year: '2-digit' });

axes: {
  y: { labelFormatter: (value) => usd.format(Number(value)) },
  x: { labelFormatter: (value) => (value instanceof Date ? month.format(value) : String(value)) },
}
```

Without a formatter, numbers use `Intl.NumberFormat` in the chart's `locale`, and percent-stacked axes add a `%` sign. Labels too long for their space are shortened with an ellipsis. If a formatter throws, the error is reported with `console.error` and the raw value is shown.

## Horizontal bar charts

In a chart with `horizontal: true` bars, the axes swap places: categories run down the left side and the value axis runs along the bottom (or the top, for a secondary value axis). `axes.x` still describes the category axis and `axes.y` the value axes, so you can switch orientation without rewriting the axis configuration.
