{
  "title": "Empirical Cumulative Distribution",
  "description": "Step-after empirical cumulative probability distribution curve.",
  "name": "tanstack-charts-empirical-cdf",
  "dependencies": [
    "@tanstack/charts",
    "@tanstack/react-charts",
    "d3-scale",
    "d3-shape"
  ],
  "registryDependencies": [
    "card"
  ],
  "type": "registry:block",
  "files": [
    {
      "path": "components/blocks/tanstack-charts/line/empirical-cdf.tsx",
      "type": "registry:block",
      "content": "import { type ChartPoint, d3Curve, defineChart, lineY, rank } 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 } from \"d3-scale\";\nimport { curveStepAfter } from \"d3-shape\";\nimport { LineChart } from \"lucide-react\";\n\nimport { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from \"@/components/ui/card\";\n\ninterface CdfDatum {\n    name: string;\n    mpg: number;\n    probability?: number;\n    rank?: number;\n}\n\nconst motionRenderer = motion({\n    initial: \"always\",\n    transition: { type: \"spring\", stiffness: 170, damping: 18, mass: 1 },\n});\n\nfunction generateDemoData(): CdfDatum[] {\n    const raw: CdfDatum[] = [];\n    for (let i = 0; i < 40; i++) {\n        const mpg = 12 + Math.floor(i * 0.8 + Math.random() * 3);\n        raw.push({ name: `Vehicle Model #${i + 1}`, mpg });\n    }\n    raw.sort((a, b) => a.mpg - b.mpg);\n\n    const ranked = rank(raw, { value: \"mpg\", order: \"ascending\" });\n    return ranked.map((r) => ({\n        ...r,\n        probability: r.rank / raw.length,\n    }));\n}\n\nfunction shadcnTooltipContent<TDatum>(points: readonly ChartPoint<TDatum>[]) {\n    if (points.length === 0) return { title: \"\", rows: [] };\n    const d = points[0].datum as CdfDatum;\n    if (!d) return { title: \"\", rows: [] };\n    const probPct = ((d.probability ?? 0) * 100).toFixed(1);\n    return {\n        title: d.name,\n        rows: [\n            { label: \"Fuel Economy\", value: `${d.mpg} mpg`, color: \"var(--chart-1)\" },\n            { label: \"Cumulative Proportion\", value: `${probPct}%`, color: \"var(--foreground)\" },\n        ],\n    };\n}\n\nexport function EmpiricalCdf() {\n    const data = useMemo(() => generateDemoData(), []);\n\n    const definition = useMemo(\n        () =>\n            defineChart(\n                {\n                    renderer: motionRenderer,\n                    marks: [\n                        lineY(data, {\n                            id: \"empirical-cdf\",\n                            key: \"name\",\n                            x: \"mpg\",\n                            y: \"probability\",\n                            curve: d3Curve(curveStepAfter),\n                            stroke: \"var(--chart-1)\",\n                            strokeWidth: 2,\n                        }),\n                    ],\n                    scales: {\n                        x: {\n                            scale: scaleLinear,\n                            grid: true,\n                            axis: { label: \"Fuel Economy (mpg)\" },\n                        },\n                        y: {\n                            scale: scaleLinear().domain([0, 1]),\n                            grid: true,\n                            axis: {\n                                ticks: { format: (v: number) => `${Math.round(v * 100)}%` },\n                                label: \"Cumulative Proportion\",\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                    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>Empirical Cumulative Distribution (ECDF)</CardTitle>\n                <CardDescription>\n                    Step-after cumulative probability distribution of vehicle fuel economy\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=\"Empirical Cumulative 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                    50th percentile (median) reached at 24 mpg <LineChart className=\"h-4 w-4 text-emerald-500\" />\n                </div>\n                <div className=\"text-muted-foreground leading-none\">\n                    Curve shows exact empirical percentage of observations below any given value threshold\n                </div>\n            </CardFooter>\n        </Card>\n    );\n}\n\nexport default EmpiricalCdf;\n"
    }
  ],
  "mcp": {
    "description": "Renders responsive data visualization with dark mode styling and customizable tooltips. Step-after empirical cumulative probability distribution curve. Relies on card. Use when presenting data insights or analytics dashboards.",
    "tags": [
      "chart",
      "tanstack"
    ]
  }
}