# Stacked Time Area

> Stacked area chart displaying aggregated time series volume.

A shadcn/ui block from the [@pacekit](https://ui.pacekit.dev/?utm_source=shadcn.exchange) registry, listed on [shadcn.exchange](https://shadcn.exchange/r/pacekit/tanstack-charts-stacked-time-area).

- Page, with a live preview: https://shadcn.exchange/r/pacekit/tanstack-charts-stacked-time-area
- Screenshot: https://abzjhwgwpdpz7ua5.public.blob.vercel-storage.com/shots/pacekit/tanstack-charts-stacked-time-area-light-e6143236cd297849.png
- Registry item: https://ui.pacekit.dev/r/tanstack-charts-stacked-time-area.json
- Listed: 2026-10-21, source last changed 2026-10-21

## Install

```bash
npx shadcn@latest add https://shadcn.exchange/r/pacekit/tanstack-charts-stacked-time-area.json
```

Run it in a project that has shadcn/ui set up. It adds the files below and installs what they depend on.

## Dependencies

- npm packages: `@tanstack/charts`, `@tanstack/react-charts`, `d3-scale`
- Registry items: `card`

## Files

### `components/tanstack-charts/area/stacked-time-area.tsx`

```tsx
import { type ChartPoint, areaY, defineChart, ruleY } 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, scaleUtc } from "d3-scale";
import { Layers } from "lucide-react";

import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from "@/components/ui/card";

interface StackedAreaDatum {
    date: Date;
    category: string;
    value: number;
}

const motionRenderer = motion({
    initial: "always",
    transition: { type: "spring", stiffness: 170, damping: 18, mass: 1 },
});

function generateDemoData(): StackedAreaDatum[] {
    const baseDate = new Date(2025, 0, 1);
    const results: StackedAreaDatum[] = [];

    for (let i = 0; i < 10; i++) {
        const d = new Date(baseDate);
        d.setMonth(baseDate.getMonth() + i);
        results.push({ date: d, category: "Direct", value: 120 + i * 10 + Math.sin(i) * 15 });
        results.push({ date: d, category: "Organic", value: 200 + i * 15 + Math.cos(i) * 25 });
        results.push({ date: d, category: "Referral", value: 80 + i * 5 + Math.sin(i * 2) * 10 });
    }
    return results;
}

function shadcnTooltipContent<TDatum>(points: readonly ChartPoint<TDatum>[]) {
    if (points.length === 0) return { title: "", rows: [] };
    const dateVal = (points[0].datum as StackedAreaDatum)?.date;
    const dateObj = dateVal ? (dateVal instanceof Date ? dateVal : new Date(dateVal)) : null;
    const titleStr =
        dateObj && !isNaN(dateObj.getTime())
            ? dateObj.toLocaleDateString("en-US", { month: "short", year: "numeric" })
            : "";
    return {
        title: titleStr,
        rows: points.map((p) => {
            const d = p.datum as StackedAreaDatum;
            return {
                label: d.category,
                value: d.value.toFixed(0),
                color: p.color || "var(--chart-1)",
            };
        }),
    };
}

export function StackedTimeArea() {
    const data = useMemo(() => generateDemoData(), []);

    const definition = useMemo(
        () =>
            defineChart(
                {
                    renderer: motionRenderer,
                    marks: [
                        areaY(data, {
                            id: "traffic-stacked-area",
                            x: "date",
                            y: "value",
                            color: "category",
                            fillOpacity: 0.75,
                        }),
                        ruleY([0], { stroke: "var(--border)" }),
                    ],
                    scales: {
                        x: {
                            scale: scaleUtc,
                            axis: {
                                line: false,
                                label: "Month",
                                ticks: {
                                    size: 0,
                                    padding: 10,
                                    format: (d: any) => {
                                        const dateObj = d instanceof Date ? d : new Date(d);
                                        return isNaN(dateObj.getTime())
                                            ? ""
                                            : dateObj.toLocaleDateString("en-US", { month: "short" });
                                    },
                                },
                            },
                        },
                        y: {
                            scale: scaleLinear().domain([0, 700]),
                            grid: true,
                            axis: {
                                line: false,
                                label: "Visitors",
                                ticks: { size: 0 },
                            },
                        },
                    },
                    color: {
                        domain: ["Direct", "Organic", "Referral"],
                        range: ["var(--chart-1)", "var(--chart-2)", "var(--chart-3)"],
                    },
                    margin: { top: 24, right: 24, bottom: 32, left: 40 },
                    theme: {
                        foreground: "var(--muted-foreground)",
                        grid: "var(--border)",
                        background: "transparent",
                    },
                },
                {
                    focus: "group-x",
                    tooltip: {
                        use: tooltip,
                        className:
                            "font-sans shadow-none rounded-md bg-popover text-popover-foreground border p-2 text-xs",
                        anchor: "group-center",
                        placement: "auto",
                        content: (points) => shadcnTooltipContent(points),
                    },
                },
            ),
        [data],
    );

    return (
        <Card className="w-full max-w-xl">
            <CardHeader>
                <CardTitle>Acquisition Channel Stack</CardTitle>
                <CardDescription>Cumulative user acquisitions stacked across growth channels over time</CardDescription>
            </CardHeader>
            <CardContent>
                <div className="h-[250px] w-full">
                    <Chart
                        definition={definition}
                        renderer={motionRenderer}
                        height={250}
                        ariaLabel="Acquisition Channel Stack 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 acquisition represents 48% of total growth <Layers className="h-4 w-4 text-violet-500" />
                </div>
                <div className="text-muted-foreground leading-none">
                    Stacked areas display cumulative volume across acquisition sources
                </div>
            </CardFooter>
        </Card>
    );
}

export default StackedTimeArea;
```
