{
  "title": "Composed Chart",
  "description": "Layered combination composed of Area, Bar, and Line marks in a single view.",
  "name": "tanstack-charts-composed-chart",
  "dependencies": [
    "@tanstack/charts",
    "@tanstack/react-charts",
    "d3-scale",
    "d3-shape"
  ],
  "registryDependencies": [
    "card"
  ],
  "type": "registry:block",
  "files": [
    {
      "path": "components/blocks/tanstack-charts/line/composed-chart.tsx",
      "type": "registry:block",
      "content": "import { type ChartPoint, areaY, barY, d3Curve, defineChart, dot, lineY } 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 { scaleBand, scaleLinear } from \"d3-scale\";\nimport { curveMonotoneX } from \"d3-shape\";\nimport { Layers } from \"lucide-react\";\n\nimport { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from \"@/components/ui/card\";\n\ninterface WeatherDatum {\n    date: Date;\n    temp_max: number;\n    temp_min: number;\n    precipitation: number;\n    wind: number;\n}\n\nconst motionRenderer = motion({\n    initial: \"always\",\n    transition: { type: \"spring\", stiffness: 170, damping: 18, mass: 1 },\n});\n\nfunction generateDemoData(): WeatherDatum[] {\n    const results: WeatherDatum[] = [];\n    for (let i = 0; i < 7; i++) {\n        const date = new Date(Date.UTC(2024, 5, 10 + i));\n        results.push({\n            date,\n            temp_max: 22 + Math.floor(Math.random() * 8),\n            temp_min: 10 + Math.floor(Math.random() * 5),\n            precipitation: Math.floor(Math.random() * 18),\n            wind: 4 + Math.floor(Math.random() * 8),\n        });\n    }\n    return results;\n}\n\nfunction shadcnTooltipContent<TDatum>(points: readonly ChartPoint<TDatum>[]) {\n    if (points.length === 0) return { title: \"\", rows: [] };\n    const d = points[0].datum as WeatherDatum;\n    if (!d) return { title: \"\", rows: [] };\n    return {\n        title: d.date.toLocaleDateString(\"en-US\", {\n            weekday: \"short\",\n            month: \"short\",\n            day: \"numeric\",\n            timeZone: \"UTC\",\n        }),\n        rows: [\n            { label: \"Max Temp\", value: `${d.temp_max}°C`, color: \"var(--chart-1)\" },\n            { label: \"Min Temp\", value: `${d.temp_min}°C`, color: \"var(--chart-2)\" },\n            { label: \"Precipitation\", value: `${d.precipitation} mm`, color: \"var(--chart-3)\" },\n            { label: \"Wind Speed\", value: `${d.wind} km/h`, color: \"var(--foreground)\" },\n        ],\n    };\n}\n\nexport function ComposedChart() {\n    const data = useMemo(() => generateDemoData(), []);\n\n    const definition = useMemo(\n        () =>\n            defineChart(\n                {\n                    renderer: motionRenderer,\n                    marks: [\n                        areaY(data, {\n                            id: \"temp-max-area\",\n                            x: \"date\",\n                            y: \"temp_max\",\n                            fill: \"var(--chart-1)\",\n                            fillOpacity: 0.2,\n                            stroke: \"var(--chart-1)\",\n                            curve: d3Curve(curveMonotoneX),\n                        }),\n                        barY(data, {\n                            id: \"precipitation-bars\",\n                            x: \"date\",\n                            y: \"precipitation\",\n                            fill: \"var(--chart-3)\",\n                            maxThickness: 20,\n                            radius: 3,\n                        }),\n                        lineY(data, {\n                            id: \"temp-min-line\",\n                            x: \"date\",\n                            y: \"temp_min\",\n                            stroke: \"var(--chart-2)\",\n                            strokeWidth: 2.25,\n                            curve: d3Curve(curveMonotoneX),\n                        }),\n                        dot(data, {\n                            id: \"wind-dots\",\n                            x: \"date\",\n                            y: \"wind\",\n                            fill: \"var(--foreground)\",\n                            r: 4.5,\n                        }),\n                    ],\n                    scales: {\n                        x: {\n                            scale: () => scaleBand<Date>().paddingInner(0.2).paddingOuter(0.1),\n                            axis: {\n                                ticks: {\n                                    format: (v: Date) =>\n                                        v.toLocaleDateString(\"en-US\", { month: \"short\", day: \"numeric\", timeZone: \"UTC\" }),\n                                },\n                                label: \"Date\",\n                            },\n                        },\n                        y: { scale: scaleLinear, grid: true, axis: { label: \"Weather Metrics\" } },\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                    keyboard: true,\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: \"pointer\",\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>Composed Multi-Mark Layered Chart</CardTitle>\n                <CardDescription>\n                    Layered combination composed of Area, Bar, Line, and Dot marks in a single view\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=\"Composed Multi-Mark Layered 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                    Multi-mark composition unifies temperature, rainfall, and wind{\" \"}\n                    <Layers className=\"h-4 w-4 text-emerald-500\" />\n                </div>\n                <div className=\"text-muted-foreground leading-none\">\n                    Combines Area (max temp), Bar (rain), Line (min temp), and Dots (wind) in a single shared coordinate\n                    system\n                </div>\n            </CardFooter>\n        </Card>\n    );\n}\n\nexport default ComposedChart;\n"
    }
  ],
  "mcp": {
    "description": "Renders responsive data visualization with dark mode styling and customizable tooltips. Layered combination composed of Area, Bar, and Line marks in a single view. Relies on card. Use when presenting data insights or analytics dashboards.",
    "tags": [
      "chart",
      "tanstack"
    ]
  }
}