{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "dot-plot-card",
  "title": "Dot Plot Card",
  "description": "A metric card with a big value, a dot-plot distribution with its peak called out, and the change vs the previous period.",
  "registryDependencies": [
    "card",
    "tooltip"
  ],
  "files": [
    {
      "path": "registry/dashboardcn/blocks/dot-plot-card.tsx",
      "content": "\"use client\"\n\nimport * as React from \"react\"\n\nimport { cn } from \"@/lib/utils\"\nimport { type NumberFormat } from \"@/registry/dashboardcn/lib/format\"\nimport { Card, CardAction, CardContent, CardHeader, CardTitle } from \"@/components/ui/card\"\nimport { DotPlot } from \"@/registry/dashboardcn/ui/dot-plot\"\nimport { MetricValue } from \"@/registry/dashboardcn/ui/metric-value\"\n\nexport interface DotPlotCardProps extends Omit<React.ComponentProps<typeof Card>, \"children\"> {\n  title: string\n  /** Headline value. Numbers are formatted with `format`; strings render as-is. */\n  value: number | string\n  format?: NumberFormat\n  /** ISO 4217 code, used when `format` is \"currency\". */\n  currency?: string\n  /** One value per column of the dot plot. */\n  data: number[]\n  /** Label per column. The peak's label is shown in the pill, e.g. \"Wed\". */\n  labels?: string[]\n  /** Text before the peak's label in the pill, e.g. \"Peak\" for \"Peak: Wed\". */\n  peakLabel?: string\n  /** Absolute change vs the previous period, in the same unit as `value`, e.g. 34002 for +34,002. */\n  delta?: number\n  /** Context for the delta, e.g. \"vs last period\". */\n  deltaLabel?: string\n  /** Format for the delta. Defaults to `format`, except a compact value gets a full number so \"+34,002\" is not lost to \"+34K\". */\n  deltaFormat?: NumberFormat\n  /** Any CSS color for the dots. Defaults to chart-1. */\n  color?: string\n  /** Number of dots in the tallest column. */\n  rows?: number\n  /** Formats a column's value in the pill while it is hovered. Defaults to a plain grouped number. */\n  valueFormatter?: (value: number) => string\n  /** Slot in the top-right corner, e.g. a menu button. */\n  action?: React.ReactNode\n}\n\nconst signedFormatters = new Map<string, Intl.NumberFormat>()\n\n/** \"+34,002\", \"-1.2K\", or \"+$390\" depending on the format. */\nfunction formatSigned(value: number, format: NumberFormat, currency: string) {\n  const key = `${format}:${currency}`\n  let formatter = signedFormatters.get(key)\n  if (!formatter) {\n    formatter = new Intl.NumberFormat(\"en-US\", {\n      ...(format === \"currency\" ? { style: \"currency\", currency } : {}),\n      ...(format === \"percent\" ? { style: \"percent\" } : {}),\n      ...(format === \"compact\" ? { notation: \"compact\" } : {}),\n      signDisplay: \"exceptZero\",\n      maximumFractionDigits: format === \"number\" ? 0 : 1,\n    })\n    signedFormatters.set(key, formatter)\n  }\n  return formatter.format(value)\n}\n\n/** A metric card with a big value, a dot-plot distribution with its peak called out, and the change vs the previous period. */\nfunction DotPlotCard({\n  title,\n  value,\n  format = \"number\",\n  currency = \"USD\",\n  data,\n  labels,\n  peakLabel = \"Peak\",\n  delta,\n  deltaLabel = \"vs last period\",\n  deltaFormat = format === \"compact\" ? \"number\" : format,\n  color = \"var(--chart-1)\",\n  rows = 6,\n  valueFormatter = (columnValue) => columnValue.toLocaleString(\"en-US\"),\n  action,\n  className,\n  ...props\n}: DotPlotCardProps) {\n  const [active, setActive] = React.useState<number | null>(null)\n  const peak = labels?.[data.indexOf(Math.max(...data))]\n  // The pill doubles as the hover readout: it shows the peak by default and the hovered column's value while hovering.\n  const pill =\n    active !== null ? (\n      <>\n        {labels?.[active] !== undefined ? `${labels[active]}: ` : null}\n        <span className=\"text-foreground font-medium\">{valueFormatter(data[active] ?? 0)}</span>\n      </>\n    ) : peak !== undefined ? (\n      <>\n        {peakLabel}: <span className=\"text-foreground font-medium\">{peak}</span>\n      </>\n    ) : null\n  return (\n    <Card data-slot=\"dot-plot-card\" className={cn(\"gap-4 py-5\", className)} {...props}>\n      <CardHeader className=\"px-5\">\n        <CardTitle className=\"text-base\">{title}</CardTitle>\n        {action ? <CardAction>{action}</CardAction> : null}\n      </CardHeader>\n      <CardContent className=\"@container px-5\">\n        {/*\n          Narrow: pill, then value and delta side by side, then the plot on its own row.\n          From 24rem: value | plot | delta on one row with the pill centered above the plot.\n        */}\n        <div className=\"grid grid-cols-[minmax(0,1fr)_auto] items-center gap-x-4 gap-y-3 @sm:grid-cols-[auto_minmax(0,1fr)_auto]\">\n          {pill !== null ? (\n            <span className=\"bg-muted text-muted-foreground col-span-2 justify-self-center rounded-full px-3 py-1.5 text-xs whitespace-nowrap @sm:col-span-1 @sm:col-start-2\">\n              {pill}\n            </span>\n          ) : null}\n          <MetricValue\n            value={value}\n            format={format}\n            currency={currency}\n            className=\"col-start-1 row-start-2 text-4xl font-semibold tracking-tight\"\n          />\n          <DotPlot\n            data={data}\n            labels={labels}\n            rows={rows}\n            color={color}\n            showTooltip={false}\n            onActiveIndexChange={setActive}\n            className=\"col-span-2 row-start-3 @sm:col-span-1 @sm:col-start-2 @sm:row-start-2\"\n          />\n          {delta !== undefined ? (\n            <div className=\"col-start-2 row-start-2 flex flex-col items-end @sm:col-start-3\">\n              <span className=\"text-muted-foreground text-xs\">{deltaLabel}</span>\n              <span className=\"text-xl font-semibold tabular-nums tracking-tight\">\n                {formatSigned(delta, deltaFormat, currency)}\n              </span>\n            </div>\n          ) : null}\n        </div>\n      </CardContent>\n    </Card>\n  )\n}\n\nexport { DotPlotCard }\n",
      "type": "registry:component"
    },
    {
      "path": "registry/dashboardcn/ui/dot-plot.tsx",
      "content": "\"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 in the tooltip and used for 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  /** 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\")\n\n/**\n * A distribution drawn as columns of stacked dots, with the peak columns at full strength.\n * Hovering a column lights it up, fades the others, and shows its value.\n */\nfunction DotPlot({\n  data,\n  labels,\n  max,\n  rows = 6,\n  color = \"var(--chart-1)\",\n  emphasis = 0.5,\n  fadedOpacity = 0.35,\n  showTooltip = true,\n  valueFormatter = defaultFormatter,\n  activeIndex,\n  onActiveIndexChange,\n  className,\n  ...props\n}: DotPlotProps) {\n  const [internal, setInternal] = React.useState<number | null>(null)\n  const active = activeIndex === undefined ? internal : activeIndex\n  const setActive = (index: number | null) => {\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\n  return (\n    <div\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      {...props}\n    >\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 = 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 label = labels?.[index]\n        return (\n          <div\n            key={index}\n            data-slot=\"dot-plot-column\"\n            data-emphasis={strong}\n            data-active={active === index}\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=\"relative flex min-w-0 flex-1 cursor-default flex-col-reverse justify-start gap-(--dot-gap) px-[calc(var(--dot-gap)/2)] max-w-[calc(var(--dot-size)+var(--dot-gap))]\"\n          >\n            {Array.from({ length: count }, (_, dot) => (\n              <span\n                key={dot}\n                className=\"aspect-square w-full rounded-full transition-opacity\"\n                style={{ backgroundColor: color, opacity: lit ? 1 : fadedOpacity }}\n              />\n            ))}\n            {showTooltip && active === index ? (\n              <span\n                role=\"tooltip\"\n                className=\"bg-popover text-popover-foreground pointer-events-none absolute bottom-full left-1/2 z-10 mb-1.5 flex -translate-x-1/2 items-baseline gap-1.5 rounded-md border px-2 py-1 text-xs whitespace-nowrap shadow-md\"\n              >\n                {label !== undefined ? (\n                  <span className=\"text-muted-foreground\">{label}</span>\n                ) : null}\n                <span className=\"font-medium tabular-nums\">{valueFormatter(value)}</span>\n              </span>\n            ) : null}\n          </div>\n        )\n      })}\n    </div>\n  )\n}\n\nexport { DotPlot }\n",
      "type": "registry:ui"
    },
    {
      "path": "registry/dashboardcn/ui/metric-value.tsx",
      "content": "\"use client\"\n\nimport * as React from \"react\"\n\nimport { cn } from \"@/lib/utils\"\nimport { formatNumber, type NumberFormat } from \"@/registry/dashboardcn/lib/format\"\nimport {\n  Tooltip,\n  TooltipContent,\n  TooltipProvider,\n  TooltipTrigger,\n} from \"@/components/ui/tooltip\"\n\n/** Values at or above this are abbreviated by default. */\nexport const DEFAULT_COMPACT_FROM = 100_000\n\nexport interface MetricValueProps extends Omit<React.ComponentProps<\"span\">, \"children\"> {\n  /** Strings render as-is. */\n  value: number | string\n  format?: NumberFormat\n  currency?: string\n  maximumFractionDigits?: number\n  /** Values at or above this are abbreviated (e.g. $158K) with the full value in a tooltip. Set to Infinity to always show the full value. */\n  compactFrom?: number\n  /** Text after the number, e.g. a unit. */\n  suffix?: React.ReactNode\n}\n\n/** A formatted number. Large values are abbreviated and reveal the full value on hover. */\nfunction MetricValue({\n  value,\n  format,\n  currency,\n  maximumFractionDigits,\n  compactFrom = DEFAULT_COMPACT_FROM,\n  suffix,\n  className,\n  ...props\n}: MetricValueProps) {\n  const classes = cn(\"tabular-nums\", className)\n\n  if (typeof value !== \"number\") {\n    return (\n      <span data-slot=\"metric-value\" className={classes} {...props}>\n        {value}\n        {suffix}\n      </span>\n    )\n  }\n\n  const full = formatNumber(value, { format, currency, maximumFractionDigits })\n  const abbreviate = format !== \"percent\" && Math.abs(value) >= compactFrom\n  if (!abbreviate) {\n    return (\n      <span data-slot=\"metric-value\" className={classes} {...props}>\n        {full}\n        {suffix}\n      </span>\n    )\n  }\n\n  return (\n    <TooltipProvider>\n      <Tooltip>\n        <TooltipTrigger asChild>\n          <span\n            data-slot=\"metric-value\"\n            data-compact=\"\"\n            className={cn(classes, \"cursor-default\")}\n            {...props}\n          >\n            {formatNumber(value, { format, currency, compact: true })}\n            {suffix}\n          </span>\n        </TooltipTrigger>\n        <TooltipContent className=\"tabular-nums\">\n          {full}\n          {suffix}\n        </TooltipContent>\n      </Tooltip>\n    </TooltipProvider>\n  )\n}\n\nexport { MetricValue }\n",
      "type": "registry:ui"
    },
    {
      "path": "registry/dashboardcn/lib/format.ts",
      "content": "export type NumberFormat = \"number\" | \"compact\" | \"currency\" | \"percent\"\n\nexport interface FormatNumberOptions {\n  format?: NumberFormat\n  /** ISO 4217 code, used when format is \"currency\". Defaults to USD. */\n  currency?: string\n  locale?: string\n  maximumFractionDigits?: number\n  /** Abbreviate large values, e.g. \"$158K\" instead of \"$158,143\". */\n  compact?: boolean\n}\n\nconst formatterCache = new Map<string, Intl.NumberFormat>()\n\nfunction getFormatter(locale: string, options: Intl.NumberFormatOptions) {\n  const key = locale + JSON.stringify(options)\n  let formatter = formatterCache.get(key)\n  if (!formatter) {\n    formatter = new Intl.NumberFormat(locale, options)\n    formatterCache.set(key, formatter)\n  }\n  return formatter\n}\n\n/**\n * Format a metric value for display.\n *\n * formatNumber(1234567)                          -> \"1,234,567\"\n * formatNumber(1234567, { format: \"compact\" })   -> \"1.2M\"\n * formatNumber(48.2, { format: \"currency\" })     -> \"$48.20\"\n * formatNumber(158143, { format: \"currency\", compact: true }) -> \"$158K\"\n * formatNumber(0.124, { format: \"percent\" })     -> \"12.4%\"\n */\nexport function formatNumber(\n  value: number,\n  {\n    format = \"number\",\n    currency = \"USD\",\n    locale = \"en-US\",\n    maximumFractionDigits,\n    compact = false,\n  }: FormatNumberOptions = {}\n): string {\n  if (!Number.isFinite(value)) return \"—\"\n\n  // Compact values keep three significant digits: $158K, $1.23M, 41.2K.\n  const compactOptions: Intl.NumberFormatOptions = compact\n    ? maximumFractionDigits === undefined\n      ? { notation: \"compact\", maximumSignificantDigits: 3 }\n      : { notation: \"compact\", maximumFractionDigits }\n    : {}\n  switch (format) {\n    case \"compact\":\n      return getFormatter(locale, {\n        notation: \"compact\",\n        maximumFractionDigits: maximumFractionDigits ?? 1,\n      }).format(value)\n    case \"currency\":\n      return getFormatter(locale, {\n        style: \"currency\",\n        currency,\n        maximumFractionDigits: maximumFractionDigits ?? 2,\n        ...compactOptions,\n      }).format(value)\n    case \"percent\":\n      return getFormatter(locale, {\n        style: \"percent\",\n        maximumFractionDigits: maximumFractionDigits ?? 1,\n      }).format(value)\n    default:\n      return getFormatter(locale, {\n        maximumFractionDigits: maximumFractionDigits ?? 0,\n        ...compactOptions,\n      }).format(value)\n  }\n}\n\n/**\n * Format a fractional change as a signed percentage.\n *\n * formatDelta(0.124)  -> \"+12.4%\"\n * formatDelta(-0.03)  -> \"-3.0%\"\n * formatDelta(0)      -> \"0.0%\"\n */\nexport function formatDelta(delta: number, locale = \"en-US\"): string {\n  if (!Number.isFinite(delta)) return \"—\"\n  return getFormatter(locale, {\n    style: \"percent\",\n    signDisplay: \"exceptZero\",\n    maximumFractionDigits: 1,\n    minimumFractionDigits: 1,\n  }).format(delta)\n}\n\n/**\n * Fractional change between two values. Returns 0 when there is no baseline.\n */\nexport function computeDelta(current: number, previous: number): number {\n  if (!previous) return 0\n  return (current - previous) / Math.abs(previous)\n}\n",
      "type": "registry:lib"
    }
  ],
  "categories": [
    "dashboard",
    "charts"
  ],
  "type": "registry:block"
}