
A step line chart displaying infrastructure capacity and active server counts built with TanStack Charts.
components/tanstack-charts/line/line-step.tsx
import { defineChart, lineY } from "@tanstack/charts";
import { d3Curve } from "@tanstack/charts/d3/shape";
import { motion } from "@tanstack/charts/motion";
import { tooltip } from "@tanstack/charts/tooltip";
import { Chart } from "@tanstack/react-charts/core";
import { useMemo } from "react";
import { max } from "d3-array";
import { scaleLinear, scalePoint } from "d3-scale";
import { curveStep } from "d3-shape";
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card";
const motionRenderer = motion({
initial: "always",
transition: { type: "spring", stiffness: 170, damping: 18, mass: 1 },
});
const chartData = [
{ month: "January", activeServers: 42 },
{ month: "February", activeServers: 58 },
{ month: "March", activeServers: 65 },
{ month: "April", activeServers: 80 },
{ month: "May", activeServers: 70 },
{ month: "June", activeServers: 50 },
{ month: "July", activeServers: 60 },
{ month: "August", activeServers: 80 },
{ month: "September", activeServers: 100 },
];
export function LineStep() {
const definition = useMemo(() => {
const yMax = max(chartData, (d) => d.activeServers) ?? 0;
return defineChart({
renderer: motionRenderer,
motion: {
transition: { type: "spring", stiffness: 170, damping: 18, mass: 1 },
},
focus: "group-x",
tooltip: {
use: tooltip,
motion: { type: "spring", stiffness: 170, damping: 18 },
className: "font-sans! shadow-none! capitalize!",
content: (points) => {
const datum = points[0]?.datum as (typeof chartData)[number];
if (!datum) return { rows: [] };
return {
title: String(points[0].xValue),
rows: [
{
label: "Active Servers",
value: String(datum.activeServers),
color: "var(--chart-1)",
},
],
};
},
},
marks: [
lineY(chartData, {
id: "activeServers",
x: (d) => d.month.slice(0, 3),
y: (d) => d.activeServers,
stroke: "var(--chart-1)",
strokeWidth: 2,
curve: d3Curve(curveStep),
points: false,
}),
],
scales: {
x: {
scale: scalePoint().domain(chartData.map((d) => d.month.slice(0, 3))),
},
y: {
scale: scaleLinear().domain([0, yMax]).nice(),
grid: true,
},
},
});
}, []);
return (
<Card className="gap-4">
<CardHeader>
<CardTitle className="text-lg">Infrastructure Capacity</CardTitle>
<CardDescription>Active Instances (Q1–Q3 2024)</CardDescription>
</CardHeader>
<CardContent>
<Chart
definition={definition}
renderer={motionRenderer}
height={250}
ariaLabel="Step line chart showing active server count over time"
/>
</CardContent>
</Card>
);
}