{
  "title": "Stacked Time Area",
  "description": "Stacked area chart displaying aggregated time series volume.",
  "name": "tanstack-charts-stacked-time-area",
  "dependencies": [
    "@tanstack/charts",
    "@tanstack/react-charts",
    "d3-scale"
  ],
  "registryDependencies": [
    "card"
  ],
  "type": "registry:block",
  "files": [
    {
      "path": "components/blocks/tanstack-charts/area/stacked-time-area.tsx",
      "type": "registry:block",
      "content": "import { type ChartPoint, areaY, defineChart, ruleY } from \"@tanstack/charts\";\nimport { motion } from \"@tanstack/charts/motion\";\nimport { tooltip } from \"@tanstack/charts/tooltip\";\nimport { Chart } from \"@tanstack/react-charts/core\";\nimport { useMemo } from \"react\";\n\nimport { scaleLinear, scaleUtc } from \"d3-scale\";\nimport { Layers } from \"lucide-react\";\n\nimport { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from \"@/components/ui/card\";\n\ninterface StackedAreaDatum {\n    date: Date;\n    category: string;\n    value: number;\n}\n\nconst motionRenderer = motion({\n    initial: \"always\",\n    transition: { type: \"spring\", stiffness: 170, damping: 18, mass: 1 },\n});\n\nfunction generateDemoData(): StackedAreaDatum[] {\n    const baseDate = new Date(2025, 0, 1);\n    const results: StackedAreaDatum[] = [];\n\n    for (let i = 0; i < 10; i++) {\n        const d = new Date(baseDate);\n        d.setMonth(baseDate.getMonth() + i);\n        results.push({ date: d, category: \"Direct\", value: 120 + i * 10 + Math.sin(i) * 15 });\n        results.push({ date: d, category: \"Organic\", value: 200 + i * 15 + Math.cos(i) * 25 });\n        results.push({ date: d, category: \"Referral\", value: 80 + i * 5 + Math.sin(i * 2) * 10 });\n    }\n    return results;\n}\n\nfunction shadcnTooltipContent<TDatum>(points: readonly ChartPoint<TDatum>[]) {\n    if (points.length === 0) return { title: \"\", rows: [] };\n    const dateVal = (points[0].datum as StackedAreaDatum)?.date;\n    const dateObj = dateVal ? (dateVal instanceof Date ? dateVal : new Date(dateVal)) : null;\n    const titleStr =\n        dateObj && !isNaN(dateObj.getTime())\n            ? dateObj.toLocaleDateString(\"en-US\", { month: \"short\", year: \"numeric\" })\n            : \"\";\n    return {\n        title: titleStr,\n        rows: points.map((p) => {\n            const d = p.datum as StackedAreaDatum;\n            return {\n                label: d.category,\n                value: d.value.toFixed(0),\n                color: p.color || \"var(--chart-1)\",\n            };\n        }),\n    };\n}\n\nexport function StackedTimeArea() {\n    const data = useMemo(() => generateDemoData(), []);\n\n    const definition = useMemo(\n        () =>\n            defineChart(\n                {\n                    renderer: motionRenderer,\n                    marks: [\n                        areaY(data, {\n                            id: \"traffic-stacked-area\",\n                            x: \"date\",\n                            y: \"value\",\n                            color: \"category\",\n                            fillOpacity: 0.75,\n                        }),\n                        ruleY([0], { stroke: \"var(--border)\" }),\n                    ],\n                    scales: {\n                        x: {\n                            scale: scaleUtc,\n                            axis: {\n                                line: false,\n                                label: \"Month\",\n                                ticks: {\n                                    size: 0,\n                                    padding: 10,\n                                    format: (d: any) => {\n                                        const dateObj = d instanceof Date ? d : new Date(d);\n                                        return isNaN(dateObj.getTime())\n                                            ? \"\"\n                                            : dateObj.toLocaleDateString(\"en-US\", { month: \"short\" });\n                                    },\n                                },\n                            },\n                        },\n                        y: {\n                            scale: scaleLinear().domain([0, 700]),\n                            grid: true,\n                            axis: {\n                                line: false,\n                                label: \"Visitors\",\n                                ticks: { size: 0 },\n                            },\n                        },\n                    },\n                    color: {\n                        domain: [\"Direct\", \"Organic\", \"Referral\"],\n                        range: [\"var(--chart-1)\", \"var(--chart-2)\", \"var(--chart-3)\"],\n                    },\n                    margin: { top: 24, right: 24, bottom: 32, left: 40 },\n                    theme: {\n                        foreground: \"var(--muted-foreground)\",\n                        grid: \"var(--border)\",\n                        background: \"transparent\",\n                    },\n                },\n                {\n                    focus: \"group-x\",\n                    tooltip: {\n                        use: tooltip,\n                        className:\n                            \"font-sans shadow-none rounded-md bg-popover text-popover-foreground border p-2 text-xs\",\n                        anchor: \"group-center\",\n                        placement: \"auto\",\n                        content: (points) => shadcnTooltipContent(points),\n                    },\n                },\n            ),\n        [data],\n    );\n\n    return (\n        <Card className=\"w-full max-w-xl\">\n            <CardHeader>\n                <CardTitle>Acquisition Channel Stack</CardTitle>\n                <CardDescription>Cumulative user acquisitions stacked across growth channels over time</CardDescription>\n            </CardHeader>\n            <CardContent>\n                <div className=\"h-[250px] w-full\">\n                    <Chart\n                        definition={definition}\n                        renderer={motionRenderer}\n                        height={250}\n                        ariaLabel=\"Acquisition Channel Stack Chart\"\n                    />\n                </div>\n            </CardContent>\n            <CardFooter className=\"flex-col items-start gap-2 text-sm\">\n                <div className=\"flex items-center gap-2 leading-none font-medium\">\n                    Organic acquisition represents 48% of total growth <Layers className=\"h-4 w-4 text-violet-500\" />\n                </div>\n                <div className=\"text-muted-foreground leading-none\">\n                    Stacked areas display cumulative volume across acquisition sources\n                </div>\n            </CardFooter>\n        </Card>\n    );\n}\n\nexport default StackedTimeArea;\n"
    }
  ],
  "mcp": {
    "description": "Renders responsive data visualization with dark mode styling and customizable tooltips. Stacked area chart displaying aggregated time series volume. Relies on card. Use when presenting data insights or analytics dashboards.",
    "tags": [
      "chart",
      "tanstack",
      "area"
    ]
  }
}