{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "dot-plot",
  "type": "registry:component",
  "title": "Dot comparison",
  "description": "Compare observations using position rather than bar area.",
  "categories": [
    "charts"
  ],
  "dependencies": [],
  "registryDependencies": [],
  "files": [
    {
      "path": "src/registry/overtrue/dot-plot.tsx",
      "type": "registry:component",
      "target": "components/overtrue/dot-plot.tsx",
      "content": "/** Adapted from dashboardcn (MIT), copyright 2026 dashboardcn contributors.\n * Source: https://github.com/NoahGdev/dashboardcn/tree/b1ffacbca0f7a0be60135c8665b9e8c267205c48\n *\n * MIT License\n *\n * Copyright (c) 2026 dashboardcn contributors\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n *\n * ---\n *\n * Portions of this project are derived from shadcn/ui and shadcn/registry-template\n * (https://github.com/shadcn-ui/ui), Copyright (c) 2023 shadcn, licensed under the\n * MIT License. The notice above applies to those portions as well.\n */\n\"use client\";\n\nimport * as React from \"react\";\n\nimport { cn } from \"@/lib/utils\";\n\nexport interface DotPlotProps extends React.ComponentProps<\"div\"> {\n  /** One value per column, in order. */\n  data: number[];\n  /** Optional label per column, shown under the column, in the tooltip, and in the accessible name. */\n  labels?: string[];\n  /** The value that fills a column. Defaults to the largest value in `data`. */\n  max?: number;\n  /** Number of dots in a full column. */\n  rows?: number;\n  /** Any CSS color. Defaults to chart-1. */\n  color?: string;\n  /**\n   * Columns at or above this fraction of `max` are drawn at full strength and the\n   * rest are faded, so the peak stands out. Set to 0 to draw every column at full strength.\n   */\n  emphasis?: number;\n  /** Opacity of the faded columns. */\n  fadedOpacity?: number;\n  /** Show a tooltip with the column's label and value while it is hovered. */\n  showTooltip?: boolean;\n  /** Show `labels` under the columns. */\n  showLabels?: boolean;\n  /** Show every nth label. Defaults to whatever keeps about 12 labels. */\n  labelEvery?: number;\n  /** Draw the empty slots of each column as faint dots, so every column reads against the same scale. */\n  showTrack?: boolean;\n  /** Formats the value in the tooltip. Defaults to a plain grouped number. */\n  valueFormatter?: (value: number) => string;\n  /** Index of the hovered column, to control it from outside. */\n  activeIndex?: number | null;\n  /** Called when the hovered column changes, with null when the pointer leaves. */\n  onActiveIndexChange?: (index: number | null) => void;\n}\n\nconst defaultFormatter = (value: number) => value.toLocaleString(\"en-US\");\nconst TRACK = \"color-mix(in oklab, var(--color-foreground) 7%, transparent)\";\n\nfunction useReducedMotion() {\n  return React.useSyncExternalStore(\n    (onChange) => {\n      const query = window.matchMedia(\"(prefers-reduced-motion: reduce)\");\n      query.addEventListener(\"change\", onChange);\n      return () => query.removeEventListener(\"change\", onChange);\n    },\n    () => window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches,\n    () => false,\n  );\n}\n\n/**\n * A distribution drawn as columns of stacked dots against a faint track, with the\n * peak columns at full strength. Hovering a column lights it up, fades the others,\n * and shows its value.\n */\nfunction DotPlot({\n  data,\n  labels,\n  max,\n  rows = 6,\n  color = \"var(--color-chart-1)\",\n  emphasis = 0.5,\n  fadedOpacity = 0.35,\n  showTooltip = true,\n  showLabels = true,\n  labelEvery,\n  showTrack = true,\n  valueFormatter = defaultFormatter,\n  activeIndex,\n  onActiveIndexChange,\n  className,\n  style,\n  ref,\n  ...props\n}: DotPlotProps) {\n  const reduced = useReducedMotion();\n  const rootRef = React.useRef<HTMLDivElement | null>(null);\n  const [width, setWidth] = React.useState(0);\n  const [internal, setInternal] = React.useState<number | null>(null);\n  const active = activeIndex === undefined ? internal : activeIndex;\n  const setActive = (index: number | null) => {\n    if (index === active) return;\n    setInternal(index);\n    onActiveIndexChange?.(index);\n  };\n\n  const top = max ?? Math.max(0, ...data);\n  const peak = data.indexOf(Math.max(...data));\n  const peakLabel = labels?.[peak];\n  const withLabels = showLabels && !!labels?.length;\n\n  // Thin the labels out when the columns get too narrow to hold them side by side.\n  React.useLayoutEffect(() => {\n    const node = rootRef.current;\n    if (!node || !withLabels) return;\n    const observer = new ResizeObserver(([entry]) =>\n      setWidth(entry?.contentRect.width ?? 0),\n    );\n    observer.observe(node);\n    return () => observer.disconnect();\n  }, [withLabels]);\n  const longest =\n    labels?.reduce((m, label) => Math.max(m, label.length), 0) ?? 0;\n  const columnWidth = width\n    ? Math.min(width / Math.max(1, data.length), 36)\n    : Infinity;\n  const fit = Math.max(1, Math.ceil((longest * 6.4 + 6) / columnWidth));\n  const every = labelEvery ?? Math.max(Math.ceil(data.length / 12), fit);\n\n  return (\n    <div\n      ref={(node) => {\n        rootRef.current = node;\n        if (typeof ref === \"function\") ref(node);\n        else if (ref) ref.current = node;\n      }}\n      data-slot=\"dot-plot\"\n      role=\"img\"\n      aria-label={\n        peakLabel !== undefined\n          ? `Distribution across ${data.length} points, peaking at ${peakLabel}`\n          : `Distribution across ${data.length} points`\n      }\n      onPointerLeave={() => setActive(null)}\n      className={cn(\n        // Dot size and gap are variables so they can be tuned from className, e.g. \"[--dot-size:0.75rem]\".\n        \"flex w-full justify-center [--dot-gap:0.25rem] [--dot-size:0.625rem]\",\n        className,\n      )}\n      style={style}\n      {...props}\n    >\n      <style href=\"overtrue-dot-plot\" precedence=\"default\">\n        {`@keyframes dot-plot-rise{from{opacity:0;transform:translateY(6px) scale(.5)}}`}\n      </style>\n      {data.map((value, index) => {\n        const fraction = Math.min(1, Math.max(0, value / (top || 1)));\n        // Any non-zero value shows at least one dot so small values still register.\n        const count =\n          fraction === 0 ? 0 : Math.max(1, Math.round(fraction * rows));\n        const strong = emphasis <= 0 || fraction >= emphasis;\n        // While a column is hovered it alone is lit; otherwise the emphasized columns are.\n        const lit = active === null ? strong : active === index;\n        const isActive = active === index;\n        const label = labels?.[index];\n        const slots = showTrack ? Math.max(rows, count) : count;\n        return (\n          <div\n            key={index}\n            data-slot=\"dot-plot-column\"\n            data-emphasis={strong}\n            data-active={isActive}\n            onPointerEnter={() => setActive(index)}\n            // Columns stretch to the full height and carry the gap as padding, so the hover target has no dead zones.\n            // They share the width equally and cap at the dot size, so dots shrink in narrow cards instead of overflowing.\n            className={cn(\n              \"relative flex min-w-0 flex-1 cursor-default flex-col items-center gap-1.5 px-[calc(var(--dot-gap)/2)]\",\n              withLabels\n                ? \"max-w-[max(calc(var(--dot-size)+var(--dot-gap)),2.25rem)]\"\n                : \"max-w-[calc(var(--dot-size)+var(--dot-gap))]\",\n            )}\n          >\n            <div className=\"flex w-full flex-1 flex-col-reverse items-center justify-start gap-(--dot-gap)\">\n              {Array.from({ length: slots }, (_, dot) => {\n                const filled = dot < count;\n                return (\n                  <span\n                    key={dot}\n                    data-filled={filled || undefined}\n                    className={cn(\n                      \"aspect-square w-full max-w-(--dot-size) shrink-0 rounded-full transition-[opacity,background-color,box-shadow] duration-150 motion-reduce:transition-none\",\n                      filled &&\n                        \"shadow-[inset_0_1px_0_rgb(255_255_255/0.22)] dark:shadow-[0_0_8px_-1px_color-mix(in_oklab,var(--dot-plot-color)_70%,transparent)]\",\n                      filled &&\n                        !reduced &&\n                        \"motion-safe:animate-[dot-plot-rise_560ms_cubic-bezier(0.22,1,0.36,1)_backwards]\",\n                    )}\n                    style={{\n                      backgroundColor: filled ? color : TRACK,\n                      opacity: filled ? (lit ? 1 : fadedOpacity) : 1,\n                      [\"--dot-plot-color\" as string]: color,\n                      animationDelay:\n                        filled && !reduced\n                          ? `${Math.round(index * Math.min(36, 420 / Math.max(1, data.length)) + dot * 28)}ms`\n                          : undefined,\n                    }}\n                  />\n                );\n              })}\n            </div>\n            {withLabels ? (\n              <span\n                aria-hidden=\"true\"\n                className={cn(\n                  \"max-w-none text-[11px] leading-none whitespace-nowrap text-muted-foreground tabular-nums transition-colors duration-150 motion-reduce:transition-none\",\n                  isActive && \"font-medium text-foreground\",\n                )}\n              >\n                {index % every === 0 || isActive ? (label ?? \"\") : \" \"}\n              </span>\n            ) : null}\n            {showTooltip && isActive ? (\n              <span\n                role=\"tooltip\"\n                data-slot=\"dot-plot-tooltip\"\n                className=\"pointer-events-none absolute bottom-full left-1/2 z-10 mb-2 flex -translate-x-1/2 items-center gap-2 rounded-xl border border-border/70 bg-popover/90 px-2.5 py-1.5 text-xs whitespace-nowrap text-popover-foreground shadow-xl backdrop-blur-md\"\n              >\n                <span\n                  aria-hidden=\"true\"\n                  className=\"size-2 shrink-0 rounded-full\"\n                  style={{ backgroundColor: color }}\n                />\n                {label !== undefined ? (\n                  <span className=\"text-muted-foreground\">{label}</span>\n                ) : null}\n                <span className=\"ml-auto pl-1 font-semibold text-foreground tabular-nums\">\n                  {valueFormatter(value)}\n                </span>\n              </span>\n            ) : null}\n          </div>\n        );\n      })}\n    </div>\n  );\n}\n\nexport { DotPlot };\n"
    }
  ],
  "docs": "Documentation: https://ui.overtrue.xyz/components/dot-plot. Requires an initialized shadcn/ui project with Tailwind CSS v4."
}
