
A 12-week daily rainfall intensity matrix using cell marks in TanStack Charts.
components/tanstack-charts/heatmap/calendar-heatmap.tsx
import { type ChartPoint, cell, defineChart } 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 { scaleBand, scaleThreshold } from "d3-scale";
import { Calendar } from "lucide-react";
import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from "@/components/ui/card";
interface CalendarCellDatum {
week: number;
day: string;
precipitation: number;
}
const motionRenderer = motion({
initial: "always",
transition: { type: "spring", stiffness: 170, damping: 18, mass: 1 },
});
const weekdays = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
function generateDemoData(): CalendarCellDatum[] {
const results: CalendarCellDatum[] = [];
for (let w = 0; w < 12; w++) {
for (let d = 0; d < 7; d++) {
const val = Math.max(0, Math.sin(w * 0.5 + d * 0.8) * 12 + (w % 3) * 2 - 2);
results.push({ week: w, day: weekdays[d], precipitation: val });
}
}
return results;
}
function shadcnTooltipContent<TDatum>(points: readonly ChartPoint<TDatum>[]) {
if (points.length === 0) return { title: "", rows: [] };
const d = points[0].datum as CalendarCellDatum;
if (!d) return { title: "", rows: [] };
return {
title: `Week ${d.week + 1} (${d.day})`,
rows: [
{
label: "Precipitation",
value: `${d.precipitation.toFixed(1)} mm`,
color: "var(--chart-1)",
},
],
};
}
export function CalendarHeatmap() {
const data = useMemo(() => generateDemoData(), []);
const definition = useMemo(
() =>
defineChart(
{
renderer: motionRenderer,
marks: [
cell(data, {
id: "calendar-cells",
x: "week",
y: "day",
color: "precipitation",
inset: 1.5,
radius: 3,
}),
],
scales: {
x: {
scale: () => scaleBand<number>().paddingInner(0.06).paddingOuter(0.03),
axis: {
line: false,
label: "Week",
ticks: {
size: 0,
format: (v: number) => `W${v + 1}`,
},
},
},
y: {
scale: () => scaleBand<string>().domain(weekdays).paddingInner(0.06).paddingOuter(0.03),
axis: {
line: false,
ticks: { size: 0 },
},
},
},
color: {
scale: scaleThreshold<number, string>,
domain: [3, 7, 12],
range: ["var(--chart-5)", "var(--chart-4)", "var(--chart-2)", "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),
},
},
),
[data],
);
return (
<Card className="w-full max-w-xl">
<CardHeader>
<CardTitle>Daily Rainfall Calendar Matrix</CardTitle>
<CardDescription>12-week calendar grid displaying daily rainfall intensity</CardDescription>
</CardHeader>
<CardContent>
<div className="h-[250px] w-full">
<Chart
definition={definition}
renderer={motionRenderer}
height={250}
ariaLabel="Daily Rainfall Calendar Matrix Chart"
/>
</div>
</CardContent>
<CardFooter className="flex-col items-start gap-2 text-sm">
<div className="flex items-center gap-2 leading-none font-medium">
Mid-week days show peak precipitation concentration{" "}
<Calendar className="h-4 w-4 text-emerald-500" />
</div>
<div className="text-muted-foreground leading-none">
Cells map day-of-week rows against sequential weekly columns
</div>
</CardFooter>
</Card>
);
}
export default CalendarHeatmap;