{
  "title": "Normalized Stacked Area",
  "description": "100% normalized stacked area chart visualizing sector percentage share.",
  "name": "tanstack-charts-normalized-stacked-area",
  "dependencies": [
    "@tanstack/charts",
    "@tanstack/react-charts",
    "d3-scale"
  ],
  "registryDependencies": [
    "card"
  ],
  "type": "registry:block",
  "files": [
    {
      "path": "components/blocks/tanstack-charts/area/normalized-stacked-area.tsx",
      "type": "registry:block",
      "content": "import { type ChartPoint, areaY, defineChart, ruleY, stack } 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 { PieChart } from \"lucide-react\";\n\nimport { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from \"@/components/ui/card\";\n\ninterface ShareDatum {\n    date: Date;\n    industry: string;\n    unemployed: number;\n}\n\nconst motionRenderer = motion({\n    initial: \"always\",\n    transition: { type: \"spring\", stiffness: 170, damping: 18, mass: 1 },\n});\n\nfunction generateDemoData(): ShareDatum[] {\n    const baseDate = new Date(2025, 0, 1);\n    const results: ShareDatum[] = [];\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, industry: \"Tech\", unemployed: 150 + Math.sin(i * 0.5) * 30 });\n        results.push({ date: d, industry: \"Finance\", unemployed: 220 + Math.cos(i * 0.5) * 40 });\n        results.push({ date: d, industry: \"Services\", unemployed: 180 + Math.sin(i * 0.8) * 20 });\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 ShareDatum)?.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 ShareDatum;\n            return {\n                label: d.industry,\n                value: `${d.unemployed.toFixed(0)}k volume`,\n                color: p.color || \"var(--chart-1)\",\n            };\n        }),\n    };\n}\n\nexport function NormalizedStackedArea() {\n    const data = useMemo(() => generateDemoData(), []);\n\n    const definition = useMemo(\n        () =>\n            defineChart(\n                {\n                    renderer: motionRenderer,\n                    marks: [\n                        areaY(data, {\n                            id: \"normalized-area\",\n                            x: \"date\",\n                            y: \"unemployed\",\n                            color: \"industry\",\n                            fillOpacity: 0.8,\n                            layout: stack({ offset: \"normalize\" }),\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, 1]),\n                            grid: true,\n                            axis: {\n                                line: false,\n                                label: \"Relative Share (%)\",\n                                ticks: {\n                                    size: 0,\n                                    format: (v: number) => `${(v * 100).toFixed(0)}%`,\n                                },\n                            },\n                        },\n                    },\n                    color: {\n                        domain: [\"Tech\", \"Finance\", \"Services\"],\n                        range: [\"var(--chart-1)\", \"var(--chart-2)\", \"var(--chart-3)\"],\n                    },\n                    margin: { top: 24, right: 24, bottom: 32, left: 45 },\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>Normalized Sector Share</CardTitle>\n                <CardDescription>\n                    100% normalized stacked area chart displaying proportional market share\n                </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=\"Normalized Sector Share 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                    Proportions total 100% across all observed temporal windows{\" \"}\n                    <PieChart className=\"h-4 w-4 text-violet-500\" />\n                </div>\n                <div className=\"text-muted-foreground leading-none\">\n                    Normalized offset scales each slice to represent percentage share of sum\n                </div>\n            </CardFooter>\n        </Card>\n    );\n}\n\nexport default NormalizedStackedArea;\n"
    }
  ],
  "mcp": {
    "description": "Renders responsive data visualization with dark mode styling and customizable tooltips. 100% normalized stacked area chart visualizing sector percentage share. Relies on card. Use when presenting data insights or analytics dashboards.",
    "tags": [
      "chart",
      "tanstack",
      "area"
    ]
  }
}