components/tanstack-charts/area/temperature-range-band.tsx
import { type ChartPoint, areaY, defineChart, lineY } 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, scaleUtc } from "d3-scale";
import { Thermometer } from "lucide-react";
import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from "@/components/ui/card";
interface TempDatum {
date: Date;
low: number;
high: number;
}
const motionRenderer = motion({
initial: "always",
transition: { type: "spring", stiffness: 170, damping: 18, mass: 1 },
});
function generateDemoData(): TempDatum[] {
const baseDate = new Date(2025, 5, 1);
return Array.from({ length: 14 }, (_, i) => {
const d = new Date(baseDate);
d.setDate(baseDate.getDate() + i);
const low = 52 + Math.sin(i * 0.4) * 6 + (i % 3);
const high = low + 14 + Math.cos(i * 0.3) * 4;
return { date: d, low, high };
});
}
function shadcnTooltipContent<TDatum>(points: readonly ChartPoint<TDatum>[]) {
if (points.length === 0) return { title: "", rows: [] };
const d = points[0].datum as TempDatum;
if (!d) return { title: "", rows: [] };
const dateStr = d.date.toLocaleDateString("en-US", { month: "short", day: "numeric" });
return {
title: dateStr,
rows: [
{ label: "High", value: `${d.high.toFixed(1)}°F`, color: "var(--chart-1)" },
{ label: "Low", value: `${d.low.toFixed(1)}°F`, color: "var(--chart-2)" },
],
};
}
export function TemperatureRangeBand() {
const data = useMemo(() => generateDemoData(), []);
const definition = useMemo(
() =>
defineChart(
{
renderer: motionRenderer,
marks: [
areaY(data, {
id: "temp-band",
x: "date",
y1: "low",
y2: "high",
fill: "var(--chart-1)",
fillOpacity: 0.2,
}),
lineY(data, {
id: "temp-low",
x: "date",
y: "low",
stroke: "var(--chart-2)",
strokeWidth: 2,
}),
lineY(data, {
id: "temp-high",
x: "date",
y: "high",
stroke: "var(--chart-1)",
strokeWidth: 2,
}),
],
scales: {
x: {
scale: scaleUtc,
axis: {
line: false,
label: "Date",
ticks: {
size: 0,
padding: 10,
format: (d: Date) =>
d.toLocaleDateString("en-US", { month: "short", day: "numeric" }),
},
},
},
y: {
scale: scaleLinear().domain([45, 80]),
grid: true,
axis: {
line: false,
label: "Temperature (°F)",
ticks: { size: 0 },
},
},
},
margin: { top: 24, right: 24, bottom: 32, left: 40 },
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 Temperature Range</CardTitle>
<CardDescription>Daily high and low ambient temperature band tracking</CardDescription>
</CardHeader>
<CardContent>
<div className="h-[250px] w-full">
<Chart
definition={definition}
renderer={motionRenderer}
height={250}
ariaLabel="Daily Temperature Range Chart"
/>
</div>
</CardContent>
<CardFooter className="flex-col items-start gap-2 text-sm">
<div className="flex items-center gap-2 leading-none font-medium">
Average variance remains within 15°F range <Thermometer className="h-4 w-4 text-sky-500" />
</div>
<div className="text-muted-foreground leading-none">
Shaded area represents temperature dispersion between min and max bounds
</div>
</CardFooter>
</Card>
);
}
export default TemperatureRangeBand;