Brush chart
Zoom a dense ordered series with two native window handles, pan or reset the window, and inspect values and named events.
pnpm dlx shadcn@latest add @haze/brush-chart---
import { ChevronDown } from "@lucide/astro";
import { cn } from "../../../lib/haze/utils";
interface Point{label:string;value:number;event?:string}
interface Props{label?:string;data?:Point[];unit?:string;initialWindow?:number;class?:string}
const{label="Daily build volume",unit="builds",initialWindow=12,data=Array.from({length:36},(_,i)=>({label:"Day "+(i+1),value:70+(i*17%43)+Math.round(Math.sin(i*.6)*24),event:i===18?"Build cache enabled":i===29?"Release candidate":undefined})),class:className}=Astro.props;
const points=data.filter(point=>Number.isFinite(point.value)),start=Math.max(0,points.length-(Number.isFinite(initialWindow)?Math.max(1,Math.floor(initialWindow)):12)),end=Math.max(0,points.length-1);
const layout=(items:Point[])=>{const lo=Math.min(...items.map(p=>p.value)),hi=Math.max(...items.map(p=>p.value)),magnitude=Math.max(Math.abs(lo),Math.abs(hi),1);return items.map((p,i)=>[items.length===1?150:i/(items.length-1)*300,hi===lo?100:180-(p.value/magnitude-lo/magnitude)/(hi/magnitude-lo/magnitude)*160]);};
const path=(items:Point[])=>layout(items).map((p,i)=>(i?"L":"M")+p.join(" ")).join(" ");
---
<figure data-haze-brush-chart data-points={JSON.stringify(points)} data-unit={unit} class={cn("grid min-w-0 gap-3 rounded-xl border border-border bg-card p-4.5 text-card-foreground",className)}>
<figcaption class="text-[0.9375rem]/5 font-medium tracking-[-0.01em] wrap-anywhere">{label}</figcaption>
{points.length?<><svg data-brush-main aria-hidden="true" viewBox="0 0 300 200" preserveAspectRatio="none" class="h-48 w-full"><path d="M0 20H300M0 100H300M0 180H300" vector-effect="non-scaling-stroke" class="stroke-border"/><path data-brush-line d={path(points.slice(start,end+1))} fill="none" stroke-width="2" stroke-linecap="round" vector-effect="non-scaling-stroke" class="stroke-brand"/><path data-brush-crosshair d="M300 15V185" vector-effect="non-scaling-stroke" class="stroke-muted-foreground" stroke-dasharray="3 3"/></svg><div class="flex justify-between gap-2 text-xs/5 text-muted-foreground"><span data-brush-first class="min-w-0 wrap-anywhere">{points[start].label}</span><span data-brush-last class="min-w-0 text-right wrap-anywhere">{points[end].label}</span></div>
<svg data-brush-overview aria-hidden="true" viewBox="0 0 300 200" preserveAspectRatio="none" class="h-12 w-full touch-pan-y rounded-lg bg-foreground/5"><path d={path(points)} fill="none" stroke-width="2" vector-effect="non-scaling-stroke" class="stroke-muted-foreground"/><rect data-brush-window x={points.length===1?0:start/(points.length-1)*300} y="0" width={points.length===1?300:(end-start)/(points.length-1)*300} height="200" class="fill-brand/10 stroke-brand" stroke-width="1" vector-effect="non-scaling-stroke"/></svg>
<div data-brush-controls hidden class="grid gap-3"><label class="grid gap-1 text-[0.8125rem]/5 font-medium text-foreground">Window starts<input data-brush-start aria-label="Window start" type="range" min="0" max={end} value={start} step="1" class="h-10 w-full min-w-0 cursor-pointer appearance-none rounded-md bg-transparent focus-visible:outline-2 focus-visible:outline-offset-1 focus-visible:outline-ring disabled:cursor-not-allowed [&::-webkit-slider-runnable-track]:h-1.5 [&::-webkit-slider-runnable-track]:rounded-full [&::-webkit-slider-runnable-track]:bg-input [&::-webkit-slider-thumb]:-mt-2 [&::-webkit-slider-thumb]:size-5.5 [&::-webkit-slider-thumb]:appearance-none [&::-webkit-slider-thumb]:rounded-full [&::-webkit-slider-thumb]:border-0 [&::-webkit-slider-thumb]:bg-foreground [&::-webkit-slider-thumb]:shadow-key [&::-webkit-slider-thumb]:transition-[scale] [&::-webkit-slider-thumb]:duration-150 [&::-webkit-slider-thumb]:ease-snappy enabled:active:[&::-webkit-slider-thumb]:scale-95 [&::-moz-range-track]:h-1.5 [&::-moz-range-track]:rounded-full [&::-moz-range-track]:bg-input [&::-moz-range-thumb]:size-5.5 [&::-moz-range-thumb]:rounded-full [&::-moz-range-thumb]:border-0 [&::-moz-range-thumb]:bg-foreground [&::-moz-range-thumb]:shadow-key [&::-moz-range-thumb]:transition-[scale] [&::-moz-range-thumb]:duration-150 [&::-moz-range-thumb]:ease-snappy enabled:active:[&::-moz-range-thumb]:scale-95 forced-colors:appearance-auto"/></label><label class="grid gap-1 text-[0.8125rem]/5 font-medium text-foreground">Window ends<input data-brush-end aria-label="Window end" type="range" min="0" max={end} value={end} step="1" class="h-10 w-full min-w-0 cursor-pointer appearance-none rounded-md bg-transparent focus-visible:outline-2 focus-visible:outline-offset-1 focus-visible:outline-ring disabled:cursor-not-allowed [&::-webkit-slider-runnable-track]:h-1.5 [&::-webkit-slider-runnable-track]:rounded-full [&::-webkit-slider-runnable-track]:bg-input [&::-webkit-slider-thumb]:-mt-2 [&::-webkit-slider-thumb]:size-5.5 [&::-webkit-slider-thumb]:appearance-none [&::-webkit-slider-thumb]:rounded-full [&::-webkit-slider-thumb]:border-0 [&::-webkit-slider-thumb]:bg-foreground [&::-webkit-slider-thumb]:shadow-key [&::-webkit-slider-thumb]:transition-[scale] [&::-webkit-slider-thumb]:duration-150 [&::-webkit-slider-thumb]:ease-snappy enabled:active:[&::-webkit-slider-thumb]:scale-95 [&::-moz-range-track]:h-1.5 [&::-moz-range-track]:rounded-full [&::-moz-range-track]:bg-input [&::-moz-range-thumb]:size-5.5 [&::-moz-range-thumb]:rounded-full [&::-moz-range-thumb]:border-0 [&::-moz-range-thumb]:bg-foreground [&::-moz-range-thumb]:shadow-key [&::-moz-range-thumb]:transition-[scale] [&::-moz-range-thumb]:duration-150 [&::-moz-range-thumb]:ease-snappy enabled:active:[&::-moz-range-thumb]:scale-95 forced-colors:appearance-auto"/></label><label class="grid gap-1 text-[0.8125rem]/5 font-medium text-foreground">Read within window<input data-brush-cursor aria-label="Reading in selected window" type="range" min={start} max={end} value={end} step="1" class="h-10 w-full min-w-0 cursor-pointer appearance-none rounded-md bg-transparent focus-visible:outline-2 focus-visible:outline-offset-1 focus-visible:outline-ring disabled:cursor-not-allowed [&::-webkit-slider-runnable-track]:h-1.5 [&::-webkit-slider-runnable-track]:rounded-full [&::-webkit-slider-runnable-track]:bg-input [&::-webkit-slider-thumb]:-mt-2 [&::-webkit-slider-thumb]:size-5.5 [&::-webkit-slider-thumb]:appearance-none [&::-webkit-slider-thumb]:rounded-full [&::-webkit-slider-thumb]:border-0 [&::-webkit-slider-thumb]:bg-foreground [&::-webkit-slider-thumb]:shadow-key [&::-webkit-slider-thumb]:transition-[scale] [&::-webkit-slider-thumb]:duration-150 [&::-webkit-slider-thumb]:ease-snappy enabled:active:[&::-webkit-slider-thumb]:scale-95 [&::-moz-range-track]:h-1.5 [&::-moz-range-track]:rounded-full [&::-moz-range-track]:bg-input [&::-moz-range-thumb]:size-5.5 [&::-moz-range-thumb]:rounded-full [&::-moz-range-thumb]:border-0 [&::-moz-range-thumb]:bg-foreground [&::-moz-range-thumb]:shadow-key [&::-moz-range-thumb]:transition-[scale] [&::-moz-range-thumb]:duration-150 [&::-moz-range-thumb]:ease-snappy enabled:active:[&::-moz-range-thumb]:scale-95 forced-colors:appearance-auto"/></label><div class="flex flex-wrap gap-2">{["Earlier","Later","All data","Latest 7"].map((text,index)=><button type="button" data-brush-preset={index} class="min-h-8 rounded-lg border border-frame bg-field px-2 text-xs/5 transition-colors duration-150 ease-snappy hover:bg-accent active:scale-[0.97] focus-visible:outline-2 focus-visible:outline-offset-1 focus-visible:outline-ring">{text}</button>)}</div></div></>:<p class="text-[0.8125rem]/5 text-muted-foreground">No finite readings to explore.</p>}
<p data-brush-readout role="status" aria-live="polite" class="min-h-10 text-xs/5 text-muted-foreground">Resize the window with its two native range handles; the overview retains the complete series.</p>
<details class="group min-w-0 border-t border-border pt-3"><summary class="flex min-h-7 cursor-pointer list-none items-center justify-between gap-2 rounded-sm text-[0.8125rem]/5 font-medium text-muted-foreground transition-colors duration-150 ease-snappy hover:text-foreground focus-visible:outline-2 focus-visible:outline-offset-1 focus-visible:outline-ring [&::-webkit-details-marker]:hidden">All readings and events<ChevronDown aria-hidden="true" class="size-3.5 shrink-0 transition-[rotate] duration-200 ease-snappy group-open:rotate-180" /></summary><table class="mt-3 w-full table-fixed text-left text-xs/5"><caption class="sr-only">{label}</caption><thead><tr><th scope="col" class="p-1">Period</th><th scope="col" class="p-1">Value / event</th></tr></thead><tbody>{points.map(point=><tr class="border-t border-border"><th scope="row" class="p-1 font-normal wrap-anywhere">{point.label}</th><td class="p-1 tabular-nums wrap-anywhere">{point.value} {unit}{point.event?" · "+point.event:""}</td></tr>)}</tbody></table></details>
</figure>
<script>
function initBrush(){document.querySelectorAll<HTMLElement>("[data-haze-brush-chart]:not([data-enhanced])").forEach(root=>{
root.dataset.enhanced="";const points:{label:string;value:number;event?:string}[]=JSON.parse(root.dataset.points||"[]"),start=root.querySelector<HTMLInputElement>("[data-brush-start]"),end=root.querySelector<HTMLInputElement>("[data-brush-end]"),cursor=root.querySelector<HTMLInputElement>("[data-brush-cursor]"),line=root.querySelector<SVGPathElement>("[data-brush-line]");if(!start||!end||!cursor||!line)return;root.querySelector("[data-brush-controls]")?.removeAttribute("hidden");let previous:number[][]=[],raf=0;
const layout=()=>{const from=Number(start.value),to=Number(end.value),items=points.slice(from,to+1),lo=Math.min(...items.map(p=>p.value)),hi=Math.max(...items.map(p=>p.value)),scale=Math.max(Math.abs(lo),Math.abs(hi),1);return Array.from({length:65},(_,i)=>{const at=i/64*(items.length-1),j=Math.floor(at),t=at-j,value=items[j].value/scale*(1-t)+items[Math.min(j+1,items.length-1)].value/scale*t;return[i/64*300,hi===lo?100:180-(value-lo/scale)/(hi/scale-lo/scale)*160];});};
const read=()=>{const index=Number(cursor.value),point=points[index],lo=Number(start.value),hi=Number(end.value),visible=points.slice(lo,hi+1),range=Math.min(...visible.map(p=>p.value))+"–"+Math.max(...visible.map(p=>p.value)),text=point.label+": "+point.value+" "+(root.dataset.unit||"")+(point.event?". Event: "+point.event:"")+". Visible range "+range+".";root.querySelector("[data-brush-readout]")!.textContent=text;cursor.setAttribute("aria-valuetext",text);root.querySelector("[data-brush-crosshair]")?.setAttribute("d","M"+(hi===lo?150:(index-lo)/(hi-lo)*300)+" 15V185");};
previous=layout();const draw=(handle="")=>{if(Number(start.value)>Number(end.value)){if(handle==="start")end.value=start.value;else start.value=end.value;}const from=Number(start.value),to=Number(end.value);start.setAttribute("aria-valuetext",points[from].label);end.setAttribute("aria-valuetext",points[to].label);cursor.min=String(from);cursor.max=String(to);cursor.value=String(Math.max(from,Math.min(to,Number(cursor.value))));root.querySelector("[data-brush-first]")!.textContent=points[from].label;root.querySelector("[data-brush-last]")!.textContent=points[to].label;const window=root.querySelector("[data-brush-window]")!;window.setAttribute("x",String(points.length===1?0:from/(points.length-1)*300));window.setAttribute("width",String(points.length===1?300:(to-from)/(points.length-1)*300));const target=layout(),origin=previous.map(p=>[...p]),began=performance.now();cancelAnimationFrame(raf);const frame=(now:number)=>{const t=matchMedia("(prefers-reduced-motion: reduce)").matches?1:Math.min(1,(now-began)/(handle?1:200));previous=target.map((p,i)=>p.map((v,j)=>origin[i][j]+(v-origin[i][j])*t));line.setAttribute("d",previous.map((p,i)=>(i?"L":"M")+p.join(" ")).join(" "));if(t<1)raf=requestAnimationFrame(frame);};raf=requestAnimationFrame(frame);read();};
start.addEventListener("input",()=>draw("start"));end.addEventListener("input",()=>draw("end"));cursor.addEventListener("input",read);
root.querySelectorAll<HTMLButtonElement>("[data-brush-preset]").forEach(button=>button.addEventListener("click",()=>{let from=Number(start.value),to=Number(end.value),span=to-from;const action=Number(button.dataset.brushPreset);if(action===0){from=Math.max(0,from-Math.max(1,span));to=from+span;}if(action===1){to=Math.min(points.length-1,to+Math.max(1,span));from=to-span;}if(action===2){from=0;to=points.length-1;}if(action===3){to=points.length-1;from=Math.max(0,to-6);}start.value=String(from);end.value=String(to);draw();}));
root.querySelector<SVGSVGElement>("[data-brush-overview]")?.addEventListener("pointerdown",event=>{const bounds=(event.currentTarget as SVGSVGElement).getBoundingClientRect(),at=Math.round(Math.max(0,Math.min(1,(event.clientX-bounds.left)/bounds.width))*(points.length-1)),span=Number(end.value)-Number(start.value),from=Math.max(0,Math.min(points.length-1-span,at-Math.floor(span/2)));start.value=String(from);end.value=String(from+span);draw();});draw("start");
});}
initBrush();if(!document.documentElement.hasAttribute("data-haze-brush-chart-listener")){document.documentElement.setAttribute("data-haze-brush-chart-listener","");document.addEventListener("astro:page-load",initBrush);}
</script>When to use
- A long metric needs a focused view while retaining its complete overview.
- Events must be read alongside the measurements within a selected interval.
When not to use
- A short series fits on one axis without zooming. Use line-chart.
Installation
Add the Haze registry to components.json once, then install Brush chart with the shadcn CLI. Works in any Astro project with Tailwind CSS v4.
pnpm dlx shadcn@latest add @haze/brush-chartnpx shadcn@latest add @haze/brush-chartyarn dlx shadcn@latest add @haze/brush-chartbunx --bun shadcn@latest add @haze/brush-chartFirst time? Registry and theme setup
1. Add to components.json:
{
"registries": {
"@haze": "http://localhost:4329/r/{name}.json"
}
}2. Install the theme once: npx shadcn@latest add @haze/theme, then import it after Tailwind in your global CSS:@import "./haze.css";
API reference
| Prop | Type | Default | Description |
|---|---|---|---|
| label | string | Daily build volume | Visible metric caption. |
| data | { label: string; value: number; event?: string }[] | – | Ordered readings; nonfinite measurements are excluded while finite negative values remain valid. |
| unit | string | builds | Measurement unit. |
| initialWindow | number | 12 | Initial count of most-recent readings; clamped to at least one. |
| class | string | – | Additional classes on the chart figure. |
Keyboard interactions
- Arrow keysHomeEnd
- Resize either window endpoint or read the focused window.
- EnterSpace
- Pan, reset to all data or select the latest seven readings.
Accessibility
- Independent native range controls select both window endpoints and expose their period names, with another range reading inside the window.
- The overview and main line are decorative; the disclosure lists every finite value and its event for access without scripts.
Notes for AI
Copy into Claude Code, Cursor, Codex or any coding agent. Agents can also read the Markdown version of this page.
Install and use
Install the Haze UI component "Brush chart" with `npx shadcn@latest add @haze/brush-chart`. If components.json has no "@haze" registry yet, add "registries": { "@haze": "http://localhost:4329/r/{name}.json" }. If src/styles/haze.css does not exist yet, run `npx shadcn@latest add @haze/theme` once and import it from the global stylesheet after Tailwind. The CLI stops at an overwrite question when a Haze file already exists and differs, even with --yes, and then installs nothing after it: read its output. If the user has not edited their Haze files, re-run with --overwrite to take the registry versions; if they have, ask them before overwriting. Then import BrushChart from "@/components/haze/ui/brush-chart.astro" (or the relative path) and render it where it fits the page. Keep Haze token classes (bg-background, text-muted-foreground, border-border...) and never hardcode colors. Docs: http://localhost:4329/ui/brush-chart.md
Customize with real content
Adapt the Haze brush-chart ui for our product. Replace the demo values of these props with our real content: label, data, unit, initialWindow, class. Keep the layout, the heading level rules and these accessibility details: Independent native range controls select both window endpoints and expose their period names, with another range reading inside the window. The overview and main line are decorative; the disclosure lists every finite value and its event for access without scripts. Do not add raw color classes; use the Haze tokens.
Choose the right block
Before using brush-chart, check it fits: use it when a long metric needs a focused view while retaining its complete overview.; events must be read alongside the measurements within a selected interval.. Avoid it when a short series fits on one axis without zooming. use line-chart.. Alternatives: @haze/line-chart, @haze/streamgraph, @haze/sparkline.
Dependencies
- npm: @lucide/astro
- @haze/utils
- @haze/theme (one-time setup)
Source · Original Haze UI work.