# Spark Bars

> Counts per day or week as word-sized columns, the latest in full color.

Page: https://uicontext.com/docs/components/spark-bars

Group: Charts.

## When to use

Counts per day or week as word-sized columns, the latest in full color. Use [Bar Chart](https://uicontext.com/docs/components/bar-chart.md) instead when the values need axes and labels.

## Usage

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

export const Deploys = <M>(h: Html.H<M>) => SparkBars(h, { label: "Deploys, last 14 days", values: [3, 5, 2, 0, 0, 4, 6, 3, 7, 2, 0, 0, 5, 4] })
```

## Examples

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

### In a table

The example the page opens with: Spark Bars in “In a table”.

```ts title="src/demo.ts" source="www/src/examples/spark-bars/demo.ts"
import type { Html } from "effect/ui/html"
import * as stylex from "@stylexjs/stylex"
import { SparkBars } from "./ui/uicontext/components/charts/SparkBars.ts"
import { sx } from "./ui/uicontext/components/Sx.ts"
import { color, font, space, stroke } from "./ui/uicontext/components/tokens.stylex.ts"

const styles = stylex.create({
	table: { inlineSize: "min(100%, 34rem)", borderCollapse: "collapse", fontSize: font.sm },
	th: { paddingBlock: space.sm, paddingInline: space.sm, textAlign: "start", fontSize: font.xs, fontWeight: 500, color: color.muted, borderBlockEndWidth: stroke.hairline, borderBlockEndStyle: "solid", borderBlockEndColor: color.line },
	td: { paddingBlock: space.sm, paddingInline: space.sm, textAlign: "start", borderBlockEndWidth: stroke.hairline, borderBlockEndStyle: "solid", borderBlockEndColor: color.line },
	num: { textAlign: "end", fontVariantNumeric: "tabular-nums" },
	failed: { color: color.danger },
})

const services = [
	{ name: "api-gateway", deploys: [3, 5, 2, 0, 0, 4, 6, 3, 7, 2, 0, 0, 5, 4], failed: 1 },
	{ name: "billing-worker", deploys: [1, 0, 0, 1, 0, 0, 0, 2, 0, 1, 0, 0, 0, 1], failed: 0 },
	{ name: "search-indexer", deploys: [0, 2, 4, 3, 0, 0, 1, 9, 12, 6, 0, 0, 3, 2], failed: 3 },
]

// deploys a day for two weeks, one row per service; today's column is the dark one
export const DeployTable = <M>(h: Html.H<M>) =>
	h.table.attrs(sx(styles.table)).children(
		h.thead.children(
			h.tr.children(
				h.th.attrs({ scope: "col", ...sx(styles.th) }).children("Service"),
				h.th.attrs({ scope: "col", ...sx(styles.th) }).children("Last 14 days"),
				h.th.attrs({ scope: "col", ...sx(styles.th, styles.num) }).children("Deploys"),
				h.th.attrs({ scope: "col", ...sx(styles.th, styles.num) }).children("Failed"),
			),
		),
		h.tbody.children(
			services.map((s) =>
				h.tr.key(s.name).children(
					h.th.attrs({ scope: "row", ...sx(styles.td) }).children(s.name),
					h.td.attrs(sx(styles.td)).children(SparkBars(h, { label: `${s.name} deploys a day, last 14 days`, values: s.deploys })),
					h.td.attrs(sx(styles.td, styles.num)).children(String(s.deploys.reduce((a, b) => a + b, 0))),
					h.td.attrs(sx(styles.td, styles.num, s.failed > 0 && styles.failed)).children(String(s.failed)),
				)),
		),
	)
```

### Across a stat card

`width: "fill"` takes the card's width; hours still to come are null. `highlight` picks out the busiest.

```ts title="src/stat.ts" source="www/src/examples/spark-bars/stat.ts"
import type { Html } from "effect/ui/html"
import * as stylex from "@stylexjs/stylex"
import { SparkBars } from "./ui/uicontext/components/charts/SparkBars.ts"
import { sx } from "./ui/uicontext/components/Sx.ts"
import { color, corner, font, space, stroke } from "./ui/uicontext/components/tokens.stylex.ts"

const styles = stylex.create({
	card: { display: "grid", gap: space.sm, inlineSize: "min(100%, 20rem)", padding: space.lg, borderWidth: stroke.hairline, borderStyle: "solid", borderColor: color.line, borderRadius: corner.card, backgroundColor: color.panel },
	label: { margin: 0, fontSize: font.sm, color: color.muted },
	value: { margin: 0, fontSize: font.xl, fontWeight: 600, fontVariantNumeric: "tabular-nums" },
	hint: { margin: 0, fontSize: font.xs, color: color.muted },
})

// signups an hour since midnight, filling the card's width; the busiest hour is the one picked out
const hours = [2, 1, 0, 0, 1, 3, 9, 18, 31, 42, 38, 35, 44, 51, 47, 40, 36, 29, null, null, null, null, null, null]
const busiest = hours.indexOf(Math.max(...hours.map((v) => v ?? 0)))

export const SignupsToday = <M>(h: Html.H<M>) =>
	h.section.attrs(sx(styles.card)).children(
		h.p.attrs(sx(styles.label)).children("Signups today"),
		h.p.attrs(sx(styles.value)).children("427"),
		SparkBars(h, { label: "Signups an hour, today", values: hours, width: "fill", height: 40, highlight: busiest, color: 3 }),
		h.p.attrs(sx(styles.hint)).children("Busiest at 13:00 · 51 signups"),
	)
```

## API reference

**Options**

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `values` (required) | `ReadonlyArray<number \| null>` | — | One number per period, oldest first; null for a period with no data. |
| `label` (required) | `string` | — | What it shows, for its accessible name: "Deploys, last 14 days". |
| `format` | `ChartFormat` | — | — |
| `width` | `number \| "fill"` | `96` | Pixels, or "fill" to take its container's width. 96 by default. |
| `height` | `number` | `24` | Pixels; 24 by default. |
| `highlight` | `number` | — | The column in full color; the last by default, -1 for all of them. |
| `color` | `ChartColor` | — | — |
| `locale` | `string` | — | — |
| `style` | `Style` | — | — |

## Installation

```sh
pnpm dlx shadcn@latest add checkfu/uicontext/spark-bars
```

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

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/Scale.ts`
- `src/ui/uicontext/components/charts/SparkBars.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/SparkBars.ts](https://github.com/checkfu/uicontext/blob/main/src/charts/SparkBars.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.
