{
  "title": "Area Chart Legend",
  "description": "An area chart displaying stacked desktop and mobile visitors with a bottom legend built with TanStack Charts.",
  "name": "tanstack-charts-area-legend",
  "dependencies": [
    "@tanstack/charts",
    "@tanstack/react-charts",
    "d3-scale",
    "d3-shape"
  ],
  "registryDependencies": [
    "card"
  ],
  "type": "registry:block",
  "files": [
    {
      "path": "components/blocks/tanstack-charts/area/area-chart-legend.tsx",
      "type": "registry:block",
      "content": "import { type ChartPoint, areaY, d3Curve, defineChart, 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, scalePoint } from \"d3-scale\";\nimport { curveNatural } from \"d3-shape\";\nimport { TrendingUp } from \"lucide-react\";\n\nimport { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from \"@/components/ui/card\";\n\ninterface AreaSeriesDatum {\n    month: string;\n    series: \"desktop\" | \"mobile\";\n    value: number;\n}\n\nconst monthSeries = [\"desktop\", \"mobile\", \"tablet\"] as const;\nconst chartColors = [\"var(--chart-1)\", \"var(--chart-2)\"];\n\nconst motionRenderer = motion({\n    initial: \"always\",\n    transition: { type: \"spring\", stiffness: 170, damping: 18, mass: 1 },\n});\n\nfunction generateDemoData(): AreaSeriesDatum[] {\n    return [\n        { month: \"January\", series: \"desktop\", value: 186 },\n        { month: \"January\", series: \"mobile\", value: 80 },\n        { month: \"February\", series: \"desktop\", value: 305 },\n        { month: \"February\", series: \"mobile\", value: 200 },\n        { month: \"March\", series: \"desktop\", value: 237 },\n        { month: \"March\", series: \"mobile\", value: 120 },\n        { month: \"April\", series: \"desktop\", value: 73 },\n        { month: \"April\", series: \"mobile\", value: 190 },\n        { month: \"May\", series: \"desktop\", value: 209 },\n        { month: \"May\", series: \"mobile\", value: 130 },\n        { month: \"June\", series: \"desktop\", value: 214 },\n        { month: \"June\", series: \"mobile\", value: 140 },\n    ];\n}\n\nfunction titleCase(value: string) {\n    return value.charAt(0).toUpperCase() + value.slice(1);\n}\n\nfunction shadcnTooltipContent<TDatum>(points: readonly ChartPoint<TDatum>[]) {\n    return {\n        title: String(points[0]?.xValue ?? \"\"),\n        rows: points.map((point) => ({\n            label: titleCase(\n                String(point.group ?? point.markId.replace(/-?(bars|lines|areas|slices|values|radar)$/u, \"\")),\n            ),\n            value: Number(point.yValue ?? point.xValue ?? 0).toLocaleString(\"en-US\"),\n            color: point.color,\n        })),\n    };\n}\n\nexport function AreaChartLegend() {\n    const data = useMemo(() => generateDemoData(), []);\n\n    const definition = useMemo(\n        () =>\n            defineChart(\n                {\n                    renderer: motionRenderer,\n                    motion: {\n                        transition: { type: \"spring\", stiffness: 170, damping: 18, mass: 1 },\n                    },\n                    marks: [\n                        areaY(data, {\n                            id: \"visitor-areas\",\n                            x: \"month\",\n                            y: \"value\",\n                            z: \"series\",\n                            color: \"series\",\n                            key: (row) => `${row.month}:${row.series}`,\n                            layout: stack({\n                                order: [\"mobile\", \"desktop\"],\n                            }),\n                            curve: d3Curve(curveNatural),\n                            fillOpacity: 0.4,\n                            stroke: (row) => (row.series === \"mobile\" ? chartColors[1] : chartColors[0]),\n                            strokeWidth: 1.5,\n                        }),\n                    ],\n                    scales: {\n                        x: {\n                            scale: scalePoint,\n                            axis: {\n                                line: false,\n                                ticks: {\n                                    size: 0,\n                                    padding: 10,\n                                    format: (value: string) => value.slice(0, 3),\n                                },\n                            },\n                        },\n                        y: {\n                            scale: scaleLinear,\n                            grid: true,\n                            axis: { line: false, ticks: false, tickLabels: false },\n                        },\n                    },\n                    color: {\n                        domain: monthSeries,\n                        range: chartColors,\n                    },\n                    margin: { top: 10, right: 12, bottom: 30, left: 12 },\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                        motion: { type: \"spring\", stiffness: 170, damping: 18 },\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                        sort: \"color-domain\",\n                        content: (points) => shadcnTooltipContent(points),\n                    },\n                },\n            ),\n        [data],\n    );\n\n    return (\n        <Card className=\"w-full max-w-2xl\">\n            <CardHeader>\n                <CardTitle>Platform Traffic Breakdown</CardTitle>\n                <CardDescription>Desktop vs Mobile traffic comparison across regional endpoints</CardDescription>\n            </CardHeader>\n            <CardContent>\n                <div className=\"h-[220px] w-full\">\n                    <Chart\n                        definition={definition}\n                        renderer={motionRenderer}\n                        height={220}\n                        ariaLabel=\"Platform Traffic Breakdown Chart\"\n                    />\n                </div>\n                <div className=\"text-muted-foreground mt-3 flex items-center justify-center gap-4 text-xs\">\n                    <div className=\"flex items-center gap-1.5\">\n                        <span className=\"bg-chart-1 h-2 w-2 rounded-full\" />\n                        <span>Desktop</span>\n                    </div>\n                    <div className=\"flex items-center gap-1.5\">\n                        <span className=\"bg-chart-2 h-2 w-2 rounded-full\" />\n                        <span>Mobile</span>\n                    </div>\n                </div>\n            </CardContent>\n            <CardFooter className=\"flex-col items-start gap-2 pt-2 text-sm\">\n                <div className=\"flex items-center gap-2 leading-none font-medium\">\n                    Mobile engagement up by 7.4% this period <TrendingUp className=\"h-4 w-4 text-emerald-500\" />\n                </div>\n                <div className=\"text-muted-foreground leading-none\">January - June 2024</div>\n            </CardFooter>\n        </Card>\n    );\n}\n\nexport default AreaChartLegend;\n"
    }
  ],
  "mcp": {
    "description": "Renders responsive data visualization with dark mode styling and customizable tooltips. An area chart displaying stacked desktop and mobile visitors with a bottom legend built with TanStack Charts. Relies on card. Use when presenting data insights or analytics dashboards.",
    "tags": [
      "chart",
      "tanstack",
      "area"
    ]
  }
}