
A default single area chart displaying website visitor trends built with TanStack Charts.
components/tanstack-charts/area/area-chart-default.tsx
import { type ChartPoint, areaY, d3Curve, 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 { 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 shadcnTooltipContent<TDatum>(points: readonly ChartPoint<TDatum>[]) {
return {
title: String(points[0]?.xValue ?? ""),
rows: points.map((point) => ({
label: "Desktop",
value: Number(point.yValue ?? point.xValue ?? 0).toLocaleString("en-US"),
color: point.color,
})),
};
}
export function AreaChartDefault() {
const data = useMemo(() => generateDemoData(), []);
const definition = useMemo(
() =>
defineChart(
{
renderer: motionRenderer,
motion: {
transition: { type: "spring", stiffness: 170, damping: 18, mass: 1 },
},
marks: [
areaY(data, {
id: "visitor-area",
x: "month",
y: "desktop",
curve: d3Curve(curveNatural),
fill: "var(--chart-1)",
fillOpacity: 0.4,
stroke: "var(--chart-1)",
strokeWidth: 1.5,
}),
],
scales: {
x: {
scale: scalePoint,
axis: {
line: false,
ticks: {
size: 0,
padding: 10,
format: (value: string) => value.slice(0, 3),
},
},
},
y: {
scale: scaleLinear,
grid: true,
axis: { line: false, ticks: false, tickLabels: false },
},
},
color: {
domain: ["desktop"],
range: ["var(--chart-1)"],
},
margin: { top: 10, right: 12, bottom: 30, 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-2xl">
<CardHeader>
<CardTitle>Monthly Active Visitors</CardTitle>
<CardDescription>Unique web visitors across web and app sessions</CardDescription>
</CardHeader>
<CardContent>
<div className="h-[250px] w-full">
<Chart
definition={definition}
renderer={motionRenderer}
height={250}
ariaLabel="Monthly Active Visitors Chart"
/>
</div>
</CardContent>
<CardFooter className="flex-col items-start gap-2 text-sm">
<div className="flex items-center gap-2 leading-none font-medium">
Organic conversion up by 5.2% this month <TrendingUp className="h-4 w-4 text-emerald-500" />
</div>
<div className="text-muted-foreground leading-none">January - June 2024</div>
</CardFooter>
</Card>
);
}
export default AreaChartDefault;