{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "funnel-chart",
  "type": "registry:component",
  "title": "Funnel Chart",
  "description": "The steps of a process that people (or things) drop out of, as centred bars that narrow with the count, each with its count, its share of the first step and its share of the step before.",
  "categories": [
    "viz"
  ],
  "dependencies": [],
  "registryDependencies": [
    "@rivier-ui/theme",
    "@rivier-ui/chart",
    "@rivier-ui/cn"
  ],
  "files": [
    {
      "path": "registry/viz/funnel-chart.tsx",
      "type": "registry:component",
      "target": "components/viz/funnel-chart.tsx",
      "content": "\"use client\";\n/**\n * @name funnel-chart\n * @category viz\n * @description The steps of a process that people (or things) drop out of, as centred bars that narrow with the count, each with its count, its share of the first step and its share of the step before.\n * @when Showing where a sequence loses the most: sign-up steps, a sales pipeline, a hiring process. For flows that split and join use sankey-chart; for amounts that are not a sequence use bar-chart horizontal.\n * @example\n * <FunnelChart\n *   label=\"Sign-up steps\"\n *   data={[\n *     { step: \"Visited\", people: 1200 },\n *     { step: \"Started\", people: 640 },\n *     { step: \"Confirmed email\", people: 410 },\n *     { step: \"Finished\", people: 290 },\n *   ]}\n *   x=\"step\"\n *   value=\"people\"\n * />\n */\nimport { useMemo } from \"react\";\nimport { cn } from \"@/lib/cn\";\nimport { ChartContainer, ChartEmpty, chartSeries, ChartSvg, ChartTooltip, fitText, formatNumber, readNumber, useChartCursor, widestText, type ChartBaseProps } from \"@/components/viz/chart\";\n\nexport type FunnelChartLabels = { step: string; value: string; ofFirst: string; ofPrevious: string };\n\nexport type FunnelChartProps<T extends object> = Omit<ChartBaseProps, \"aspect\"> & {\n  /** One object per step, in order. */\n  data: readonly T[];\n  x: keyof T & string;\n  value: keyof T & string;\n  /** Height of a step's row in pixels. Default 44. */\n  rowHeight?: number;\n  labels?: Partial<FunnelChartLabels>;\n};\n\nconst LABELS: FunnelChartLabels = { step: \"Step\", value: \"Count\", ofFirst: \"of the first step\", ofPrevious: \"of the step before\" };\n\nexport function FunnelChart<T extends object>({ data, x, value, rowHeight = 44, labels: given, label, height, emptyLabel, locale, formatValue, className, ...props }: FunnelChartProps<T>) {\n  const labels = { ...LABELS, ...given };\n  const steps = useMemo(\n    () =>\n      data.flatMap((row) => {\n        const amount = readNumber(row, value);\n        return amount === null ? [] : [{ name: String((row as Record<string, unknown>)[x] ?? \"\"), amount: Math.max(0, amount) }];\n      }),\n    [data, x, value],\n  );\n  const cursor = useChartCursor(steps.length);\n  const number = formatValue ?? ((amount: number) => formatNumber(amount, locale));\n  const percent = (share: number): string => formatNumber(share, locale, { style: \"percent\", maximumFractionDigits: 1 });\n  const first = steps[0]?.amount ?? 0;\n  const shares = steps.map((step, i) => ({ ofFirst: first > 0 ? step.amount / first : 0, ofPrevious: i === 0 ? 1 : (steps[i - 1]?.amount ?? 0) > 0 ? step.amount / (steps[i - 1]?.amount ?? 1) : 0 }));\n  const table = {\n    caption: label,\n    columns: [labels.step, labels.value, labels.ofFirst, labels.ofPrevious],\n    rows: steps.map((step, i) => [step.name, number(step.amount), percent(shares[i]?.ofFirst ?? 0), i === 0 ? \"\" : percent(shares[i]?.ofPrevious ?? 0)]),\n  };\n  const current = cursor.active === null ? undefined : steps[cursor.active];\n  const rowsOf = (i: number): { label: string; value: string }[] => [\n    { label: labels.value, value: number(steps[i]?.amount ?? 0) },\n    { label: labels.ofFirst, value: percent(shares[i]?.ofFirst ?? 0) },\n    ...(i > 0 ? [{ label: labels.ofPrevious, value: percent(shares[i]?.ofPrevious ?? 0) }] : []),\n  ];\n\n  return (\n    <div data-slot=\"funnel-chart\" className={cn(\"flex w-full min-w-0 flex-col gap-3\", className)} {...props}>\n      <ChartContainer\n        height={height ?? Math.max(80, steps.length * rowHeight)}\n        table={table}\n        announcement={\n          cursor.keyboard && current && cursor.active !== null\n            ? `${current.name}: ${rowsOf(cursor.active)\n                .map((row) => `${row.value} ${row.label}`)\n                .join(\", \")}`\n            : \"\"\n        }\n      >\n        {({ width, height: plotHeight }) => {\n          if (steps.length === 0 || first <= 0) return <ChartEmpty>{emptyLabel}</ChartEmpty>;\n          const row = plotHeight / steps.length;\n          const nameRoom = Math.min(width * 0.32, widestText(steps.map((s) => s.name)) + 12);\n          const valueRoom = Math.min(width * 0.26, widestText(steps.map((s, i) => `${number(s.amount)} · ${percent(shares[i]?.ofFirst ?? 0)}`)) + 12);\n          const barRoom = Math.max(20, width - nameRoom - valueRoom);\n          const centre = nameRoom + barRoom / 2;\n          const bar = Math.min(28, row - 10);\n          return (\n            <>\n              <ChartSvg label={label} width={width} height={plotHeight} interactive {...cursor.svgProps}>\n                <g aria-hidden=\"true\">\n                  {steps.map((step, i) => {\n                    const w = Math.max(2, (step.amount / first) * barRoom);\n                    const y = i * row + (row - bar) / 2;\n                    const next = steps[i + 1];\n                    return (\n                      <g\n                        key={i}\n                        onPointerEnter={() => cursor.point(i)}\n                        className={cn(\"transition-opacity duration-instant motion-reduce:transition-none\", cursor.active !== null && cursor.active !== i && \"opacity-45\")}\n                      >\n                        <rect x={0} y={i * row} width={width} height={row} fill=\"transparent\" />\n                        {next ? (\n                          <polygon\n                            points={`${centre - w / 2},${y + bar} ${centre + w / 2},${y + bar} ${centre + Math.max(2, (next.amount / first) * barRoom) / 2},${y + row} ${centre - Math.max(2, (next.amount / first) * barRoom) / 2},${y + row}`}\n                            className={chartSeries(0).fill}\n                            fillOpacity={0.15}\n                          />\n                        ) : null}\n                        <rect x={centre - w / 2} y={y} width={w} height={bar} className={cn(chartSeries(0).fill, \"[rx:min(var(--radius),4px)]\")} />\n                        <text x={0} y={y + bar / 2} dy=\"0.32em\" className=\"fill-foreground text-xs\">\n                          {fitText(step.name, nameRoom - 8)}\n                        </text>\n                        <text x={width} y={y + bar / 2} dy=\"0.32em\" textAnchor=\"end\" className=\"fill-muted-foreground text-xs tabular-nums\">\n                          <tspan className=\"fill-foreground font-medium\">{number(step.amount)}</tspan> · {percent(shares[i]?.ofFirst ?? 0)}\n                        </text>\n                      </g>\n                    );\n                  })}\n                  {current && cursor.active !== null ? (\n                    <rect x={1} y={cursor.active * row + 1} width={width - 2} height={row - 2} fill=\"none\" className=\"stroke-foreground [rx:min(var(--radius),4px)]\" strokeWidth={2} />\n                  ) : null}\n                </g>\n              </ChartSvg>\n              {current && cursor.active !== null ? <ChartTooltip x={centre} y={cursor.active * row + row / 2} side=\"right\" title={current.name} rows={rowsOf(cursor.active)} /> : null}\n            </>\n          );\n        }}\n      </ChartContainer>\n    </div>\n  );\n}\n"
    }
  ],
  "meta": {
    "category": "viz",
    "when": "Showing where a sequence loses the most: sign-up steps, a sales pipeline, a hiring process. For flows that split and join use sankey-chart; for amounts that are not a sequence use bar-chart horizontal.",
    "example": "<FunnelChart\n  label=\"Sign-up steps\"\n  data={[\n    { step: \"Visited\", people: 1200 },\n    { step: \"Started\", people: 640 },\n    { step: \"Confirmed email\", people: 410 },\n    { step: \"Finished\", people: 290 },\n  ]}\n  x=\"step\"\n  value=\"people\"\n/>",
    "client": true
  }
}
