components/tanstack-charts/line/line-chart-dots.tsx
import { type ChartPoint, d3Curve, defineChart, dot, 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, scalePoint } from "d3-scale";
import { curveNatural } from "d3-shape";
import { TrendingUp } from "lucide-react";
import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from "@/components/ui/card";
interface MonthDatum {
month: string;
desktop: number;
}
const motionRenderer = motion({
initial: "always",
transition: { type: "spring", stiffness: 170, damping: 18, mass: 1 },
});
function generateDemoData(): MonthDatum[] {
return [
{ month: "January", desktop: 186 },
{ month: "February", desktop: 305 },
{ month: "March", desktop: 237 },
{ month: "April", desktop: 73 },
{ month: "May", desktop: 209 },
{ month: "June", desktop: 214 },
];
}
function titleCase(value: string) {
return value.charAt(0).toUpperCase() + value.slice(1);
}
function shadcnTooltipContent<TDatum>(points: readonly ChartPoint<TDatum>[]) {
return {
title: String(points[0]?.xValue ?? ""),
rows: points.map((point) => ({
label: titleCase(
String(point.group ?? point.markId.replace(/-?(bars|lines|areas|slices|values|radar)$/u, "")),
),
value: Number(point.yValue ?? point.xValue ?? 0).toLocaleString("en-US"),
color: point.color,
})),
};
}
export function LineChartDots() {
const data = useMemo(() => generateDemoData(), []);
const definition = useMemo(
() =>
defineChart(
{
renderer: motionRenderer,
motion: {
transition: { type: "spring", stiffness: 170, damping: 18, mass: 1 },
},
marks: [
lineY(data, {
id: "visitor-line",
x: "month",
y: "desktop",
curve: d3Curve(curveNatural),
stroke: "var(--chart-1)",
strokeWidth: 2,
}),
dot(data, {
id: "visitor-dots",
x: "month",
y: "desktop",
r: 4,
fill: "var(--chart-1)",
stroke: "var(--chart-1)",
strokeWidth: 1,
}),
],
scales: {
x: {
scale: scalePoint,
axis: {
line: false,
ticks: {
size: 0,
padding: 10,
format: (value: string) => value.slice(0, 3),
},
},
},
y: {
scale: scaleLinear().domain([0, 320]),
grid: true,
axis: {
line: false,
ticks: {
values: [0, 80, 160, 240, 320],
size: 0,
},
tickLabels: false,
},
},
},
color: {
domain: ["desktop"],
range: ["var(--chart-1)"],
},
margin: {
top: 8,
right: 12,
bottom: 35,
left: 12,
},
theme: {
foreground: "var(--muted-foreground)",
grid: "var(--border)",
background: "transparent",
},
},
{
focus: "group-x",
tooltip: {
use: tooltip,
motion: { type: "spring", stiffness: 170, damping: 18 },
className:
"font-sans shadow-none rounded-md bg-popover text-popover-foreground border p-2 text-xs",
anchor: "group-center",
placement: "auto",
sort: "color-domain",
content: (points) => shadcnTooltipContent(points),
},
},
),
[data],
);
return (
<Card className="w-full max-w-xl">
<CardHeader>
<CardTitle>System Memory Utilization</CardTitle>
<CardDescription>RAM usage percentage across production server nodes</CardDescription>
</CardHeader>
<CardContent>
<div className="h-[250px] w-full">
<Chart
definition={definition}
renderer={motionRenderer}
height={250}
ariaLabel="System Memory Utilization Chart"
/>
</div>
</CardContent>
<CardFooter className="flex-col items-start gap-2 text-sm">
<div className="flex items-center gap-2 leading-none font-medium">
Memory efficiency optimized by 5.2% this month <TrendingUp className="h-4 w-4 text-emerald-500" />
</div>
<div className="text-muted-foreground leading-none">Showing total visitors for the last 6 months</div>
</CardFooter>
</Card>
);
}
export default LineChartDots;