components/tanstack-charts/bar/interval-timeline.tsx
import { type ChartPoint, barX, defineChart } 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 { scaleBand, scaleLinear } from "d3-scale";
import { Activity } from "lucide-react";
import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from "@/components/ui/card";
interface RangeDatum {
task: string;
start: number;
end: number;
status: string;
}
const motionRenderer = motion({
initial: "always",
transition: { type: "spring", stiffness: 170, damping: 18, mass: 1 },
});
function generateDemoData(): RangeDatum[] {
return [
{ task: "Design System", start: 10, end: 35, status: "Complete" },
{ task: "API Gateway", start: 25, end: 60, status: "Complete" },
{ task: "Auth Service", start: 40, end: 80, status: "In Progress" },
{ task: "Dashboard UI", start: 55, end: 95, status: "In Progress" },
{ task: "Deploy Pipeline", start: 75, end: 120, status: "Pending" },
];
}
function shadcnTooltipContent<TDatum>(points: readonly ChartPoint<TDatum>[]) {
if (points.length === 0) return { title: "", rows: [] };
const d = points[0].datum as RangeDatum;
if (!d) return { title: "", rows: [] };
return {
title: d.task,
rows: [
{ label: "Status", value: d.status, color: points[0].color || "var(--chart-1)" },
{ label: "Duration", value: `${d.start} - ${d.end} days`, color: "var(--foreground)" },
],
};
}
export function IntervalTimeline() {
const data = useMemo(() => generateDemoData(), []);
const definition = useMemo(
() =>
defineChart(
{
renderer: motionRenderer,
marks: [
barX(data, {
id: "interval-bars",
x1: "start",
x2: "end",
y: "task",
color: "status",
inset: 2,
radius: 4,
}),
],
scales: {
x: {
scale: scaleLinear().domain([0, 140]),
grid: true,
axis: {
line: false,
label: "Timeline (Days)",
ticks: { size: 0 },
},
},
y: {
scale: () => scaleBand<string>().paddingInner(0.25),
axis: {
line: false,
ticks: { size: 0 },
},
},
},
color: {
domain: ["Complete", "In Progress", "Pending"],
range: ["var(--chart-1)", "var(--chart-2)", "var(--chart-3)"],
},
margin: { top: 24, right: 24, bottom: 32, left: 100 },
theme: {
foreground: "var(--muted-foreground)",
grid: "var(--border)",
background: "transparent",
},
},
{
focus: "group-y",
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>Project Phase Schedule</CardTitle>
<CardDescription>Horizontal interval timeline tracking initiative milestone ranges</CardDescription>
</CardHeader>
<CardContent>
<div className="h-[250px] w-full">
<Chart
definition={definition}
renderer={motionRenderer}
height={250}
ariaLabel="Project Phase Schedule Chart"
/>
</div>
</CardContent>
<CardFooter className="flex-col items-start gap-2 text-sm">
<div className="flex items-center gap-2 leading-none font-medium">
4 out of 5 core tasks progressing on schedule <Activity className="h-4 w-4 text-emerald-500" />
</div>
<div className="text-muted-foreground leading-none">
Bar start and end positions delineate task interval start and target completion dates
</div>
</CardFooter>
</Card>
);
}
export default IntervalTimeline;