{
  "title": "Boxplot Chart",
  "description": "Grouped quartile boxplots highlighting median and outlier spreads.",
  "name": "tanstack-charts-boxplot",
  "dependencies": [
    "@tanstack/charts",
    "@tanstack/react-charts",
    "d3-scale"
  ],
  "registryDependencies": [
    "card"
  ],
  "type": "registry:block",
  "files": [
    {
      "path": "components/blocks/tanstack-charts/bar/boxplot.tsx",
      "type": "registry:block",
      "content": "import { type ChartPoint, boxY, defineChart } 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 { BarChart3 } from \"lucide-react\";\n\nimport { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from \"@/components/ui/card\";\n\ninterface BoxplotDatum {\n    Expt: number;\n    Speed: number;\n    Run: number;\n}\n\nconst motionRenderer = motion({\n    initial: \"always\",\n    transition: { type: \"spring\", stiffness: 170, damping: 18, mass: 1 },\n});\n\nfunction generateDemoData(): BoxplotDatum[] {\n    const results: BoxplotDatum[] = [];\n    const baseSpeeds = [850, 920, 880, 810, 890];\n    for (let expt = 1; expt <= 5; expt++) {\n        const base = baseSpeeds[expt - 1];\n        for (let run = 1; run <= 12; run++) {\n            const spread = Math.sin(run * 1.3 + expt) * 60 + Math.cos(run * 0.7) * 30;\n            results.push({ Expt: expt, Speed: Math.round(base + spread), Run: run });\n        }\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 any;\n    if (!datum) return { title: \"\", rows: [] };\n    if (datum.kind === \"summary\") {\n        return {\n            title: `Experiment ${datum.category}`,\n            rows: [\n                { label: \"Median\", value: `${datum.median.toFixed(1)} km/s`, color: \"var(--chart-1)\" },\n                {\n                    label: \"IQR (Q1 - Q3)\",\n                    value: `${datum.q1.toFixed(1)} – ${datum.q3.toFixed(1)}`,\n                    color: \"var(--foreground)\",\n                },\n            ],\n        };\n    }\n    return {\n        title: `Experiment ${datum.category} Outlier`,\n        rows: [{ label: \"Speed\", value: `${datum.value.toFixed(1)} km/s`, color: \"var(--chart-2)\" }],\n    };\n}\n\nexport function BoxplotChart() {\n    const data = useMemo(() => generateDemoData(), []);\n\n    const definition = useMemo(\n        () =>\n            defineChart(\n                {\n                    renderer: motionRenderer,\n                    marks: [\n                        boxY(data, {\n                            id: \"experiment-boxplot\",\n                            x: \"Expt\",\n                            y: \"Speed\",\n                            key: (row: BoxplotDatum) => `${row.Expt}:${row.Run}`,\n                            fill: \"var(--chart-1)\",\n                            stroke: \"var(--chart-1)\",\n                            inset: 12,\n                            r: 3,\n                        }),\n                    ],\n                    scales: {\n                        x: {\n                            scale: () => scaleBand<number>().padding(0.25),\n                            axis: {\n                                line: false,\n                                label: \"Experiment Trial\",\n                                ticks: { size: 0, padding: 10 },\n                            },\n                        },\n                        y: {\n                            scale: scaleLinear().domain([700, 1050]),\n                            grid: true,\n                            axis: {\n                                line: false,\n                                label: \"Speed Variance\",\n                                ticks: { size: 0 },\n                            },\n                        },\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>Experiment Speed Distribution</CardTitle>\n                <CardDescription>Grouped quartile boxplots highlighting median and outlier spreads</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=\"Experiment Speed Distribution 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                    Trial 2 demonstrated tightest interquartile stability{\" \"}\n                    <BarChart3 className=\"h-4 w-4 text-indigo-500\" />\n                </div>\n                <div className=\"text-muted-foreground leading-none\">\n                    Boxes enclose the middle 50% of readings; whiskers extend to non-outlier limits\n                </div>\n            </CardFooter>\n        </Card>\n    );\n}\n\nexport default BoxplotChart;\n"
    }
  ],
  "mcp": {
    "description": "Renders responsive data visualization with dark mode styling and customizable tooltips. Grouped quartile boxplots highlighting median and outlier spreads. Relies on card. Use when presenting data insights or analytics dashboards.",
    "tags": [
      "chart",
      "tanstack"
    ]
  }
}