
Topographic vector field isoline contours indicating spatial wind speed gradients.
components/tanstack-charts/area/contour-topography.tsx
import { type ChartPoint, defineChart, dot } from "@tanstack/charts";
import { motion } from "@tanstack/charts/motion";
import { contour } from "@tanstack/charts/spatial/contour";
import { tooltip } from "@tanstack/charts/tooltip";
import { Chart } from "@tanstack/react-charts/core";
import { useMemo } from "react";
import { scaleLinear, scaleThreshold } from "d3-scale";
import { Compass } from "lucide-react";
import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from "@/components/ui/card";
interface GridCell {
u: number;
v: number;
x: number;
y: number;
val: number;
}
const motionRenderer = motion({
initial: "always",
transition: { type: "spring", stiffness: 170, damping: 18, mass: 1 },
});
const contourThresholds = [2, 5, 8, 11, 14];
function generateDemoGrid() {
const width = 20;
const height = 15;
const data: GridCell[] = [];
for (let j = 0; j < height; j++) {
for (let i = 0; i < width; i++) {
const u = Math.sin(i * 0.4) * 8 + 6;
const v = Math.cos(j * 0.5) * 8 + 6;
const val = Math.hypot(u, v);
data.push({ u, v, x: i, y: j, val });
}
}
return { width, height, data };
}
function shadcnTooltipContent<TDatum>(points: readonly ChartPoint<TDatum>[]) {
if (points.length === 0) return { title: "", rows: [] };
const d = points[0].datum as GridCell;
if (!d) return { title: "", rows: [] };
return {
title: `Grid Location (${d.x}, ${d.y})`,
rows: [
{
label: "Wind Velocity",
value: `${d.val.toFixed(1)} m/s`,
color: "var(--chart-1)",
},
{
label: "Vector (U, V)",
value: `${d.u.toFixed(1)}, ${d.v.toFixed(1)} m/s`,
color: "var(--muted-foreground)",
},
],
};
}
export function ContourTopography() {
const grid = useMemo(() => generateDemoGrid(), []);
const definition = useMemo(
() =>
defineChart(
{
renderer: motionRenderer,
marks: [
contour(grid.data, {
id: "contour-isobands",
width: grid.width,
height: grid.height,
value: (row: GridCell) => Math.hypot(row.u, row.v),
thresholds: contourThresholds,
stroke: "var(--background)",
strokeWidth: 0.75,
}),
dot(grid.data, {
id: "contour-grid-dots",
x: "x",
y: "y",
r: 2,
fill: "var(--foreground)",
fillOpacity: 0.25,
states: [
{
when: { focus: "primary" },
style: { r: 5, fill: "var(--chart-1)", fillOpacity: 1 },
},
],
}),
],
scales: {
x: {
scale: scaleLinear().domain([0, grid.width]),
axis: false,
},
y: {
scale: scaleLinear().domain([0, grid.height]),
axis: false,
},
},
color: {
scale: scaleThreshold<number, string>,
domain: contourThresholds.slice(1),
range: [
"var(--chart-5)",
"var(--chart-4)",
"var(--chart-3)",
"var(--chart-2)",
"var(--chart-1)",
],
},
margin: { top: 20, right: 24, bottom: 24, left: 24 },
theme: {
foreground: "var(--muted-foreground)",
grid: "transparent",
background: "transparent",
},
},
{
focus: "nearest",
keyboard: true,
tooltip: {
use: tooltip,
className:
"font-sans shadow-none rounded-md bg-popover text-popover-foreground border p-2 text-xs pointer-events-none transition-all duration-75",
anchor: "pointer",
placement: "auto",
content: (points) => shadcnTooltipContent(points),
},
},
),
[grid],
);
return (
<Card className="w-full max-w-xl">
<CardHeader>
<CardTitle>Topographic Wind Velocity Contours</CardTitle>
<CardDescription>
Filled vector field isoline contours indicating spatial wind speed gradients
</CardDescription>
</CardHeader>
<CardContent>
<div className="h-[250px] w-full">
<Chart
definition={definition}
renderer={motionRenderer}
height={250}
ariaLabel="Topographic Wind Velocity Contours Chart"
/>
</div>
</CardContent>
<CardFooter className="flex-col items-start gap-2 text-sm">
<div className="flex items-center gap-2 leading-none font-medium">
High velocity core (dark blue) centers over central grid origin{" "}
<Compass className="h-4 w-4 text-blue-500" />
</div>
<div className="text-muted-foreground leading-none">
Hover over grid observation points to view localized wind speed vectors
</div>
</CardFooter>
</Card>
);
}
export default ContourTopography;