
A stacked gradient area chart demonstrating desktop and mobile visitors over 6 months built with TanStack Charts.
components/tanstack-charts/area/area-chart-gradient.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;
mobile: number;
}
const motionRenderer = motion({
initial: "always",
transition: { type: "spring", stiffness: 170, damping: 18, mass: 1 },
});
function generateDemoData(): MonthDatum[] {
return [
{ month: "January", desktop: 186, mobile: 80 },
{ month: "February", desktop: 305, mobile: 200 },
{ month: "March", desktop: 237, mobile: 120 },
{ month: "April", desktop: 73, mobile: 190 },
{ month: "May", desktop: 209, mobile: 130 },
{ month: "June", desktop: 214, mobile: 140 },
];
}
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 AreaChartGradient() {
const data = useMemo(() => generateDemoData(), []);
const definition = useMemo(
() =>
defineChart(
{
renderer: motionRenderer,
motion: {
transition: { type: "spring", stiffness: 170, damping: 18, mass: 1 },
},
gradients: [
{
id: "fillDesktop",
x1: 0,
y1: 0,
x2: 0,
y2: 1,
stops: [
{ offset: 0.05, color: "var(--chart-1)", opacity: 0.8 },
{ offset: 0.95, color: "var(--chart-1)", opacity: 0.1 },
],
},
{
id: "fillMobile",
x1: 0,
y1: 0,
x2: 0,
y2: 1,
stops: [
{ offset: 0.05, color: "var(--chart-2)", opacity: 0.8 },
{ offset: 0.95, color: "var(--chart-2)", opacity: 0.1 },
],
},
],
marks: [
areaY(data, {
id: "desktop-area",
x: "month",
y: "desktop",
curve: d3Curve(curveNatural),
fill: "url(#fillDesktop)",
stroke: "var(--chart-1)",
strokeWidth: 1.5,
}),
areaY(data, {
id: "mobile-area",
x: "month",
y: "mobile",
curve: d3Curve(curveNatural),
fill: "url(#fillMobile)",
stroke: "var(--chart-2)",
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", "mobile"],
range: ["var(--chart-1)", "var(--chart-2)"],
},
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>Global CDN Bandwidth</CardTitle>
<CardDescription>Network egress traffic across edge locations (TB)</CardDescription>
</CardHeader>
<CardContent>
<div className="h-[250px] w-full">
<Chart
definition={definition}
renderer={motionRenderer}
height={250}
ariaLabel="Global CDN Bandwidth Chart"
/>
</div>
</CardContent>
<CardFooter className="flex-col items-start gap-2 text-sm">
<div className="flex items-center gap-2 leading-none font-medium">
Egress volume increased by 12.4% this quarter <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 AreaChartGradient;