
2D polar coordinate distribution plotting wind speed and angle heading.
components/tanstack-charts/line/polar-scatter.tsx
import { type ChartPoint, defineChart } from "@tanstack/charts";
import { motion } from "@tanstack/charts/motion";
import { angleGrid, polar, radialDot, radialGrid } from "@tanstack/charts/polar";
import { tooltip } from "@tanstack/charts/tooltip";
import { Chart } from "@tanstack/react-charts/core";
import { useMemo } from "react";
import { scaleLinear } from "d3-scale";
import { CircleDot } from "lucide-react";
import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from "@/components/ui/card";
interface PolarWindDatum {
id: number;
direction: number;
speed: number;
station: string;
}
const motionRenderer = motion({
initial: "always",
transition: { type: "spring", stiffness: 170, damping: 18, mass: 1 },
});
const angleDomain = [0, 360];
const radiusDomain = [0, 20];
const angleGridValues = [0, 45, 90, 135, 180, 225, 270, 315];
const radiusGridValues = [5, 10, 15, 20];
function generateDemoData(): PolarWindDatum[] {
const results: PolarWindDatum[] = [];
for (let i = 0; i < 40; i++) {
const direction = Math.round(Math.random() * 360);
const speed = Number((3 + Math.random() * 15).toFixed(1));
results.push({
id: i,
direction,
speed,
station: `Station-${Math.floor(i / 10) + 1}`,
});
}
return results;
}
function shadcnTooltipContent<TDatum>(points: readonly ChartPoint<TDatum>[]) {
if (points.length === 0) return { title: "", rows: [] };
const d = points[0].datum as PolarWindDatum;
if (!d) return { title: "", rows: [] };
return {
title: `${d.station} (Bearing: ${d.direction}°)`,
rows: [{ label: "Wind Velocity", value: `${d.speed} m/s`, color: "var(--chart-1)" }],
};
}
export function PolarScatterChart() {
const data = useMemo(() => generateDemoData(), []);
const definition = useMemo(
() =>
defineChart(
{
renderer: motionRenderer,
scales: {
x: null,
y: null,
},
marks: [
polar({
radiusRatio: 0.76,
scales: {
angle: { scale: scaleLinear().domain(angleDomain) },
radius: { scale: scaleLinear().domain(radiusDomain) },
},
guides: [
radialGrid({
values: radiusGridValues,
labels: false,
stroke: "var(--border)",
strokeOpacity: 0.5,
}),
angleGrid({
values: angleGridValues,
labels: false,
stroke: "var(--border)",
strokeOpacity: 0.5,
}),
],
marks: [
radialDot(data, {
id: "polar-scatter-dots",
angle: "direction",
radius: "speed",
r: 4.5,
fill: "var(--chart-1)",
stroke: "var(--background)",
strokeWidth: 1.5,
}),
],
}),
],
margin: { top: 20, right: 20, bottom: 20, left: 20 },
theme: {
foreground: "var(--muted-foreground)",
grid: "var(--border)",
background: "transparent",
},
},
{
keyboard: true,
tooltip: {
use: tooltip,
className:
"font-sans shadow-none rounded-md bg-popover text-popover-foreground border p-2 text-xs",
anchor: "pointer",
placement: "auto",
content: (points) => shadcnTooltipContent(points),
},
},
),
[data],
);
return (
<Card className="w-full max-w-xl">
<CardHeader>
<CardTitle>Surface Wind Directional Polar Scatter</CardTitle>
<CardDescription>
2D polar coordinate distribution plotting wind bearings and speed observations
</CardDescription>
</CardHeader>
<CardContent>
<div className="h-[270px] w-full">
<Chart
definition={definition}
renderer={motionRenderer}
height={270}
ariaLabel="Surface Wind Directional Polar Scatter Chart"
/>
</div>
</CardContent>
<CardFooter className="flex-col items-start gap-2 text-sm">
<div className="flex items-center gap-2 leading-none font-medium">
Multi-station wind velocity readings plotted by bearing{" "}
<CircleDot className="h-4 w-4 text-emerald-500" />
</div>
<div className="text-muted-foreground leading-none">
`radialDot` projects discrete observation points onto polar coordinate space
</div>
</CardFooter>
</Card>
);
}
export default PolarScatterChart;