{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"line-charts-gradient-metric","type":"registry:component","title":"Line charts · Gradient metric","description":"One metric under a volt gradient with labelled peak and low markers, an average guide, a glowing latest point and a headline that follows the crosshair.","author":"GetUrUI <hello@geturui.dev>","dependencies":["motion","lucide-react"],"registryDependencies":[],"files":[{"path":"registry/geturui/examples/line-charts/gradient-metric.tsx","type":"registry:component","target":"components/examples/line-charts/gradient-metric.tsx","content":"\"use client\";\n\nimport * as React from \"react\";\nimport { AnimatePresence, motion, useInView, useReducedMotion } from \"motion/react\";\nimport { ArrowDown, ArrowUp } from \"lucide-react\";\nimport { cn } from \"@/lib/utils\";\n\nexport interface GradientMetricChartProps {\n  title?: string;\n  /** One value per label, oldest first. */\n  values?: number[];\n  labels?: string[];\n  formatValue?: (n: number) => string;\n  /** Caption under the headline value. */\n  caption?: string;\n  className?: string;\n}\n\nconst VALUES = [212, 198, 226, 241, 233, 219, 186, 204, 247, 262, 258, 241, 275, 289, 301, 284, 268, 296, 312, 334, 327, 309, 341, 358, 412, 376, 352, 369, 388, 394];\nconst LABELS = VALUES.map((_, i) => {\n  const d = new Date(Date.UTC(2026, 8, 3 + i));\n  return d.toLocaleDateString(\"en-US\", { month: \"short\", day: \"numeric\", timeZone: \"UTC\" });\n});\n\nfunction domain(min: number, max: number, count = 4) {\n  const raw = Math.max(max - min, 1) / count;\n  const mag = 10 ** Math.floor(Math.log10(raw));\n  const step = [1, 2, 2.5, 5, 10].map((m) => m * mag).find((s) => s >= raw) ?? raw;\n  const lo = Math.floor(min / step) * step;\n  const hi = Math.ceil(max / step) * step;\n  return { lo, hi, ticks: Array.from({ length: Math.round((hi - lo) / step) + 1 }, (_, i) => lo + i * step) };\n}\n\nfunction smooth(pts: [number, number][]) {\n  const f = (n: number) => n.toFixed(2);\n  return pts\n    .map((p, i) => {\n      if (i === 0) return `M${f(p[0])},${f(p[1])}`;\n      const a = pts[i - 2] ?? pts[i - 1];\n      const b = pts[i - 1];\n      const c = pts[i + 1] ?? p;\n      return `C${f(b[0] + (p[0] - a[0]) / 6)},${f(b[1] + (p[1] - a[1]) / 6)} ${f(p[0] - (c[0] - b[0]) / 6)},${f(p[1] - (c[1] - b[1]) / 6)} ${f(p[0])},${f(p[1])}`;\n    })\n    .join(\"\");\n}\n\n/** Single-metric line with a volt gradient, labelled peak and low markers, an average guide and a crosshair readout on hover or arrow keys. */\nexport function GradientMetricChart({\n  title = \"Daily signups\",\n  values = VALUES,\n  labels = LABELS,\n  formatValue = (n) => Math.round(n).toLocaleString(\"en-US\"),\n  caption = \"Last 30 days\",\n  className,\n}: GradientMetricChartProps) {\n  const reduce = useReducedMotion() ?? false;\n  const ref = React.useRef<HTMLDivElement>(null);\n  const inView = useInView(ref, { once: true, margin: \"-40px\" });\n  const uid = React.useId().replace(/[^a-zA-Z0-9_-]/g, \"\");\n  const [active, setActive] = React.useState<number | null>(null);\n\n  const n = values.length;\n  const min = Math.min(...values);\n  const max = Math.max(...values);\n  const { lo, hi, ticks } = domain(min, max);\n  const avg = values.reduce((a, b) => a + b, 0) / Math.max(n, 1);\n  const iMin = values.indexOf(min);\n  const iMax = values.indexOf(max);\n  const xp = (i: number) => (n > 1 ? (i / (n - 1)) * 100 : 50);\n  const yp = (v: number) => (1 - (v - lo) / (hi - lo || 1)) * 100;\n  const pts = values.map((v, i) => [xp(i), yp(v)] as [number, number]);\n  const line = smooth(pts);\n  const last = values[n - 1] ?? 0;\n  const change = values[0] ? (last - values[0]) / values[0] : 0;\n\n  function onMove(e: React.PointerEvent<HTMLDivElement>) {\n    const r = e.currentTarget.getBoundingClientRect();\n    setActive(Math.max(0, Math.min(n - 1, Math.round(((e.clientX - r.left) / r.width) * (n - 1)))));\n  }\n  function onKey(e: React.KeyboardEvent) {\n    if (e.key === \"ArrowLeft\" || e.key === \"ArrowRight\") {\n      e.preventDefault();\n      setActive((a) => Math.max(0, Math.min(n - 1, (a ?? n - 1) + (e.key === \"ArrowRight\" ? 1 : -1))));\n    } else if (e.key === \"Home\" || e.key === \"End\") {\n      e.preventDefault();\n      setActive(e.key === \"Home\" ? 0 : n - 1);\n    } else if (e.key === \"Escape\") setActive(null);\n  }\n\n  const marker = (i: number, kind: \"Peak\" | \"Low\") => (\n    <motion.div\n      key={kind}\n      aria-hidden\n      initial={{ opacity: 0, y: kind === \"Peak\" ? 6 : -6 }}\n      animate={inView ? { opacity: active === null ? 1 : 0.35, y: 0 } : undefined}\n      transition={{ delay: reduce ? 0 : 1.1, type: \"spring\", stiffness: 320, damping: 26 }}\n      className=\"pointer-events-none absolute\"\n      style={{ left: `${xp(i)}%`, top: `${yp(values[i] ?? 0)}%` }}\n    >\n      <span className=\"absolute size-2 -translate-x-1/2 -translate-y-1/2 rounded-full border-2 border-surface-raised bg-ink\" />\n      <span\n        className={cn(\n          \"absolute whitespace-nowrap rounded-full border border-border bg-surface-raised px-2 py-0.5 text-[11px] shadow-sm\",\n          kind === \"Peak\" ? \"bottom-2.5\" : \"top-2.5\",\n          xp(i) > 75 ? \"right-0 translate-x-3\" : xp(i) < 25 ? \"left-0 -translate-x-3\" : \"-translate-x-1/2\",\n        )}\n      >\n        <span className=\"text-ink-subtle\">{kind} </span>\n        <span className=\"font-mono font-semibold tabular-nums text-ink\">{formatValue(values[i] ?? 0)}</span>\n        <span className=\"text-ink-subtle\"> · {labels[i]}</span>\n      </span>\n    </motion.div>\n  );\n\n  return (\n    <figure className={cn(\"@container w-full rounded-xl border border-border bg-surface-raised p-4 shadow-sm sm:p-5\", className)}>\n      <figcaption className=\"flex flex-wrap items-end justify-between gap-3\">\n        <div>\n          <p className=\"text-[13px] font-medium text-ink-muted\">{title}</p>\n          <p className=\"mt-1 font-display text-[32px] font-semibold leading-none tracking-[-0.8px] tabular-nums text-ink\">{formatValue(active === null ? last : values[active] ?? 0)}</p>\n          <p className=\"mt-1.5 text-[12px] text-ink-subtle\">{active === null ? caption : labels[active]}</p>\n        </div>\n        <p className={cn(\"inline-flex items-center gap-1 rounded-full px-2 py-0.5 text-[12px] font-semibold tabular-nums\", change >= 0 ? \"bg-volt-soft text-success\" : \"bg-surface text-danger\")}>\n          {change >= 0 ? <ArrowUp aria-hidden className=\"size-3.5\" /> : <ArrowDown aria-hidden className=\"size-3.5\" />}\n          {change >= 0 ? \"Up\" : \"Down\"} {Math.abs(change * 100).toFixed(1)}%\n          <span className=\"font-normal text-ink-subtle\">over period</span>\n        </p>\n      </figcaption>\n\n      <div className=\"mt-8 grid grid-cols-[2.25rem_minmax(0,1fr)] gap-x-2\">\n        <div aria-hidden className=\"relative h-48 @md:h-56\">\n          {ticks.map((t) => (\n            <span key={t} className=\"absolute right-0 -translate-y-1/2 font-mono text-[10.5px] tabular-nums text-ink-subtle\" style={{ top: `${yp(t)}%` }}>\n              {formatValue(t)}\n            </span>\n          ))}\n        </div>\n        <div\n          ref={ref}\n          tabIndex={0}\n          role=\"group\"\n          aria-label={`${title} chart. Use left and right arrow keys to read each day.`}\n          onPointerMove={onMove}\n          onPointerLeave={() => setActive(null)}\n          onFocus={() => setActive((a) => a ?? n - 1)}\n          onBlur={() => setActive(null)}\n          onKeyDown={onKey}\n          className=\"relative h-48 touch-none rounded-xs outline-none focus-visible:shadow-[var(--focus-ring)] @md:h-56\"\n        >\n          <svg\n            role=\"img\"\n            aria-label={`${title}, ${caption}: from ${formatValue(values[0] ?? 0)} to ${formatValue(last)}. Peak ${formatValue(max)} on ${labels[iMax]}, low ${formatValue(min)} on ${labels[iMin]}, average ${formatValue(avg)}.`}\n            viewBox=\"0 0 100 100\"\n            preserveAspectRatio=\"none\"\n            className=\"absolute inset-0 size-full overflow-visible\"\n          >\n            <defs>\n              <linearGradient id={`${uid}-fill`} x1=\"0\" x2=\"0\" y1=\"0\" y2=\"1\">\n                <stop offset=\"0%\" style={{ stopColor: \"var(--volt-ink)\", stopOpacity: 0.3 }} />\n                <stop offset=\"100%\" style={{ stopColor: \"var(--volt-ink)\", stopOpacity: 0 }} />\n              </linearGradient>\n              <clipPath id={`${uid}-reveal`}>\n                <motion.rect x={-2} y={-10} height={120} initial={{ width: reduce ? 104 : 0 }} animate={{ width: inView || reduce ? 104 : 0 }} transition={{ duration: reduce ? 0 : 1.3, ease: [0.65, 0, 0.35, 1] }} />\n              </clipPath>\n            </defs>\n            {ticks.map((t, i) => (\n              <line key={t} x1={0} x2={100} y1={yp(t)} y2={yp(t)} className=\"stroke-border\" strokeDasharray={i === 0 ? undefined : \"2 4\"} vectorEffect=\"non-scaling-stroke\" />\n            ))}\n            <line x1={0} x2={100} y1={yp(avg)} y2={yp(avg)} className=\"stroke-ink-subtle\" strokeDasharray=\"6 4\" vectorEffect=\"non-scaling-stroke\" />\n            <g clipPath={`url(#${uid}-reveal)`}>\n              <path d={`${line}L100,100L0,100Z`} fill={`url(#${uid}-fill)`} />\n              <path d={line} fill=\"none\" style={{ stroke: \"var(--volt-ink)\" }} strokeWidth={2.25} strokeLinecap=\"round\" vectorEffect=\"non-scaling-stroke\" />\n            </g>\n            {active !== null && <line x1={xp(active)} x2={xp(active)} y1={0} y2={100} className=\"stroke-ink-subtle\" strokeDasharray=\"3 3\" vectorEffect=\"non-scaling-stroke\" />}\n          </svg>\n\n          <span aria-hidden className=\"absolute right-1 -translate-y-full pb-0.5 font-mono text-[10.5px] text-ink-muted\" style={{ top: `${yp(avg)}%` }}>\n            Avg {formatValue(avg)}\n          </span>\n          {marker(iMax, \"Peak\")}\n          {marker(iMin, \"Low\")}\n\n          <motion.span\n            aria-hidden\n            initial={{ opacity: 0, scale: 0 }}\n            animate={inView ? { opacity: 1, scale: 1 } : undefined}\n            transition={{ delay: reduce ? 0 : 1.2, type: \"spring\", stiffness: 400, damping: 20 }}\n            className=\"pointer-events-none absolute -ml-1.5 -mt-1.5 size-3 rounded-full bg-volt-ink ring-4 ring-volt-soft\"\n            style={{ left: \"100%\", top: `${yp(last)}%` }}\n          />\n          <AnimatePresence>\n            {active !== null && (\n              <motion.span\n                key=\"dot\"\n                aria-hidden\n                initial={{ opacity: 0, left: `${xp(active)}%`, top: `${yp(values[active] ?? 0)}%` }}\n                animate={{ opacity: 1, left: `${xp(active)}%`, top: `${yp(values[active] ?? 0)}%` }}\n                exit={{ opacity: 0 }}\n                transition={reduce ? { duration: 0 } : { type: \"spring\", stiffness: 520, damping: 38 }}\n                className=\"pointer-events-none absolute size-3 -translate-x-1/2 -translate-y-1/2 rounded-full border-2 border-surface-raised bg-volt-ink shadow-sm\"\n              />\n            )}\n          </AnimatePresence>\n        </div>\n        <div />\n        <div aria-hidden className=\"mt-2 flex justify-between font-mono text-[10.5px] text-ink-subtle\">\n          <span>{labels[0]}</span>\n          <span className=\"hidden @md:inline\">{labels[Math.floor((n - 1) / 2)]}</span>\n          <span>{labels[n - 1]}</span>\n        </div>\n      </div>\n      <p className=\"sr-only\" aria-live=\"polite\">\n        {active !== null ? `${labels[active]}: ${formatValue(values[active] ?? 0)}` : \"\"}\n      </p>\n    </figure>\n  );\n}\n"},{"path":"LICENSE-geturui.md","type":"registry:file","target":"LICENSE-geturui.md","content":"MIT License\n\nCopyright (c) 2026 geturui.com\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n"}],"categories":["examples","chart","line chart","time series","comparison","annotations","tooltip","crosshair","svg"],"meta":{"tier":"free","license":"MIT","docs":"https://geturui.com/docs/line-charts#gradient-metric"}}