{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "sparkline",
  "title": "Sparkline",
  "description": "A trend small enough to sit beside a value, in a table row or a stat card: a line, an area or bars, with no axes and an optional end dot. The point under the pointer or the keys stays bright while the rest steps back, and new values move every point from where it is.",
  "dependencies": [],
  "registryDependencies": [
    "https://kinoui.com/r/area-chart.json",
    "https://kinoui.com/r/bar-chart.json",
    "https://kinoui.com/r/chart-core.json",
    "https://kinoui.com/r/chart-math.json",
    "https://kinoui.com/r/line-chart.json",
    "https://kinoui.com/r/line-path.json"
  ],
  "files": [
    {
      "path": "registry/components/kino/sparkline.tsx",
      "content": "'use client'\n\nimport { Area, AreaChart } from '@/registry/components/kino/area-chart'\nimport { Bar, BarChart } from '@/registry/components/kino/bar-chart'\nimport { Line, LineChart } from '@/registry/components/kino/line-chart'\nimport type { CategoryChartProps } from '@/registry/lib/chart-base'\nimport { type ChartDatum, numberOf, rangeOf } from '@/registry/lib/chart-math'\nimport type { DomainEnd } from '@/registry/lib/chart-scale'\nimport type { LineCurve } from '@/registry/lib/line-path'\n\nimport { cn } from '@/lib/utils'\n\n/*\n * The Sparkline: a trend small enough to sit beside a value, in a table row\n * or a stat card. It is the Line, Area or Bar chart with no axes, so it\n * looks, moves and reads as they do: the point under the pointer or the\n * keys stays bright while the rest steps back, a new range moves every point\n * from where it is, and the plot is a slider a keyboard steps through. A\n * line fits its range to the data, so its rise and fall fill the room; an\n * area and bars start at zero, since their size is their value.\n */\n\ntype SparklineVariant = 'line' | 'area' | 'bar'\n\ninterface SparklineProps extends Omit<\n  CategoryChartProps,\n  | 'tickCount'\n  | 'tickValues'\n  | 'allowDecimals'\n  | 'patterns'\n  | 'hiddenSeries'\n  | 'defaultHiddenSeries'\n  | 'onHiddenSeriesChange'\n> {\n  /** Draws each series as a line, a line with the area under it filled, or bars. */\n  variant?: SparklineVariant\n  /** How a line runs between its points: a smooth curve that never overshoots them, straight, or in steps. */\n  curve?: LineCurve\n  /** Marks the last value of each line with a dot, where the value beside it reads. */\n  endDot?: boolean\n  /**\n   * Series drawn as a dashed line with no fill, behind the rest: the period\n   * before, to compare with. Bars draw no dashes, so a bar sparkline draws them as bars.\n   */\n  dashed?: readonly string[]\n  /**\n   * The key of the first category whose values are still coming in, such as\n   * the minute now running: a line is dashed from the one before it on, and\n   * the tooltip says so. Areas and bars draw it as the rest.\n   */\n  forecastFrom?: string\n  /**\n   * Fits an area's range to its data, as a line's is, with room below, in\n   * place of starting at zero: for a trend whose shape matters more than its\n   * size, such as a KPI's, whose number says the size. Values that never go\n   * below zero are never grounded below it. Lines and bars keep their range.\n   */\n  fit?: boolean\n  /**\n   * How it arrives the first time it comes into view: drawn along from the\n   * reading start, bars one after another, or simply there, the default,\n   * since a page that loads doesn't animate into place.\n   */\n  entrance?: 'draw' | 'none'\n}\n\n/** The words a sparkline says in its own small room, in place of the charts' longer ones. */\nconst sparklineMessages = {\n  empty: 'No data',\n  error: 'Couldn’t load',\n}\n\n/**\n * The range a sparkline's values run over: the ends `given`, and each end\n * left `'auto'` where the data reaches, never rounded out to a tick. A\n * line's runs from its lowest value to its highest, so its shape fills the\n * room, and an area's or bars' from zero, since their size is their value.\n * A `fit` area runs from below its lowest value, by half its rise and fall,\n * so its shape fills the room above a band of fill.\n */\nfunction sparklineDomain(\n  data: readonly ChartDatum[],\n  keys: readonly string[],\n  variant: SparklineVariant,\n  given: readonly [DomainEnd, DomainEnd] = ['auto', 'auto'],\n  fit = false,\n): readonly [DomainEnd, DomainEnd] {\n  const fits = fit && variant === 'area'\n  const [low, high] = fittedDomain(data, keys, fits ? 'line' : variant)\n  // The floor comes from the data's own lowest value, before a flat line's room is added.\n  const [floor] = fits ? fittedDomain(data, keys, 'area') : [low]\n  const lowest =\n    typeof floor === 'number'\n      ? floor\n      : typeof low === 'number' && typeof high === 'number'\n        ? (low + high) / 2\n        : low\n  const ground =\n    fits && typeof low === 'number' && typeof high === 'number'\n      ? Math.max(\n          typeof lowest === 'number' && lowest >= 0 ? 0 : -Infinity,\n          low - (high - low) / 2,\n        )\n      : low\n  return [given[0] === 'auto' ? ground : given[0], given[1] === 'auto' ? high : given[1]]\n}\n\n/** Where the data reaches: a line with one value sits in the middle; with no values, it is left to fit. */\nfunction fittedDomain(\n  data: readonly ChartDatum[],\n  keys: readonly string[],\n  variant: SparklineVariant,\n): readonly [DomainEnd, DomainEnd] {\n  let low = variant === 'line' ? Infinity : 0\n  let high = variant === 'line' ? -Infinity : 0\n  for (const row of data) {\n    for (const key of keys) {\n      const values = rangeOf(row[key]) ?? [numberOf(row[key])]\n      for (const value of values) {\n        if (value === null) continue\n        low = Math.min(low, value)\n        high = Math.max(high, value)\n      }\n    }\n  }\n  if (!Number.isFinite(low)) return ['auto', 'auto']\n  if (low === high) {\n    if (variant !== 'line') return ['auto', 'auto']\n    const room = Math.abs(low) / 10 || 1\n    return [low - room, high + room]\n  }\n  return [low, high]\n}\n\n/** The line charts' own variables, read from the sparkline's, so one name tunes both variants. */\nconst sparklineVariables: Partial<\n  Record<SparklineVariant, Record<`--${string}`, string>>\n> = {\n  line: {\n    '--line-chart-width': 'var(--sparkline-width, 1.5px)',\n  },\n  area: {\n    '--area-chart-width': 'var(--sparkline-width, 1.5px)',\n    '--area-chart-fill-top': 'var(--sparkline-fill-top, 0.32)',\n    '--area-chart-fill-bottom': 'var(--sparkline-fill-bottom, 0.04)',\n  },\n}\n\n/**\n * A sparkline: one or more series drawn small, with no axes, in a\n * `<figure>` named by its `label`. A chart's `ChartTooltip` goes inside it\n * as it goes inside a chart.\n */\nfunction Sparkline({\n  variant = 'line',\n  curve = 'monotone',\n  endDot = false,\n  dashed = [],\n  forecastFrom,\n  fit = false,\n  entrance = 'none',\n  domain,\n  messages,\n  className,\n  style,\n  children,\n  ...props\n}: SparklineProps) {\n  const keys = Object.keys(props.config)\n  const shared = {\n    'data-sparkline': '',\n    ...props,\n    domain: sparklineDomain(props.data, keys, variant, domain, fit),\n    messages: { ...sparklineMessages, ...messages },\n    'data-variant': variant,\n    // Every variant keeps room beside the plot for an end dot, so sparklines in a column line\n    // up, and the status's words and button sit in one row, in the room a sparkline has.\n    className: cn(\n      'h-10 grid-cols-[minmax(0.375rem,auto)_minmax(0,1fr)_minmax(0.375rem,auto)] py-1 [&_[data-slot=chart-status]]:flex-row [&_[data-slot=chart-status]]:gap-1.5 [&_[data-slot=chart-status]]:p-0 [&_[data-slot=chart-status]_p]:text-xs',\n      className,\n    ),\n    style: { ...sparklineVariables[variant], ...style },\n  }\n  if (variant === 'bar') {\n    return (\n      <BarChart {...shared} entrance={entrance === 'draw' ? 'cascade' : 'none'}>\n        {keys.map((key) => (\n          <Bar key={key} dataKey={key} />\n        ))}\n        {children}\n      </BarChart>\n    )\n  }\n  const dots = endDot ? 'last' : false\n  const solid = keys.filter((key) => !dashed.includes(key))\n  // Drawn first, so the series compared with them sit over them.\n  const behind = keys\n    .filter((key) => dashed.includes(key))\n    .map((key) => <Line key={key} dataKey={key} curve={curve} dashed />)\n  if (variant === 'area') {\n    // Its dashed series are lines among the areas.\n    const config = Object.fromEntries(\n      keys.map((key) => [\n        key,\n        dashed.includes(key)\n          ? { ...props.config[key], mark: 'line' as const }\n          : props.config[key],\n      ]),\n    )\n    return (\n      <AreaChart {...shared} config={config} entrance={entrance}>\n        {behind}\n        {solid.map((key) => (\n          <Area key={key} dataKey={key} curve={curve} dots={dots} />\n        ))}\n        {children}\n      </AreaChart>\n    )\n  }\n  return (\n    <LineChart {...shared} zero={false} entrance={entrance}>\n      {behind}\n      {solid.map((key) => (\n        <Line\n          key={key}\n          dataKey={key}\n          curve={curve}\n          dots={dots}\n          forecastFrom={forecastFrom}\n        />\n      ))}\n      {children}\n    </LineChart>\n  )\n}\n\nexport { Sparkline, type SparklineProps, type SparklineVariant, sparklineDomain }\n",
      "type": "registry:component",
      "target": "@components/kino/sparkline.tsx"
    }
  ],
  "type": "registry:component"
}