components/tanstack-charts/line/line-gaps.tsx
import { type ChartPoint, defineChart, 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, scaleUtc } from "d3-scale";
import { TrendingUp } from "lucide-react";
import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from "@/components/ui/card";
interface StockDatum {
date: Date;
close: number | null;
}
const motionRenderer = motion({
initial: "always",
transition: { type: "spring", stiffness: 170, damping: 18, mass: 1 },
});
function generateDemoData(): StockDatum[] {
const baseDate = new Date(2025, 0, 1);
return Array.from({ length: 24 }, (_, i) => {
const d = new Date(baseDate);
d.setDate(baseDate.getDate() + i * 7);
// Introduce gaps in Q1 (weeks 4 to 8)
const isGap = i >= 4 && i <= 7;
return {
date: d,
close: isGap ? null : 150 + Math.sin(i * 0.4) * 25 + i * 2,
};
});
}
function shadcnTooltipContent<TDatum>(points: readonly ChartPoint<TDatum>[]) {
const p = points[0];
if (!p || !p.datum) return { title: "", rows: [] };
const d = p.datum as unknown as StockDatum;
if (!d || !d.date) return { title: "", rows: [] };
const dateObj = d.date instanceof Date ? d.date : new Date(d.date);
const dateStr = isNaN(dateObj.getTime())
? ""
: dateObj.toLocaleDateString("en-US", { month: "short", day: "numeric" });
return {
title: dateStr,
rows: [
{
label: "Close",
value: d.close !== null ? `$${d.close.toFixed(2)}` : "N/A (Gap)",
color: p.color || "var(--chart-1)",
},
],
};
}
export function LineGaps() {
const data = useMemo(() => generateDemoData(), []);
const definition = useMemo(
() =>
defineChart(
{
renderer: motionRenderer,
marks: [
lineY(data, {
id: "stock-line",
x: "date",
y: (row: StockDatum) => row.close,
stroke: "var(--chart-1)",
strokeWidth: 2.25,
}),
],
scales: {
x: {
scale: scaleUtc,
axis: {
line: false,
label: "Week",
ticks: {
size: 0,
padding: 10,
format: (d: Date) => d.toLocaleDateString("en-US", { month: "short", day: "numeric" }),
},
},
},
y: {
scale: scaleLinear().domain([100, 220]),
grid: true,
axis: {
line: false,
label: "Close ($)",
ticks: { size: 0 },
},
},
},
margin: { top: 24, right: 24, bottom: 32, left: 40 },
theme: {
foreground: "var(--muted-foreground)",
grid: "var(--border)",
background: "transparent",
},
},
{
focus: "group-x",
tooltip: {
use: tooltip,
className:
"font-sans shadow-none rounded-md bg-popover text-popover-foreground border p-2 text-xs",
anchor: "group-center",
placement: "auto",
content: (points) => shadcnTooltipContent(points),
},
},
),
[data],
);
return (
<Card className="w-full max-w-xl">
<CardHeader>
<CardTitle>Asset Price with Data Gaps</CardTitle>
<CardDescription>Weekly closing prices highlighting missing reporting intervals</CardDescription>
</CardHeader>
<CardContent>
<div className="h-[250px] w-full">
<Chart
definition={definition}
renderer={motionRenderer}
height={250}
ariaLabel="Asset Price with Data Gaps Chart"
/>
</div>
</CardContent>
<CardFooter className="flex-col items-start gap-2 text-sm">
<div className="flex items-center gap-2 leading-none font-medium">
Market recovery following Q1 gap interval <TrendingUp className="h-4 w-4 text-emerald-500" />
</div>
<div className="text-muted-foreground leading-none">
Gaps indicate suspended trading sessions during system upgrade
</div>
</CardFooter>
</Card>
);
}
export default LineGaps;