{
  "title": "Cumulative Histogram",
  "description": "Binned cumulative frequency histogram of population metrics.",
  "name": "tanstack-charts-cumulative-histogram",
  "dependencies": [
    "@tanstack/charts",
    "@tanstack/react-charts",
    "d3-array",
    "d3-scale"
  ],
  "registryDependencies": [
    "card"
  ],
  "type": "registry:block",
  "files": [
    {
      "path": "components/blocks/tanstack-charts/bar/cumulative-histogram.tsx",
      "type": "registry:block",
      "content": "import { type ChartPoint, binX, cumulative, defineChart, rect } 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 { thresholdScott } from \"d3-array\";\nimport { scaleLinear } from \"d3-scale\";\nimport { BarChart2 } from \"lucide-react\";\n\nimport { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from \"@/components/ui/card\";\n\ninterface WeightDatum {\n    weight: number;\n}\n\nconst motionRenderer = motion({\n    initial: \"always\",\n    transition: { type: \"spring\", stiffness: 170, damping: 18, mass: 1 },\n});\n\nfunction generateDemoData(): WeightDatum[] {\n    const results: WeightDatum[] = [];\n    for (let i = 0; i < 80; i++) {\n        // Normal distribution approximation\n        const weight = 65 + (Math.sin(i) + Math.cos(i * 1.5) + Math.sin(i * 0.7)) * 12;\n        results.push({ weight });\n    }\n    return results;\n}\n\nfunction shadcnTooltipContent<TDatum>(points: readonly ChartPoint<TDatum>[]) {\n    if (points.length === 0) return { title: \"\", rows: [] };\n    const p = points[0];\n    const d = p.datum as any;\n    if (!d) return { title: \"\", rows: [] };\n    return {\n        title: `Weight Range: ${d.x1.toFixed(1)} – ${d.x2.toFixed(1)} kg`,\n        rows: [\n            {\n                label: \"Cumulative Total\",\n                value: `${d.cumulativeCount} subjects`,\n                color: \"var(--chart-1)\",\n            },\n        ],\n    };\n}\n\nexport function CumulativeHistogram() {\n    const data = useMemo(() => generateDemoData(), []);\n\n    const cumulativeBins = useMemo(() => {\n        const bins = binX(data, {\n            value: \"weight\",\n            thresholds: thresholdScott,\n            outputs: { count: { reduce: \"count\" } },\n        });\n        return cumulative(bins, {\n            orderBy: \"x1\",\n            outputs: { cumulativeCount: { value: \"count\", reduce: \"sum\" } },\n        });\n    }, [data]);\n\n    const definition = useMemo(\n        () =>\n            defineChart(\n                {\n                    renderer: motionRenderer,\n                    marks: [\n                        rect(cumulativeBins, {\n                            id: \"cumulative-rects\",\n                            x1: \"x1\",\n                            x2: \"x2\",\n                            y1: () => 0,\n                            y2: \"cumulativeCount\",\n                            fill: \"var(--chart-1)\",\n                            inset: 1,\n                        }),\n                    ],\n                    scales: {\n                        x: {\n                            scale: scaleLinear,\n                            grid: true,\n                            axis: {\n                                line: false,\n                                label: \"Weight (kg)\",\n                                ticks: { size: 0 },\n                            },\n                        },\n                        y: {\n                            scale: scaleLinear,\n                            grid: true,\n                            axis: {\n                                line: false,\n                                label: \"Cumulative Count\",\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        [cumulativeBins],\n    );\n\n    return (\n        <Card className=\"w-full max-w-xl\">\n            <CardHeader>\n                <CardTitle>Cumulative Population Distribution</CardTitle>\n                <CardDescription>Binned cumulative frequency histogram of subject body weights</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=\"Cumulative Population 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                    80% of cohort falls below 78kg weight threshold <BarChart2 className=\"h-4 w-4 text-cyan-500\" />\n                </div>\n                <div className=\"text-muted-foreground leading-none\">\n                    Bar height reflects total cumulative count up to each bin upper bound\n                </div>\n            </CardFooter>\n        </Card>\n    );\n}\n\nexport default CumulativeHistogram;\n"
    }
  ],
  "mcp": {
    "description": "Renders responsive data visualization with dark mode styling and customizable tooltips. Binned cumulative frequency histogram of population metrics. Relies on card. Use when presenting data insights or analytics dashboards.",
    "tags": [
      "chart",
      "tanstack"
    ]
  }
}