# Waffle chart

> Allocate exactly 100 cells to category shares using largest-remainder rounding, with a keyboard cell reader and exact-value table.

- Kind: ui · Category: Data display · Interactivity: script
- Install: `npx shadcn@latest add @haze/waffle-chart`
- File: `src/components/haze/ui/waffle-chart.astro`
- npm dependencies: @lucide/astro
- Registry dependencies: @haze/utils
- Docs page: https://hazeui.com/ui/waffle-chart

## Use when
- Countable percentage points communicate a small set of shares.
- A composition includes small categories that need explicit cell counts.

## Avoid when
- A continuous radial summary is more compact. Use donut-chart.

## Alternatives
- [donut-chart](https://hazeui.com/ui/donut-chart.md)
- [split-bar](https://hazeui.com/ui/split-bar.md)
- [bar-chart](https://hazeui.com/ui/bar-chart.md)

## Props

| Name | Type | Default | Required | Description |
|---|---|---|---|---|
| label | `string` | How the team travels | no | Name of the composition and its table. |
| segments | `{ label: string; value: number }[]` |  | no | Finite nonnegative quantities. Zero totals produce an empty message. |
| class | `string` |  | no | Additional classes on the chart figure. |

## Accessibility
- Grid cells are decorative; the native cell range announces each category, exact share and assigned cell count.
- Selecting a legend item dims other cells while keeping every category and its value in the table.
- Key Arrow keys / Home / End: Move the native cursor through the 100 percentage cells.
- Key Enter / Space: Select or clear a named category highlight.

## Agent prompts

### Install and use

Install the Haze UI component "Waffle chart" with `npx shadcn@latest add @haze/waffle-chart`. If components.json has no "@haze" registry yet, add "registries": { "@haze": "https://hazeui.com/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 WaffleChart from "@/components/haze/ui/waffle-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: https://hazeui.com/ui/waffle-chart.md

### Customize with real content

Adapt the Haze waffle-chart ui for our product. Replace the demo values of these props with our real content: label, segments, class. Keep the layout, the heading level rules and these accessibility details: Grid cells are decorative; the native cell range announces each category, exact share and assigned cell count. Selecting a legend item dims other cells while keeping every category and its value in the table. Do not add raw color classes; use the Haze tokens.

### Choose the right block

Before using waffle-chart, check it fits: use it when countable percentage points communicate a small set of shares.; a composition includes small categories that need explicit cell counts.. Avoid it when a continuous radial summary is more compact. use donut-chart.. Alternatives: @haze/donut-chart, @haze/split-bar, @haze/bar-chart.

## Example requests that should lead here
- Build a 100-cell commuting survey chart with largest-remainder percentage rounding and keyboard exploration.

## Source

```astro
---
import { ChevronDown } from "@lucide/astro";
import { cn } from "../../../lib/haze/utils";
interface Segment{label:string;value:number}
interface Props{label?:string;segments?:Segment[];class?:string}
const{label="How the team travels",segments=[{label:"Walk or cycle",value:42},{label:"Public transit",value:35},{label:"Car",value:18},{label:"Other",value:5}],class:className}=Astro.props;
const items=segments.filter(item=>Number.isFinite(item.value)&&item.value>=0),max=Math.max(0,...items.map(item=>item.value)),sum=max?items.reduce((s,item)=>s+item.value/max,0):0;
const exact=items.map(item=>sum?item.value/max/sum*100:0),counts=exact.map(Math.floor),remainder=exact.map((value,index)=>({index,fraction:value-counts[index]})).sort((a,b)=>b.fraction-a.fraction||a.index-b.index);
const missing=100-counts.reduce((s,v)=>s+v,0);if(sum)for(let i=0;i<missing;i++)counts[remainder[i].index]++;
const cells=counts.flatMap((count,index)=>Array.from({length:count},()=>index)),colors=["bg-brand","bg-success","bg-warning","bg-muted-foreground","bg-destructive"];
const percent=(n:number)=>new Intl.NumberFormat("en",{maximumFractionDigits:1}).format(n)+"%";
---
<figure data-haze-waffle-chart data-items={JSON.stringify(items)} class={cn("@container grid min-w-0 gap-4 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>
 {sum?<div class="grid min-w-0 items-center gap-4 @sm:grid-cols-[minmax(0,1fr)_minmax(0,1fr)]"><div aria-hidden="true" class="mx-auto grid aspect-square w-full max-w-64 grid-cols-10 gap-1">{cells.map((group,index)=><span data-waffle-cell={index} data-group={group} class={cn("aspect-square min-w-0 rounded-xs transition-[opacity,scale] duration-200 ease-snappy data-[dimmed]:scale-75 data-[dimmed]:opacity-25",colors[group%colors.length])}></span>)}</div><div class="grid min-w-0 gap-2">{items.map((item,index)=><button data-waffle-option={index} type="button" disabled aria-pressed="false" class="flex min-h-10 min-w-0 items-center gap-2 rounded-lg p-2 text-left text-xs/5 transition-colors duration-150 ease-snappy enabled:hover:bg-accent aria-pressed:bg-accent focus-visible:outline-2 focus-visible:outline-offset-1 focus-visible:outline-ring"><span aria-hidden="true" class={cn("size-2 shrink-0 rounded-xs",colors[index%colors.length])}></span><span class="min-w-0 flex-1 wrap-anywhere">{item.label}</span><span class="shrink-0 tabular-nums">{percent(exact[index])}</span></button>)}</div></div>:<p class="text-[0.8125rem]/5 text-muted-foreground">No positive shares to display.</p>}
 {sum>0&&<label hidden data-waffle-control class="grid gap-1 text-xs/5 text-muted-foreground">Explore the 100 cells<input data-waffle-cursor type="range" min="0" max="99" step="1" value="0" aria-label="Cell in the percentage grid" 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>}
 <p data-waffle-readout role="status" aria-live="polite" class="min-h-10 text-xs/5 text-muted-foreground">Each square represents one percentage point; rounding assigns exactly 100 cells.</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">Values and assigned cells<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">Category</th><th scope="col" class="p-1 text-right">Value / share / cells</th></tr></thead><tbody>{items.map((item,index)=><tr class="border-t border-border"><th scope="row" class="p-1 font-normal wrap-anywhere">{item.label}</th><td class="p-1 text-right tabular-nums wrap-anywhere">{item.value} / {percent(exact[index])} / {counts[index]}</td></tr>)}</tbody></table></details>
</figure>
<script>
function initWaffle(){document.querySelectorAll<HTMLElement>("[data-haze-waffle-chart]:not([data-enhanced])").forEach(root=>{
 root.dataset.enhanced="";const items:{label:string;value:number}[]=JSON.parse(root.dataset.items||"[]"),cells=Array.from(root.querySelectorAll<HTMLElement>("[data-waffle-cell]")),buttons=Array.from(root.querySelectorAll<HTMLButtonElement>("[data-waffle-option]")),max=Math.max(0,...items.map(item=>item.value)),total=max?items.reduce((s,item)=>s+item.value/max,0):0;
 const select=(index:number,cell?:number)=>{buttons.forEach((button,i)=>button.setAttribute("aria-pressed",String(i===index)));cells.forEach(node=>node.toggleAttribute("data-dimmed",index>=0&&Number(node.dataset.group)!==index));const item=items[index];root.querySelector("[data-waffle-readout]")!.textContent=item?(cell===undefined?"":"Cell "+(cell+1)+" of 100 · ")+item.label+" · "+item.value+" · "+(item.value/max/total*100).toFixed(1)+"% · "+cells.filter(node=>Number(node.dataset.group)===index).length+" cells":"All categories shown. Each cell is one percentage point.";};
 buttons.forEach((button,index)=>{button.disabled=false;button.addEventListener("click",()=>select(button.getAttribute("aria-pressed")==="true"?-1:index));});const cursor=root.querySelector<HTMLInputElement>("[data-waffle-cursor]");root.querySelector("[data-waffle-control]")?.removeAttribute("hidden");cursor?.addEventListener("input",()=>{const index=Number(cursor.value);select(Number(cells[index]?.dataset.group),index);cursor.setAttribute("aria-valuetext",root.querySelector("[data-waffle-readout]")!.textContent||"");});cells.forEach((cell,index)=>cell.addEventListener("pointerenter",()=>{if(cursor)cursor.value=String(index);select(Number(cell.dataset.group),index);}));
});}
initWaffle();if(!document.documentElement.hasAttribute("data-haze-waffle-chart-listener")){document.documentElement.setAttribute("data-haze-waffle-chart-listener","");document.addEventListener("astro:page-load",initWaffle);}
</script>
```
