components/tanstack-charts/line/log-scale-scatter.tsx
import { type ChartPoint, defineChart, dot } 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, scaleLog } from "d3-scale";
import { Scaling } from "lucide-react";
import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from "@/components/ui/card";
interface LogDatum {
name: string;
size: number;
depth: number;
}
const motionRenderer = motion({
initial: "always",
transition: { type: "spring", stiffness: 170, damping: 18, mass: 1 },
});
function generateDemoData(): LogDatum[] {
const results: LogDatum[] = [];
const packages = ["analytics", "animate", "data", "display", "flex", "physics", "query", "util"];
for (let i = 0; i < 90; i++) {
const pkg = packages[i % packages.length];
const depth = 1 + Math.floor(Math.random() * 5);
const size = Math.floor(200 * Math.pow(10, Math.random() * 2.5));
results.push({
name: `flare.${pkg}.Class${i + 1}`,
size,
depth,
});
}
return results;
}
function shadcnTooltipContent<TDatum>(points: readonly ChartPoint<TDatum>[]) {
if (points.length === 0) return { title: "", rows: [] };
const d = points[0].datum as LogDatum;
if (!d) return { title: "", rows: [] };
return {
title: d.name,
rows: [
{ label: "Class Size (Bytes)", value: `${d.size.toLocaleString()}`, color: "var(--chart-1)" },
{ label: "Hierarchy Depth", value: `${d.depth}`, color: "var(--foreground)" },
],
};
}
export function LogScaleScatter() {
const data = useMemo(() => generateDemoData(), []);
const definition = useMemo(
() =>
defineChart(
{
renderer: motionRenderer,
marks: [
dot(data, {
id: "class-size-points",
x: "size",
y: "depth",
key: "name",
r: 4,
fill: "var(--chart-1)",
stroke: "var(--background)",
strokeWidth: 1,
}),
],
margin: { top: 24, right: 24, bottom: 32, left: 45 },
scales: {
x: {
scale: scaleLog().domain([100, 100_000]),
grid: true,
axis: {
label: "Class Size (Log Bytes)",
ticks: {
format: (v: number) => (v >= 1000 ? `${v / 1000}k` : `${v}`),
},
},
},
y: {
scale: scaleLinear().domain([0, 6]),
grid: true,
axis: { label: "Package Hierarchy Depth" },
},
},
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>Logarithmic Axis Class Size Scatterplot</CardTitle>
<CardDescription>
Log-scale X axis scatter showing multi-order-of-magnitude codebase distributions
</CardDescription>
</CardHeader>
<CardContent>
<div className="h-[250px] w-full">
<Chart
definition={definition}
renderer={motionRenderer}
height={250}
ariaLabel="Logarithmic Axis Class Size 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">
Log scale expands wide 100 to 100,000 Byte dynamic range{" "}
<Scaling className="h-4 w-4 text-emerald-500" />
</div>
<div className="text-muted-foreground leading-none">
Logarithmic spacing prevents compression of smaller values on linear scales
</div>
</CardFooter>
</Card>
);
}
export default LogScaleScatter;