# Gauge

> One reading on a half dial, with bands that say whether it's fine and a mark for the target.

Page: https://uicontext.com/docs/components/gauge

Group: Charts.

## When to use

One reading on a half dial, with bands that say whether it's fine and a mark for the target. Use [Meter](https://uicontext.com/docs/components/meter.md) instead when the value sits in a row, not on its own.

## Usage

```ts
import { Gauge } from "./ui/uicontext/components/charts/Gauge.ts"

export const Storage = <M>(h: Html.H<M>) =>
	Gauge(h, { label: "Disk", value: 0.62, max: 1, format: { style: "percent" }, bands: [{ upTo: 0.8, tone: "good", label: "OK" }, { upTo: 1, tone: "danger", label: "Nearly full" }] })
```

## Examples

Each example is a complete file. Its imports point where the CLI installs the source.

### Service health

The example the page opens with: Gauge in “Service health”.

```ts title="src/demo.ts" source="www/src/examples/gauge/demo.ts"
import type { Html } from "effect/ui/html"
import * as stylex from "@stylexjs/stylex"
import { Gauge } from "./ui/uicontext/components/charts/Gauge.ts"
import { sx } from "./ui/uicontext/components/Sx.ts"
import { space } from "./ui/uicontext/components/tokens.stylex.ts"

const styles = stylex.create({ row: { display: "flex", flexWrap: "wrap", justifyContent: "center", gap: space.xl } })

const ms = { style: "unit", unit: "millisecond" } as const

// a service's health at a glance: latency against its budget, error rate against the SLO, CPU load
export const ServiceHealth = <M>(h: Html.H<M>) =>
	h.div.attrs(sx(styles.row)).children(
		Gauge(h, {
			label: "p95 latency",
			value: 182,
			max: 400,
			format: ms,
			target: 250,
			bands: [{ upTo: 250, tone: "good", label: "Within budget" }, { upTo: 320, tone: "warning", label: "Slow" }, { upTo: 400, tone: "danger", label: "Too slow" }],
		}),
		Gauge(h, {
			label: "Error rate",
			value: 0.0071,
			max: 0.01,
			format: { style: "percent", digits: 2 },
			bands: [{ upTo: 0.005, tone: "good", label: "Healthy" }, { upTo: 0.008, tone: "warning", label: "Near the SLO" }, { upTo: 0.01, tone: "danger", label: "Breaching" }],
		}),
		Gauge(h, {
			label: "CPU",
			value: 0.93,
			max: 1,
			format: { style: "percent", digits: 0 },
			bands: [{ upTo: 0.7, tone: "good", label: "OK" }, { upTo: 0.85, tone: "warning", label: "Busy" }, { upTo: 1, tone: "danger", label: "Saturated" }],
		}),
	)
```

### A score against a goal

Without `bands` the arc takes the first chart color. `target` marks the goal on the track.

```ts title="src/score.ts" source="www/src/examples/gauge/score.ts"
import type { Html } from "effect/ui/html"
import { Gauge } from "./ui/uicontext/components/charts/Gauge.ts"

// customer satisfaction this quarter, out of 5, with the team's goal marked
export const Satisfaction = <M>(h: Html.H<M>) =>
	Gauge(h, {
		label: "Customer satisfaction, Q4",
		value: 4.3,
		min: 1,
		max: 5,
		target: 4.5,
		size: 240,
		format: { style: "number", digits: 1 },
	})
```

## API reference

**Options**

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `label` (required) | `string` | — | What's measured: "p95 latency" |
| `value` (required) | `number` | — | — |
| `min` | `number` | — | — |
| `max` (required) | `number` | — | — |
| `format` | `ChartFormat` | — | — |
| `bands` | `GaugeBand[]` | — | Stretches of the track that mean something, low to high |
| `target` | `number` | — | A goal, marked with a tick on the track |
| `size` | `number` | `200` | Pixels wide; 200 by default |
| `locale` | `string` | — | — |
| `style` | `Style` | — | — |

## Installation

```sh
pnpm dlx shadcn@latest add checkfu/uicontext/gauge
```

With npm: `npx shadcn@latest add checkfu/uicontext/gauge`. With yarn: `yarn dlx shadcn@latest add checkfu/uicontext/gauge`. With bun: `bunx --bun shadcn@latest add checkfu/uicontext/gauge`.

It writes these files:

- `src/ui/uicontext/components/Sx.ts`
- `src/ui/uicontext/components/charts/Data.ts`
- `src/ui/uicontext/components/charts/Format.ts`
- `src/ui/uicontext/components/charts/Frame.ts`
- `src/ui/uicontext/components/charts/Gauge.ts`
- `src/ui/uicontext/components/charts/Scale.ts`
- `src/ui/uicontext/components/charts/chart.stylex.ts`
- `src/ui/uicontext/components/charts/slots.ts`
- `src/ui/uicontext/components/tokens.stylex.ts`

And installs the npm packages `@stylexjs/stylex`, `effect`.

## Source

- [src/charts/Gauge.ts](https://github.com/checkfu/uicontext/blob/main/src/charts/Gauge.ts)

## Related

- [Chart Foundations](https://uicontext.com/docs/components/chart-foundations.md) (`chart-foundations`): The scales, formats, legend and tooltip every chart shares.
- [Line Chart](https://uicontext.com/docs/components/line-chart.md) (`line-chart`): Values over time for one or many series, with dashed comparisons and forecasts.
- [Area Chart](https://uicontext.com/docs/components/area-chart.md) (`area-chart`): Totals over time and what makes them up, stacked or as shares of 100%.
- [Bar Chart](https://uicontext.com/docs/components/bar-chart.md) (`bar-chart`): Compare categories with grouped, stacked or sideways bars.
- [Combo Chart](https://uicontext.com/docs/components/combo-chart.md) (`combo-chart`): Bars and a line on one time axis, each read against its own scale.
- [Donut Chart](https://uicontext.com/docs/components/donut-chart.md) (`donut-chart`): Shares of one total, with the total in the middle.
