{
  "title": "Density Contours",
  "description": "Kernel density estimation contours displaying 2D spatial clustering concentration.",
  "name": "tanstack-charts-density-contours",
  "dependencies": [
    "@tanstack/charts",
    "@tanstack/react-charts",
    "d3-scale"
  ],
  "registryDependencies": [
    "card"
  ],
  "type": "registry:block",
  "files": [
    {
      "path": "components/blocks/tanstack-charts/area/density-contours.tsx",
      "type": "registry:block",
      "content": "import { type ChartPoint, defineChart, dot } from \"@tanstack/charts\";\nimport { motion } from \"@tanstack/charts/motion\";\nimport { densityContour } from \"@tanstack/charts/spatial/density\";\nimport { tooltip } from \"@tanstack/charts/tooltip\";\nimport { Chart } from \"@tanstack/react-charts/core\";\nimport { useMemo } from \"react\";\n\nimport { scaleLinear } from \"d3-scale\";\nimport { CircleDot } from \"lucide-react\";\n\nimport { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from \"@/components/ui/card\";\n\ninterface DensityPointDatum {\n    length: number;\n    depth: number;\n}\n\nconst motionRenderer = motion({\n    initial: \"always\",\n    transition: { type: \"spring\", stiffness: 170, damping: 18, mass: 1 },\n});\n\nconst densityBandwidth = 12;\nconst densityThresholds = [0.0004, 0.0008, 0.0012, 0.0016, 0.002];\n\nfunction generateDemoData(): DensityPointDatum[] {\n    const results: DensityPointDatum[] = [];\n    for (let i = 0; i < 100; i++) {\n        const length = 34 + (Math.sin(i * 0.4) + 1) * 11 + (i % 4);\n        const depth = 13 + (Math.cos(i * 0.6) + 1) * 4 + (i % 3);\n        results.push({ length, depth });\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 DensityPointDatum;\n    if (!d) return { title: \"\", rows: [] };\n    return {\n        title: \"Sample Observation\",\n        rows: [\n            {\n                label: \"Culmen Length\",\n                value: `${d.length.toFixed(1)} mm`,\n                color: \"var(--chart-1)\",\n            },\n            {\n                label: \"Culmen Depth\",\n                value: `${d.depth.toFixed(1)} mm`,\n                color: \"var(--chart-2)\",\n            },\n        ],\n    };\n}\n\nexport function DensityContours() {\n    const data = useMemo(() => generateDemoData(), []);\n\n    const definition = useMemo(\n        () =>\n            defineChart(\n                {\n                    renderer: motionRenderer,\n                    marks: [\n                        densityContour(data, {\n                            id: \"density-contour-rings\",\n                            x: \"length\",\n                            y: \"depth\",\n                            bandwidth: densityBandwidth,\n                            thresholds: densityThresholds,\n                            fill: \"var(--chart-1)\",\n                            fillOpacity: 0.16,\n                            stroke: \"var(--chart-1)\",\n                            strokeWidth: 1,\n                        }),\n                        dot(data, {\n                            id: \"density-sample-dots\",\n                            x: \"length\",\n                            y: \"depth\",\n                            r: 2.5,\n                            fill: \"var(--chart-1)\",\n                            fillOpacity: 0.45,\n                            states: [\n                                {\n                                    when: { focus: \"primary\" },\n                                    style: {\n                                        r: 6,\n                                        fill: \"var(--chart-1)\",\n                                        fillOpacity: 1,\n                                        stroke: \"var(--background)\",\n                                        strokeWidth: 1.5,\n                                    },\n                                },\n                            ],\n                        }),\n                    ],\n                    scales: {\n                        x: {\n                            scale: scaleLinear().domain([18, 72]),\n                            axis: {\n                                line: false,\n                                label: \"Culmen Length (mm)\",\n                                ticks: { size: 0 },\n                            },\n                        },\n                        y: {\n                            scale: scaleLinear().domain([5, 29]),\n                            axis: {\n                                line: false,\n                                label: \"Culmen Depth (mm)\",\n                                ticks: { size: 0 },\n                            },\n                        },\n                    },\n                    guides: false,\n                    margin: { top: 20, right: 30, bottom: 44, left: 48 },\n                    theme: {\n                        foreground: \"var(--muted-foreground)\",\n                        grid: \"transparent\",\n                        background: \"transparent\",\n                    },\n                },\n                {\n                    focus: \"nearest\",\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 pointer-events-none transition-all duration-75\",\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>Bivariate Density Contours</CardTitle>\n                <CardDescription>\n                    Kernel density estimation contours displaying 2D spatial clustering concentration\n                </CardDescription>\n            </CardHeader>\n            <CardContent>\n                <div className=\"h-[250px] w-full overflow-visible\">\n                    <Chart\n                        definition={definition}\n                        renderer={motionRenderer}\n                        height={250}\n                        ariaLabel=\"Bivariate Density Contours 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                    Innermost contour ring represents maximum bivariate sample density{\" \"}\n                    <CircleDot className=\"h-4 w-4 text-indigo-500\" />\n                </div>\n                <div className=\"text-muted-foreground leading-none\">\n                    Hover over sample observation points to inspect 2D coordinates and spatial clustering\n                </div>\n            </CardFooter>\n        </Card>\n    );\n}\n\nexport default DensityContours;\n"
    }
  ],
  "mcp": {
    "description": "Renders responsive data visualization with dark mode styling and customizable tooltips. Kernel density estimation contours displaying 2D spatial clustering concentration. Relies on card. Use when presenting data insights or analytics dashboards.",
    "tags": [
      "chart",
      "tanstack"
    ]
  }
}