components/tanstack-charts/line/framed-scatter.tsx
import { type ChartPoint, defineChart, dot, frame } 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 } from "d3-scale";
import { Frame } from "lucide-react";
import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from "@/components/ui/card";
interface ScatterDatum {
id: string;
weight: number;
mpg: number;
name: string;
}
const motionRenderer = motion({
initial: "always",
transition: { type: "spring", stiffness: 170, damping: 18, mass: 1 },
});
function generateDemoData(): ScatterDatum[] {
const data: ScatterDatum[] = [];
const models = [
"Ford Mustang",
"Chevrolet Impala",
"Toyota Corolla",
"Honda Civic",
"Dodge Challenger",
"BMW 3 Series",
"Volkswagen Golf",
"Nissan Sentra",
"Subaru Impreza",
"Mazda 3",
];
for (let i = 0; i < 60; i++) {
const weight = 1800 + Math.round(Math.random() * 3000);
const baseMpg = 48 - (weight - 1800) * 0.009;
const mpg = Math.max(10, Math.round(baseMpg + (Math.random() - 0.5) * 10));
const name = `${models[i % models.length]} #${i + 1}`;
data.push({ id: `car-${i}`, weight, mpg, name });
}
return data;
}
function shadcnTooltipContent<TDatum>(points: readonly ChartPoint<TDatum>[]) {
if (points.length === 0) return { title: "", rows: [] };
const d = points[0].datum as ScatterDatum;
if (!d) return { title: "", rows: [] };
return {
title: d.name,
rows: [
{ label: "Curb Weight", value: `${d.weight} lbs`, color: "var(--chart-1)" },
{ label: "Fuel Economy", value: `${d.mpg} mpg`, color: "var(--foreground)" },
],
};
}
export function FramedScatter() {
const data = useMemo(() => generateDemoData(), []);
const definition = useMemo(
() =>
defineChart(
{
renderer: motionRenderer,
marks: [
frame({
inset: 4,
radius: 8,
fill: "var(--primary)",
fillOpacity: 0.05,
stroke: "var(--primary)",
strokeOpacity: 0.3,
strokeWidth: 1.5,
}),
dot(data, {
key: "id",
x: "weight",
y: "mpg",
fill: "var(--chart-1)",
fillOpacity: 0.75,
r: 4.5,
stroke: "var(--background)",
strokeWidth: 1,
}),
],
scales: {
x: { scale: scaleLinear, grid: false },
y: { scale: scaleLinear, grid: false },
},
guides: false,
margin: 16,
theme: {
foreground: "var(--muted-foreground)",
grid: "transparent",
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>Framed Region Scatterplot</CardTitle>
<CardDescription>Guide-free scatterplot enclosed in a rounded geometric plot frame</CardDescription>
</CardHeader>
<CardContent>
<div className="h-[250px] w-full">
<Chart
definition={definition}
renderer={motionRenderer}
height={250}
ariaLabel="Framed Region Scatterplot Chart"
/>
</div>
</CardContent>
<CardFooter className="flex-col items-start gap-2 text-sm">
<div className="flex items-center gap-2 leading-none font-medium">
60 vehicle data points mapped inside bounded SVG frame <Frame className="text-primary h-4 w-4" />
</div>
<div className="text-muted-foreground leading-none">
Shows strong inverse correlation between vehicle weight and fuel efficiency
</div>
</CardFooter>
</Card>
);
}
export default FramedScatter;