components/tanstack-charts/line/composed-chart.tsx
import { type ChartPoint, areaY, barY, d3Curve, defineChart, dot, 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 { scaleBand, scaleLinear } from "d3-scale";
import { curveMonotoneX } from "d3-shape";
import { Layers } from "lucide-react";
import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from "@/components/ui/card";
interface WeatherDatum {
date: Date;
temp_max: number;
temp_min: number;
precipitation: number;
wind: number;
}
const motionRenderer = motion({
initial: "always",
transition: { type: "spring", stiffness: 170, damping: 18, mass: 1 },
});
function generateDemoData(): WeatherDatum[] {
const results: WeatherDatum[] = [];
for (let i = 0; i < 7; i++) {
const date = new Date(Date.UTC(2024, 5, 10 + i));
results.push({
date,
temp_max: 22 + Math.floor(Math.random() * 8),
temp_min: 10 + Math.floor(Math.random() * 5),
precipitation: Math.floor(Math.random() * 18),
wind: 4 + Math.floor(Math.random() * 8),
});
}
return results;
}
function shadcnTooltipContent<TDatum>(points: readonly ChartPoint<TDatum>[]) {
if (points.length === 0) return { title: "", rows: [] };
const d = points[0].datum as WeatherDatum;
if (!d) return { title: "", rows: [] };
return {
title: d.date.toLocaleDateString("en-US", {
weekday: "short",
month: "short",
day: "numeric",
timeZone: "UTC",
}),
rows: [
{ label: "Max Temp", value: `${d.temp_max}°C`, color: "var(--chart-1)" },
{ label: "Min Temp", value: `${d.temp_min}°C`, color: "var(--chart-2)" },
{ label: "Precipitation", value: `${d.precipitation} mm`, color: "var(--chart-3)" },
{ label: "Wind Speed", value: `${d.wind} km/h`, color: "var(--foreground)" },
],
};
}
export function ComposedChart() {
const data = useMemo(() => generateDemoData(), []);
const definition = useMemo(
() =>
defineChart(
{
renderer: motionRenderer,
marks: [
areaY(data, {
id: "temp-max-area",
x: "date",
y: "temp_max",
fill: "var(--chart-1)",
fillOpacity: 0.2,
stroke: "var(--chart-1)",
curve: d3Curve(curveMonotoneX),
}),
barY(data, {
id: "precipitation-bars",
x: "date",
y: "precipitation",
fill: "var(--chart-3)",
maxThickness: 20,
radius: 3,
}),
lineY(data, {
id: "temp-min-line",
x: "date",
y: "temp_min",
stroke: "var(--chart-2)",
strokeWidth: 2.25,
curve: d3Curve(curveMonotoneX),
}),
dot(data, {
id: "wind-dots",
x: "date",
y: "wind",
fill: "var(--foreground)",
r: 4.5,
}),
],
scales: {
x: {
scale: () => scaleBand<Date>().paddingInner(0.2).paddingOuter(0.1),
axis: {
ticks: {
format: (v: Date) =>
v.toLocaleDateString("en-US", { month: "short", day: "numeric", timeZone: "UTC" }),
},
label: "Date",
},
},
y: { scale: scaleLinear, grid: true, axis: { label: "Weather Metrics" } },
},
margin: { top: 24, right: 24, bottom: 32, left: 45 },
theme: {
foreground: "var(--muted-foreground)",
grid: "var(--border)",
background: "transparent",
},
},
{
keyboard: true,
tooltip: {
use: tooltip,
className:
"font-sans shadow-none rounded-md bg-popover text-popover-foreground border p-2 text-xs",
anchor: "pointer",
placement: "auto",
content: (points) => shadcnTooltipContent(points),
},
},
),
[data],
);
return (
<Card className="w-full max-w-xl">
<CardHeader>
<CardTitle>Composed Multi-Mark Layered Chart</CardTitle>
<CardDescription>
Layered combination composed of Area, Bar, Line, and Dot marks in a single view
</CardDescription>
</CardHeader>
<CardContent>
<div className="h-[250px] w-full">
<Chart
definition={definition}
renderer={motionRenderer}
height={250}
ariaLabel="Composed Multi-Mark Layered Chart"
/>
</div>
</CardContent>
<CardFooter className="flex-col items-start gap-2 text-sm">
<div className="flex items-center gap-2 leading-none font-medium">
Multi-mark composition unifies temperature, rainfall, and wind{" "}
<Layers className="h-4 w-4 text-emerald-500" />
</div>
<div className="text-muted-foreground leading-none">
Combines Area (max temp), Bar (rain), Line (min temp), and Dots (wind) in a single shared coordinate
system
</div>
</CardFooter>
</Card>
);
}
export default ComposedChart;