# Quantitative Binned Heatmap

> Two-dimensional quantitative binned density heatmap grid.

A shadcn/ui block from the [@pacekit](https://ui.pacekit.dev/?utm_source=shadcn.exchange) registry, listed on [shadcn.exchange](https://shadcn.exchange/r/pacekit/tanstack-charts-quantitative-binned-heatmap).

- Page, with a live preview: https://shadcn.exchange/r/pacekit/tanstack-charts-quantitative-binned-heatmap
- Screenshot: https://abzjhwgwpdpz7ua5.public.blob.vercel-storage.com/shots/pacekit/tanstack-charts-quantitative-binned-heatmap-light-61eb41a3c3077639.png
- Registry item: https://ui.pacekit.dev/r/tanstack-charts-quantitative-binned-heatmap.json
- Listed: 2026-10-21, source last changed 2026-10-21

## Install

```bash
npx shadcn@latest add https://shadcn.exchange/r/pacekit/tanstack-charts-quantitative-binned-heatmap.json
```

Run it in a project that has shadcn/ui set up. It adds the files below and installs what they depend on.

## Dependencies

- npm packages: `@tanstack/charts`, `@tanstack/react-charts`, `d3-scale`
- Registry items: `card`

## Files

### `components/tanstack-charts/heatmap/quantitative-binned-heatmap.tsx`

```tsx
import { type ChartPoint, binXY, defineChart, rect } from "@tanstack/charts";
import { motion } from "@tanstack/charts/motion";
import { tooltip } from "@tanstack/charts/tooltip";
import { Chart } from "@tanstack/react-charts/core";
import { useMemo } from "react";

import { scaleLinear, scaleSequential } from "d3-scale";
import { Grid } from "lucide-react";

import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from "@/components/ui/card";

interface BinnedDatum {
    culmen_length_mm: number;
    culmen_depth_mm: number;
}

const motionRenderer = motion({
    initial: "always",
    transition: { type: "spring", stiffness: 170, damping: 18, mass: 1 },
});

function generateDemoData(): BinnedDatum[] {
    const results: BinnedDatum[] = [];
    for (let i = 0; i < 120; i++) {
        const length = 32 + (Math.sin(i * 0.4) + 1) * 12 + (i % 5);
        const depth = 13 + (Math.cos(i * 0.5) + 1) * 4 + (i % 3);
        results.push({ culmen_length_mm: length, culmen_depth_mm: depth });
    }
    return results;
}

function shadcnTooltipContent<TDatum>(points: readonly ChartPoint<TDatum>[]) {
    if (points.length === 0) return { title: "", rows: [] };
    const d = points[0].datum as any;
    if (!d) return { title: "", rows: [] };
    return {
        title: `Length ${d.x1}–${d.x2}mm · Depth ${d.y1}–${d.y2}mm`,
        rows: [{ label: "Sample Density", value: `${d.count} observations`, color: "var(--chart-1)" }],
    };
}

export function QuantitativeBinnedHeatmap() {
    const data = useMemo(() => generateDemoData(), []);

    const cells = useMemo(() => {
        const xBoundaries = [30, 34, 38, 42, 46, 50, 54, 58, 62];
        const yBoundaries = [12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22];
        return binXY(data, {
            x: "culmen_length_mm",
            y: "culmen_depth_mm",
            xThresholds: xBoundaries,
            yThresholds: yBoundaries,
            outputs: { count: { reduce: "count" } },
        }).filter((cell) => cell.count > 0);
    }, [data]);

    const definition = useMemo(
        () =>
            defineChart(
                {
                    renderer: motionRenderer,
                    marks: [
                        rect(cells, {
                            id: "heatmap-cells",
                            x1: "x1",
                            x2: "x2",
                            y1: "y1",
                            y2: "y2",
                            color: "count",
                            inset: 0.75,
                        }),
                    ],
                    scales: {
                        x: {
                            scale: scaleLinear().domain([30, 62]),
                            grid: true,
                            axis: {
                                line: false,
                                label: "Bill Length (mm)",
                                ticks: { size: 0 },
                            },
                        },
                        y: {
                            scale: scaleLinear().domain([12, 22]),
                            grid: true,
                            axis: {
                                line: false,
                                label: "Bill Depth (mm)",
                                ticks: { size: 0 },
                            },
                        },
                    },
                    color: {
                        scale: scaleSequential,
                        range: ["var(--chart-5)", "var(--chart-1)"],
                    },
                    margin: { top: 24, right: 24, bottom: 32, left: 45 },
                    theme: {
                        foreground: "var(--muted-foreground)",
                        grid: "var(--border)",
                        background: "transparent",
                    },
                },
                {
                    focus: "group-x",
                    tooltip: {
                        use: tooltip,
                        className:
                            "font-sans shadow-none rounded-md bg-popover text-popover-foreground border p-2 text-xs",
                        anchor: "group-center",
                        placement: "auto",
                        content: (points) => shadcnTooltipContent(points),
                    },
                },
            ),
        [cells],
    );

    return (
        <Card className="w-full max-w-xl">
            <CardHeader>
                <CardTitle>2D Binned Density Heatmap</CardTitle>
                <CardDescription>Two-dimensional histogram binning quantitative bill length vs depth</CardDescription>
            </CardHeader>
            <CardContent>
                <div className="h-[250px] w-full">
                    <Chart
                        definition={definition}
                        renderer={motionRenderer}
                        height={250}
                        ariaLabel="2D Binned Density Heatmap Chart"
                    />
                </div>
            </CardContent>
            <CardFooter className="flex-col items-start gap-2 text-sm">
                <div className="flex items-center gap-2 leading-none font-medium">
                    Highest point density concentrates around 42mm x 16mm <Grid className="h-4 w-4 text-blue-500" />
                </div>
                <div className="text-muted-foreground leading-none">
                    Color intensity scales sequentially based on observation count in each bin
                </div>
            </CardFooter>
        </Card>
    );
}

export default QuantitativeBinnedHeatmap;
```
