{
  "title": "Hexbin Density",
  "description": "Spatial hexbin clustering mapping point density concentration.",
  "name": "tanstack-charts-hexbin-density",
  "dependencies": [
    "@tanstack/charts",
    "@tanstack/react-charts",
    "d3-scale"
  ],
  "registryDependencies": [
    "card"
  ],
  "type": "registry:block",
  "files": [
    {
      "path": "components/blocks/tanstack-charts/area/hexbin-density.tsx",
      "type": "registry:block",
      "content": "import { type ChartPoint, defineChart } from \"@tanstack/charts\";\nimport { motion } from \"@tanstack/charts/motion\";\nimport { hexbin } from \"@tanstack/charts/spatial/hexbin\";\nimport { tooltip } from \"@tanstack/charts/tooltip\";\nimport { Chart } from \"@tanstack/react-charts/core\";\nimport { useMemo } from \"react\";\n\nimport { scaleLinear, scaleThreshold } from \"d3-scale\";\nimport { Hexagon } from \"lucide-react\";\n\nimport { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from \"@/components/ui/card\";\n\ninterface HexDatum {\n    weight: number;\n    mpg: number;\n}\n\nconst motionRenderer = motion({\n    initial: \"always\",\n    transition: { type: \"spring\", stiffness: 170, damping: 18, mass: 1 },\n});\n\nfunction generateDemoData(): HexDatum[] {\n    const results: HexDatum[] = [];\n    for (let i = 0; i < 250; i++) {\n        const weight = 1800 + Math.random() * 3200;\n        const baseMpg = 45 - (weight - 1800) * 0.008;\n        const mpg = Math.max(8, baseMpg + (Math.random() - 0.5) * 12);\n        results.push({ weight, mpg });\n    }\n    return results;\n}\n\nfunction shadcnTooltipContent<TDatum>(points: readonly ChartPoint<TDatum>[]) {\n    if (points.length === 0) return { title: \"\", rows: [] };\n    const datum = points[0].datum as { x: number; y: number; count: number };\n    if (!datum) return { title: \"\", rows: [] };\n    return {\n        title: `Bin Center: ${datum.x?.toFixed(0)} lb, ${datum.y?.toFixed(1)} mpg`,\n        rows: [{ label: \"Data Point Count\", value: `${datum.count} items`, color: \"var(--chart-1)\" }],\n    };\n}\n\nexport function HexbinDensity() {\n    const data = useMemo(() => generateDemoData(), []);\n\n    const definition = useMemo(\n        () =>\n            defineChart(\n                {\n                    renderer: motionRenderer,\n                    marks: [\n                        hexbin(data, {\n                            x: \"weight\",\n                            y: \"mpg\",\n                            binWidth: 24,\n                            color: \"count\",\n                            r: 11,\n                            stroke: \"var(--background)\",\n                            strokeWidth: 0.75,\n                        }),\n                    ],\n                    scales: {\n                        x: {\n                            scale: scaleLinear().domain([1500, 5200]),\n                            grid: true,\n                            axis: { label: \"Vehicle Weight (lb)\" },\n                        },\n                        y: {\n                            scale: scaleLinear().domain([5, 50]),\n                            grid: true,\n                            axis: { label: \"Fuel Economy (mpg)\" },\n                        },\n                    },\n                    color: {\n                        scale: scaleThreshold<number, string>,\n                        domain: [3, 8, 15],\n                        range: [\"var(--chart-5)\", \"var(--chart-3)\", \"var(--chart-2)\", \"var(--chart-1)\"],\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>Hexagonal Point Density Binned Plot</CardTitle>\n                <CardDescription>\n                    Spatial hexbin clustering mapping vehicle weight vs fuel efficiency density\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=\"Hexagonal Point Density Binned Plot 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                    High density concentration around 2,500 lbs & 30 mpg{\" \"}\n                    <Hexagon className=\"h-4 w-4 text-emerald-500\" />\n                </div>\n                <div className=\"text-muted-foreground leading-none\">\n                    Hexagonal tessellation avoids visual scatter plot overlap in dense bivariate distributions\n                </div>\n            </CardFooter>\n        </Card>\n    );\n}\n\nexport default HexbinDensity;\n"
    }
  ],
  "mcp": {
    "description": "Renders responsive data visualization with dark mode styling and customizable tooltips. Spatial hexbin clustering mapping point density concentration. Relies on card. Use when presenting data insights or analytics dashboards.",
    "tags": [
      "chart",
      "tanstack"
    ]
  }
}