# zidane-ui/charts

Import: `import { … } from 'zidane-ui/charts'`. Source: [ui/src/components/charts/index.ts](https://github.com/Tahul/zidane/blob/main/ui/src/components/charts/index.ts).

## Components

### `AreaChart`

Component.

Renders one filled area per series, from zero or stacked. The pointer or the arrow keys
show every series' value at an x with a crosshair; a legend entry hides and shows its series.

```ts
const AreaChart: MemoExoticComponent<(__namedParameters: AreaChartProps) => Element>
```

Source: [ui/src/components/charts/AreaChart.tsx:51](https://github.com/Tahul/zidane/blob/main/ui/src/components/charts/AreaChart.tsx#L51)

### `BarChart`

Component.

Renders a bar chart, grouped or stacked, vertical or horizontal. The pointer or the arrow
keys show a row's values; a legend entry hides and shows its series.

```ts
const BarChart: MemoExoticComponent<(__namedParameters: BarChartProps) => Element>
```

Source: [ui/src/components/charts/BarChart.tsx:271](https://github.com/Tahul/zidane/blob/main/ui/src/components/charts/BarChart.tsx#L271)

### `LineChart`

Component.

Renders one line per series. The pointer or the arrow keys show every series' value at an
x with a crosshair; a series' end label or legend entry hides and shows it.

```ts
const LineChart: MemoExoticComponent<(__namedParameters: LineChartProps) => Element>
```

Source: [ui/src/components/charts/LineChart.tsx:94](https://github.com/Tahul/zidane/blob/main/ui/src/components/charts/LineChart.tsx#L94)

### `ScatterChart`

Component.

Renders one dot per point, colored by series. The pointer shows the nearest point with a
crosshair; the arrow keys step through the points in x order; a legend entry hides and shows
its series.

```ts
const ScatterChart: MemoExoticComponent<(__namedParameters: ScatterChartProps) => Element>
```

Source: [ui/src/components/charts/ScatterChart.tsx:239](https://github.com/Tahul/zidane/blob/main/ui/src/components/charts/ScatterChart.tsx#L239)

### `Sparkline`

Component.

Renders a trend line sized to its box, lowest value at the bottom.

```ts
const Sparkline: MemoExoticComponent<(__namedParameters: SparklineProps) => Element>
```

Source: [ui/src/components/charts/Sparkline.tsx:45](https://github.com/Tahul/zidane/blob/main/ui/src/components/charts/Sparkline.tsx#L45)

### `Trend`

Component.

Renders the latest value, its change since the first point, and the chart.

```ts
const Trend: MemoExoticComponent<(__namedParameters: TrendProps) => Element>
```

Source: [ui/src/components/charts/Trend.tsx:169](https://github.com/Tahul/zidane/blob/main/ui/src/components/charts/Trend.tsx#L169)

## Functions

### `fitText`

Function.

`text` cut with an ellipsis to fit `max` pixels in `font`, or whole when it fits.

```ts
function fitText(text: string, max: number, font: string): string
```

**Parameters**

- `text` (`string`): The label.
- `max` (`number`): The room, in CSS pixels.
- `font` (`string`): The CSS font shorthand it is drawn in.

**Returns** `string`

Source: [ui/src/components/charts/measure.ts:71](https://github.com/Tahul/zidane/blob/main/ui/src/components/charts/measure.ts#L71)

### `measureText`

Function.

The width of `text` in `font` (a CSS font shorthand, such as `10px system-ui`), in CSS pixels.

```ts
function measureText(text: string, font: string): number
```

**Parameters**

- `text` (`string`): The text to measure.
- `font` (`string`): The CSS font shorthand it is drawn in.

**Returns** `number`

Source: [ui/src/components/charts/measure.ts:39](https://github.com/Tahul/zidane/blob/main/ui/src/components/charts/measure.ts#L39)

### `seriesColor`

Function.

The color of series `index`, as a theme variable.

```ts
function seriesColor(index: number, count?: number): string
```

**Parameters**

- `index` (`number`): The series index, from 0. Past the last color the colors repeat.
- `count?` (`number`): How many series the chart has. Given: one series is the accent, and with
  more the first is the muted baseline and the next ones start at the accent. Left out:
  the plain palette, `--zg-chart-1` first.

**Returns** `string`

Source: [ui/src/components/charts/frame.tsx:35](https://github.com/Tahul/zidane/blob/main/ui/src/components/charts/frame.tsx#L35)

## Constants

### `BASELINE_COLOR`

Constant.

The muted color of a baseline series.

```ts
const BASELINE_COLOR = 'var(--zg-chart-baseline)'
```

Source: [ui/src/components/charts/frame.tsx:25](https://github.com/Tahul/zidane/blob/main/ui/src/components/charts/frame.tsx#L25)

### `SCATTER_LABELS_MAX`

Constant.

At most this many points carry a label (CH19): the subject, the extremes, the outliers.

```ts
const SCATTER_LABELS_MAX = 8
```

Source: [ui/src/components/charts/ScatterChart.tsx:41](https://github.com/Tahul/zidane/blob/main/ui/src/components/charts/ScatterChart.tsx#L41)

### `SERIES_COLORS`

Constant.

How many series colors the theme defines (`--zg-chart-1` to `--zg-chart-6`).

```ts
const SERIES_COLORS = 6
```

Source: [ui/src/components/charts/frame.tsx:22](https://github.com/Tahul/zidane/blob/main/ui/src/components/charts/frame.tsx#L22)

## Interfaces

### `AreaChartProps`

Interface.

Props for [`AreaChart`](https://zidane.si/docs/reference/ui-charts.md#AreaChart).

```ts
interface AreaChartProps extends XYChartProps
```

**Properties**

- `axisTitles?` (`{ x?: string; y?: string }`, from `XYChartProps`): The axis titles, with their units: `y` above the value axis, `x` under the plot.
- `className?` (`string`, from `XYChartProps`): Extra classes on the root element.
- `curve?` (`'linear' | 'smooth'`, default `'linear'`): `smooth` bends each edge through its points without overshoot (monotone).
- `format?` (`(value: number) => string`, from `XYChartProps`): Formats a y value for the axis. Default: the number in full, short past 10,000.
- `height?` (`number`, from `XYChartProps`): Height in pixels. Default: 180.
- `highlight?` (`ChartHighlight`, from `XYChartProps`): One series to emphasize (the others dim) and an x value to mark with a line.
- `label?` (`string`, from `XYChartProps`): The chart's accessible name.
- `marks?` (`readonly ChartMark[]`, from `XYChartProps`): Rules at an x, lines at a y and shaded x ranges, in data units.
- `series` (`readonly ChartSeries[]`, from `XYChartProps`): The series.
- `stacked?` (`boolean`): Stack the series. The series then share their x values by index: the first series' x
  values place every layer.
- `style?` (`CSSProperties`, from `XYChartProps`): Inline styles on the root element.
- `tickUnit?` (`boolean`, from `XYChartProps`, default `true`): The unit on the value axis's top tick. Off when the axis title says it.
- `time?` (`boolean`, from `XYChartProps`): x values are times in milliseconds.
- `title?` (`ReactNode`, from `XYChartProps`): A visible title above the plot, with the legend beside it.
- `unit?` (`string`, from `XYChartProps`): The y values' unit, for example `ms`, `%` or `$` (see `withUnit`).
- `width?` (`number`, from `XYChartProps`): Width in pixels. Default: the container's width.
- `xFormat?` (`(value: number) => string`, from `XYChartProps`): Formats an x value for the axis. Default: a number, or a short date with `time`.
- `zoomable?` (`boolean`, from `XYChartProps`): The wheel, a pinch and `+` / `-` zoom the x axis, with a reset (for a chart shown full screen).

Source: [ui/src/components/charts/AreaChart.tsx:32](https://github.com/Tahul/zidane/blob/main/ui/src/components/charts/AreaChart.tsx#L32)

### `BarChartProps`

Interface.

Props for [`BarChart`](https://zidane.si/docs/reference/ui-charts.md#BarChart).

```ts
interface BarChartProps
```

**Properties**

- `axisTitles?` (`{ x?: string; y?: string; y2?: string }`): The axis titles, with their units: `y` above the value axis, `y2` above the overlay's right axis, `x` under the plot.
- `className?` (`string`): Extra classes on the root element.
- `format?` (`(value: number) => string`): Formats a value for the axis and the data table. Default: the number in full, short past 10,000.
- `height?` (`number`): Height in pixels. Default: from the rows, at most 240 when vertical.
- `highlight?` (`ChartHighlight`): One series to emphasize (the others dim), and a row (`at`, its index) tinted behind its bars.
- `horizontal?` (`boolean`): Bars run left to right, categories down the left edge. Default: horizontal when a row
  label is longer than 12 characters or there are more than 8 rows.
- `label?` (`string`): The chart's accessible name.
- `marks?` (`readonly ChartMark[]`): Rules, reference lines and shaded ranges. `x`, `from` and `to` are row indexes (a range from
  2.5 to 4.5 covers rows 3 and 4); `y` is a value. Horizontal bars draw the value lines only.
- `overlay?` (`BarOverlay`): One series drawn as a line with dots over the bars (an average, a rate), on its own right axis when asked. Vertical bars only.
- `rows` (`readonly BarRow[]`): The categories, each with one value per series.
- `series` (`readonly string[]`): The series names, in the order of each row's values.
- `seriesColors?` (`readonly string[]`): Each series' color, as theme variables, in place of the color rule (a Trend's weekdays and weekends).
- `stacked?` (`boolean`): Stack the series in one bar. Default: side by side.
- `style?` (`CSSProperties`): Inline styles on the root element.
- `tickUnit?` (`boolean`, default `true`): The unit on the value axis's top tick. Off when the axis title says it.
- `title?` (`ReactNode`): A visible title above the plot, with the legend beside it.
- `unit?` (`string`): The values' unit, for example `ms`, `%` or `$` (see `withUnit`).
- `values?` (`boolean`): The value on each bar. Default: when there are 12 bars or fewer.
- `width?` (`number`): Width in pixels. Default: the container's width.
- `zoomable?` (`boolean`): Vertical bars: the wheel, a pinch and `+` / `-` zoom into the rows, with a reset (for a chart shown full screen).

Source: [ui/src/components/charts/BarChart.tsx:50](https://github.com/Tahul/zidane/blob/main/ui/src/components/charts/BarChart.tsx#L50)

### `BarOverlay`

Interface.

A series a [`BarChart`](https://zidane.si/docs/reference/ui-charts.md#BarChart) draws as a line over its bars.

```ts
interface BarOverlay
```

**Properties**

- `axis?` (`'right' | 'left'`): `right`: its own scale and axis on the right. Default: the bars' axis.
- `color?` (`string`): Its color, as a theme variable. Default: a color apart from the bars'.
- `dots?` (`boolean`, default `true`): Dots on its values while they stay apart.
- `format?` (`(value: number) => string`): Formats its values. Default: the chart's.
- `series` (`number`): The index of the series in `series`.
- `unit?` (`string`): Its unit, for the right axis and the tooltip. Default: the chart's.
- `values?` (`readonly (number | null)[]`): Its values per row, with `null` for a gap. Default: the rows' values.
- `width?` (`number`, default `2`): Its stroke width in pixels.

Source: [ui/src/components/charts/BarChart.tsx:100](https://github.com/Tahul/zidane/blob/main/ui/src/components/charts/BarChart.tsx#L100)

### `ChartSeries`

Interface.

One named series of points.

```ts
interface ChartSeries
```

**Properties**

- `label` (`string`): The series name, for the legend.
- `points` (`readonly ChartPoint[]`): The points, in x order for lines and areas.

Source: [ui/src/components/charts/xy.tsx:36](https://github.com/Tahul/zidane/blob/main/ui/src/components/charts/xy.tsx#L36)

### `LineChartProps`

Interface.

Props for [`LineChart`](https://zidane.si/docs/reference/ui-charts.md#LineChart).

```ts
interface LineChartProps extends XYChartProps
```

**Properties**

- `axisTitles?` (`{ x?: string; y?: string }`, from `XYChartProps`): The axis titles, with their units: `y` above the value axis, `x` under the plot.
- `className?` (`string`, from `XYChartProps`): Extra classes on the root element.
- `curve?` (`'linear' | 'smooth'`, default `'linear'`): `smooth` bends each line through its points without overshoot (monotone).
- `endLabel?` (`'name' | 'value' | 'none'`): What each line's end says. `name`: the series name (the default with 2 to 4 series, in
  place of a legend). `value`: the last value. `none`: nothing.
- `format?` (`(value: number) => string`, from `XYChartProps`): Formats a y value for the axis. Default: the number in full, short past 10,000.
- `height?` (`number`, from `XYChartProps`): Height in pixels. Default: 180.
- `highlight?` (`ChartHighlight`, from `XYChartProps`): One series to emphasize (the others dim) and an x value to mark with a line.
- `label?` (`string`, from `XYChartProps`): The chart's accessible name.
- `marks?` (`readonly ChartMark[]`, from `XYChartProps`): Rules at an x, lines at a y and shaded x ranges, in data units.
- `series` (`readonly ChartSeries[]`, from `XYChartProps`): The series.
- `style?` (`CSSProperties`, from `XYChartProps`): Inline styles on the root element.
- `tickUnit?` (`boolean`, from `XYChartProps`, default `true`): The unit on the value axis's top tick. Off when the axis title says it.
- `time?` (`boolean`, from `XYChartProps`): x values are times in milliseconds.
- `title?` (`ReactNode`, from `XYChartProps`): A visible title above the plot, with the legend beside it.
- `unit?` (`string`, from `XYChartProps`): The y values' unit, for example `ms`, `%` or `$` (see `withUnit`).
- `width?` (`number`, from `XYChartProps`): Width in pixels. Default: the container's width.
- `xFormat?` (`(value: number) => string`, from `XYChartProps`): Formats an x value for the axis. Default: a number, or a short date with `time`.
- `yMax?` (`number`): The highest value the y axis shows, at least.
- `yMin?` (`number`): The lowest value the y axis shows, at least.
- `zero?` (`boolean`): Start the y axis at zero. Default: only when a value is below zero or the smallest is under 20% of the largest.
- `zoomable?` (`boolean`, from `XYChartProps`): The wheel, a pinch and `+` / `-` zoom the x axis, with a reset (for a chart shown full screen).

Source: [ui/src/components/charts/LineChart.tsx:59](https://github.com/Tahul/zidane/blob/main/ui/src/components/charts/LineChart.tsx#L59)

### `ScatterChartProps`

Interface.

Props for [`ScatterChart`](https://zidane.si/docs/reference/ui-charts.md#ScatterChart).

```ts
interface ScatterChartProps extends XYChartProps
```

**Properties**

- `axisTitles?` (`{ x?: string; y?: string }`, from `XYChartProps`): The axis titles, with their units: `y` above the value axis, `x` under the plot.
- `className?` (`string`, from `XYChartProps`): Extra classes on the root element.
- `fit?` (`'none' | 'linear'`): `linear`: a dashed least squares line over every shown point, with r and its equation under the plot.
- `format?` (`(value: number) => string`, from `XYChartProps`): Formats a y value for the axis. Default: the number in full, short past 10,000.
- `height?` (`number`, from `XYChartProps`): Height in pixels. Default: 180.
- `highlight?` (`ChartHighlight`, from `XYChartProps`): One series to emphasize (the others dim) and an x value to mark with a line.
- `label?` (`string`, from `XYChartProps`): The chart's accessible name.
- `marks?` (`readonly ChartMark[]`, from `XYChartProps`): Rules at an x, lines at a y and shaded x ranges, in data units.
- `pointLabels?` (`readonly readonly (string | null | undefined)[][]`): A name per point, per series (`null` for none): drawn next to the point when there is room.
- `pointSizes?` (`readonly readonly (number | null | undefined)[][]`): A size per point, per series: the point's area follows it (a bubble chart).
- `radius?` (`number`, default `4.5`): Each point's radius in pixels, when no size is given.
- `series` (`readonly ChartSeries[]`, from `XYChartProps`): The series.
- `sizeFormat?` (`(value: number) => string`): Formats a size for the legend and the tooltip. Default: the number with its unit.
- `sizeLabel?` (`string`): What the size means, for the size legend.
- `style?` (`CSSProperties`, from `XYChartProps`): Inline styles on the root element.
- `subject?` (`string`): The name of the point the question is about: drawn larger in the accent, the others in the baseline color, and always labelled.
- `tickUnit?` (`boolean`, from `XYChartProps`, default `true`): The unit on the value axis's top tick. Off when the axis title says it.
- `time?` (`boolean`, from `XYChartProps`): x values are times in milliseconds.
- `title?` (`ReactNode`, from `XYChartProps`): A visible title above the plot, with the legend beside it.
- `unit?` (`string`, from `XYChartProps`): The y values' unit, for example `ms`, `%` or `$` (see `withUnit`).
- `width?` (`number`, from `XYChartProps`): Width in pixels. Default: the container's width.
- `xFormat?` (`(value: number) => string`, from `XYChartProps`): Formats an x value for the axis. Default: a number, or a short date with `time`.
- `xScale?` (`'log' | 'linear'`): `log`: a log x axis (every x above zero).
- `yScale?` (`'log' | 'linear'`): `log`: a log y axis (every y above zero).
- `zoomable?` (`boolean`, from `XYChartProps`): The wheel, a pinch and `+` / `-` zoom the x axis, with a reset (for a chart shown full screen).

Source: [ui/src/components/charts/ScatterChart.tsx:49](https://github.com/Tahul/zidane/blob/main/ui/src/components/charts/ScatterChart.tsx#L49)

### `SparklineProps`

Interface.

Props for [`Sparkline`](https://zidane.si/docs/reference/ui-charts.md#Sparkline).

```ts
interface SparklineProps
```

**Properties**

- `className?` (`string`): Extra classes on the SVG.
- `height?` (`number`, default `24`): Height in pixels.
- `highlight?` (`'none' | 'min' | 'max' | 'last'`): A dot on the last, the lowest or the highest value. Default: none.
- `label?` (`string`): Accessible name. Default: the `sparklineLabel` label.
- `style?` (`CSSProperties`): Inline styles on the SVG.
- `tone?` (`SparklineTone`, default `'series'`): The line's color.
- `values` (`readonly number[]`): The values, oldest first.
- `width?` (`number`, default `96`): Width in pixels.

Source: [ui/src/components/charts/Sparkline.tsx:21](https://github.com/Tahul/zidane/blob/main/ui/src/components/charts/Sparkline.tsx#L21)

### `TrendProps`

Interface.

Props for [`Trend`](https://zidane.si/docs/reference/ui-charts.md#Trend).

```ts
interface TrendProps
```

**Properties**

- `className?` (`string`): Extra classes on the root element.
- `format?` (`(value: number) => string`): Formats a value. Default: the number in full, short past 10,000.
- `goodWhen?` (`'neutral' | 'up' | 'down'`, default `'neutral'`): Which way is good: it colors the change. `neutral`: never colored.
- `height?` (`number`, default `270`): The chart's height in pixels.
- `highlight?` (`string`): The label of a point to mark on the chart.
- `marks?` (`readonly ChartMark[]`): Rules, reference lines and shaded ranges, x in point indexes (0 is the first point).
- `mode?` (`TrendMode`, default `'auto'`): `auto`: bars for daily counts over 14 to 120 days, else a line. `line` and `bars` force one.
- `points` (`readonly TrendPoint[]`): The points, oldest first.
- `size?` (`'stat' | 'headline'`, default `'stat'`): `headline`: the latest value at the figure step, for the one number that answers a view.
- `style?` (`CSSProperties`): Inline styles on the root element.
- `summary?` (`boolean`, default `true`): Show the latest value and its change above the chart. Off under a view's own answer (a stat
  row, a headline), where a second headline competes with it.
- `title?` (`ReactNode`): A title above the value. Also the chart's accessible name.
- `unit?` (`string`): The values' unit, for example `ms` (see `withUnit`).
- `width?` (`number`): The chart's width in pixels. Default: the container's width.
- `zero?` (`boolean`): Start the y axis at zero. Default: only when a value is below zero or the smallest is under 20% of the largest. Bars always start at zero.

Source: [ui/src/components/charts/Trend.tsx:27](https://github.com/Tahul/zidane/blob/main/ui/src/components/charts/Trend.tsx#L27)

### `XYChartProps`

Interface.

Props every x/y chart takes.

```ts
interface XYChartProps
```

**Properties**

- `axisTitles?` (`{ x?: string; y?: string }`): The axis titles, with their units: `y` above the value axis, `x` under the plot.
- `className?` (`string`): Extra classes on the root element.
- `format?` (`(value: number) => string`): Formats a y value for the axis. Default: the number in full, short past 10,000.
- `height?` (`number`): Height in pixels. Default: 180.
- `highlight?` (`ChartHighlight`): One series to emphasize (the others dim) and an x value to mark with a line.
- `label?` (`string`): The chart's accessible name.
- `marks?` (`readonly ChartMark[]`): Rules at an x, lines at a y and shaded x ranges, in data units.
- `series` (`readonly ChartSeries[]`): The series.
- `style?` (`CSSProperties`): Inline styles on the root element.
- `tickUnit?` (`boolean`, default `true`): The unit on the value axis's top tick. Off when the axis title says it.
- `time?` (`boolean`): x values are times in milliseconds.
- `title?` (`ReactNode`): A visible title above the plot, with the legend beside it.
- `unit?` (`string`): The y values' unit, for example `ms`, `%` or `$` (see `withUnit`).
- `width?` (`number`): Width in pixels. Default: the container's width.
- `xFormat?` (`(value: number) => string`): Formats an x value for the axis. Default: a number, or a short date with `time`.
- `zoomable?` (`boolean`): The wheel, a pinch and `+` / `-` zoom the x axis, with a reset (for a chart shown full screen).

Source: [ui/src/components/charts/xy.tsx:44](https://github.com/Tahul/zidane/blob/main/ui/src/components/charts/xy.tsx#L44)

## Types

### `BarRow`

Type.

One category: its label, then one value per series.

```ts
type BarRow = readonly [label: string, values: readonly number[]]
```

Source: [ui/src/components/charts/BarChart.tsx:47](https://github.com/Tahul/zidane/blob/main/ui/src/components/charts/BarChart.tsx#L47)

### `ChartPoint`

Type.

One point: x, then y. With `time`, x is a time in milliseconds.

```ts
type ChartPoint = readonly [x: number, y: number]
```

Source: [ui/src/components/charts/xy.tsx:33](https://github.com/Tahul/zidane/blob/main/ui/src/components/charts/xy.tsx#L33)

### `SparklineTone`

Type.

The color of a [`Sparkline`](https://zidane.si/docs/reference/ui-charts.md#Sparkline).

```ts
type SparklineTone = 'series' | 'positive' | 'negative' | 'neutral'
```

Source: [ui/src/components/charts/Sparkline.tsx:11](https://github.com/Tahul/zidane/blob/main/ui/src/components/charts/Sparkline.tsx#L11)

### `TrendMode`

Type.

How a [`Trend`](https://zidane.si/docs/reference/ui-charts.md#Trend) draws its points.

```ts
type TrendMode = 'auto' | 'line' | 'bars'
```

Source: [ui/src/components/charts/Trend.tsx:24](https://github.com/Tahul/zidane/blob/main/ui/src/components/charts/Trend.tsx#L24)

### `TrendPoint`

Type.

One point: its label (a date, a run) and its value.

```ts
type TrendPoint = readonly [label: string, value: number]
```

Source: [ui/src/components/charts/Trend.tsx:21](https://github.com/Tahul/zidane/blob/main/ui/src/components/charts/Trend.tsx#L21)
