
A linear area chart tracking cloud infrastructure compute consumption built with TanStack Charts.
components/tanstack-charts/area/area-linear.tsx
import { areaY, 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 { max } from "d3-array";
import { scaleLinear, scalePoint } from "d3-scale";
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", usage: 186 },
{ month: "February", usage: 305 },
{ month: "March", usage: 237 },
{ month: "April", usage: 73 },
{ month: "May", usage: 209 },
{ month: "June", usage: 214 },
];
export function AreaLinear() {
const definition = useMemo(() => {
const yMax = max(chartData, (d) => d.usage) ?? 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: "Usage",
value: String(datum.usage),
color: "var(--chart-1)",
},
],
};
},
},
marks: [
areaY(chartData, {
id: "usage",
x: (d) => d.month.slice(0, 3),
y: (d) => d.usage,
fill: "var(--chart-1)",
fillOpacity: 0.25,
}),
lineY(chartData, {
id: "usage-line",
x: (d) => d.month.slice(0, 3),
y: (d) => d.usage,
stroke: "var(--chart-1)",
strokeWidth: 2,
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-0">
<CardHeader>
<CardTitle className="text-lg">Compute Engine Allocation</CardTitle>
<CardDescription>Total vCPU hours consumed across core production workloads</CardDescription>
</CardHeader>
<CardContent>
<Chart
definition={definition}
renderer={motionRenderer}
height={250}
ariaLabel="Linear area chart tracking cloud infrastructure compute consumption"
/>
</CardContent>
</Card>
);
}