{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "composed-chart",
  "title": "Composed chart",
  "description": "Bars, lines, areas and points on one plot, each series drawn as its config says and read against the value axis it names, up to one on each side, with the second axis’s ticks on the first’s gridlines. The parts are the Bar, Line and Area charts’ own; each kind of mark stacks in its own layer, the category under the pointer stays bright across every mark, and one tooltip lists every series there.",
  "dependencies": [],
  "registryDependencies": [
    "https://kinoui.com/r/area-chart.json",
    "https://kinoui.com/r/bar-chart.json",
    "https://kinoui.com/r/line-chart.json",
    "https://kinoui.com/r/chart-area-marks.json",
    "https://kinoui.com/r/chart-bar-marks.json",
    "https://kinoui.com/r/chart-line-marks.json",
    "https://kinoui.com/r/chart-point-marks.json",
    "https://kinoui.com/r/chart-bars.json",
    "https://kinoui.com/r/chart-core.json",
    "https://kinoui.com/r/chart-frame.json",
    "https://kinoui.com/r/chart-line.json",
    "https://kinoui.com/r/chart-live.json",
    "https://kinoui.com/r/chart-math.json",
    "https://kinoui.com/r/chart-points.json"
  ],
  "files": [
    {
      "path": "registry/components/kino/composed-chart.tsx",
      "content": "'use client'\n\nimport { Area, type AreaProps } from '@/registry/components/kino/area-chart'\nimport { Bar, type BarProps, BarTotals } from '@/registry/components/kino/bar-chart'\nimport { Line, type LineProps } from '@/registry/components/kino/line-chart'\nimport { areaMarks } from '@/registry/lib/chart-area-marks'\nimport { barMarks, checkBarGap } from '@/registry/lib/chart-bar-marks'\nimport { BarFrameContext, BarPlaceholder } from '@/registry/lib/chart-bars'\nimport {\n  figureProps,\n  seriesWords,\n  useChartActive,\n  useDeclared,\n} from '@/registry/lib/chart-base'\nimport {\n  type CartesianChartProps,\n  cartesianChartKeys,\n  cartesianSignature,\n  cartesianWords,\n  type ChartPartDeclaration,\n  type LiveChartProps,\n  type NamedValueAxis,\n  useCartesian,\n  useCartesianChart,\n} from '@/registry/lib/chart-cartesian'\nimport { VALUE_AXIS } from '@/registry/lib/chart-frame'\nimport {\n  EntranceWipe,\n  framePoints,\n  frameRest,\n  frameTop,\n  LineFamilyFrame,\n  LinePlaceholder,\n  useLineFamily,\n} from '@/registry/lib/chart-line'\nimport { lineMarks } from '@/registry/lib/chart-line-marks'\nimport { ChartLiveLayer } from '@/registry/lib/chart-live-clock'\nimport {\n  cascadeAt,\n  type ChartDatum,\n  type ChartSeries,\n  type ChartStack,\n  plotPoint,\n  rangeOf,\n} from '@/registry/lib/chart-math'\nimport { pointMarks } from '@/registry/lib/chart-point-marks'\nimport { markerAt, type PlacedPoint, PointSeries } from '@/registry/lib/chart-points'\nimport type { BandGaps } from '@/registry/lib/chart-scale'\nimport type { ComponentProps } from 'react'\n\n/*\n * The Composed chart: bars, lines, areas and points on one plot, each series\n * drawn as its config says and read against the value axis it names, up to\n * one on each side. Its parts are the single-mark charts' own, so a bar here\n * looks and moves as the Bar chart's does and a line as the Line chart's;\n * the frame fits every axis as it renders, lines the second axis's ticks up\n * with the first's gridlines, and stacks each kind of mark in its own layer,\n * areas at the back and points in front, whatever order the parts are\n * written in. The category under the pointer stays bright across every\n * mark, and one tooltip lists every series there.\n */\n\n/** A series of a Composed chart: what it is drawn as, which the config always says, and for bars and areas, which stack it joins. */\ntype ComposedSeries = Omit<ChartSeries, 'mark' | 'stack'> &\n  (\n    | {\n        mark: 'bar' | 'area'\n        /** When the chart stacks, the series with the same `stack` stack together, beside the other stacks. */\n        stack?: string\n      }\n    | { mark: 'line' | 'point' }\n  )\n\n/** Every key is a series the chart draws, in the order written, which is also the order they stack in. */\ntype ComposedChartConfig = Record<string, ComposedSeries>\n\ninterface ComposedChartProps extends Omit<CartesianChartProps, 'config'>, LiveChartProps {\n  config: ComposedChartConfig\n  /**\n   * Every value axis besides the chart's own, by the id its series name in\n   * their config's `axis`: how it fits, writes and titles its values. Its\n   * ticks line up with the own axis's gridlines unless `align` is off.\n   */\n  axes?: Readonly<Record<string, NamedValueAxis>>\n  /**\n   * How the bars, and the areas, of one category combine: side by side,\n   * stacked, stacked as shares of their total, or overlapped, each series of\n   * a `stack` in front of the one before. Each kind stacks among itself.\n   */\n  stack?: ChartStack\n  /**\n   * How the marks arrive the first time the chart comes into view: rising\n   * from their baselines, drawn along from the reading start with the bars\n   * rising one after another in step, or simply there, the default, since a\n   * page that loads doesn't animate into place.\n   */\n  entrance?: 'rise' | 'draw' | 'none'\n  /** The space between two categories, as a share of one category's step, when bars stand in them. */\n  categoryGap?: number\n  /** The space between bars side by side, as a share of one bar's slot, more than -1 and less than 1; below 0 each overlaps the one before by that share. */\n  barGap?: number\n}\n\nconst composedChartKeys = [\n  ...cartesianChartKeys,\n  'axes',\n  'stack',\n  'entrance',\n  'categoryGap',\n  'barGap',\n]\n\n/** A line's key in the legend: a short stroke, told apart from a bar's square. */\nconst LINE_KEY = 'M-1.1,-0.3H1.1V0.3H-1.1Z'\n\n/** Every series at a stop, read along x. */\nconst X_BROWSE = { kind: 'x' } as const\n\n/**\n * The chart's root: it fits each value axis to the series read against it,\n * lays out the frame its parts draw in, and places every mark at its\n * category, bars in bands and lines, areas and points at their centres.\n */\nfunction ComposedChart(props: ComposedChartProps) {\n  const {\n    config,\n    axes = {},\n    stack = 'none',\n    entrance = 'none',\n    categoryGap,\n    barGap = 0.12,\n    description,\n    empty,\n    className,\n    children,\n    ref,\n  } = props\n  checkAxes(config, axes)\n  checkBarGap(barGap)\n  const marks = Object.values(config).map((series) => series.mark)\n  const hasBars = marks.includes('bar')\n  const gaps: BandGaps = categoryGap === undefined ? {} : { gap: categoryGap }\n  const cartesian = useCartesianChart(props, {\n    kinds: [barMarks, lineMarks, areaMarks, pointMarks],\n    orientation: 'vertical',\n    stack,\n    entrance: entrance !== 'none',\n    cascade:\n      entrance === 'draw' && hasBars\n        ? { variable: '--composed-chart-cascade', fallback: 0.6 }\n        : undefined,\n    gaps,\n    axes,\n  })\n  const { chart, model, placeholder, fromPlaceholder, phase } = cartesian\n  const stops = chart.current\n  const markOf = model.markOf\n  const pointKeys = chart.series.filter((key) => markOf(key) === 'point')\n  // The legend's key takes each mark's shape, so a series reads by more than its colour.\n  const markerOf = (key: string) => {\n    const mark = markOf(key)\n    if (mark === 'line') return LINE_KEY\n    if (mark === 'point') return markerAt(pointKeys.indexOf(key))\n    return null\n  }\n  const traced = (key: string) => markOf(key) === 'line' || markOf(key) === 'area'\n  return (\n    <BarFrameContext\n      value={{\n        model,\n        kind: barMarks,\n        phase,\n        fromPlaceholder,\n        entrance: entrance === 'draw' ? 'cascade' : entrance,\n        cascade: stops.map((_, at) => cascadeAt(at, stops.length, 'start')),\n        cascadeSpan: 'var(--composed-chart-cascade, 0.6)',\n        barGap,\n        lights: 'stop',\n      }}\n    >\n      <LineFamilyFrame\n        chart={cartesian}\n        markerOf={markerOf}\n        // The tooltip sits over the highest of the stop's marks, whatever they are.\n        anchor={(stop) => {\n          const along = chart.entered ? model.reach(stop.index) : null\n          return plotPoint(\n            stop.band,\n            along ?? model.baselines.value,\n            'vertical',\n            chart.direction,\n          )\n        }}\n        words={seriesWords(chart, cartesianWords(chart, null))}\n        browse={X_BROWSE}\n        family={{\n          name: 'composed-chart',\n          phase,\n          entrance,\n          // A hidden area folds into the one below it; a hidden line or point lies flat.\n          pointsOf: (key, rising) =>\n            framePoints(\n              chart,\n              model,\n              key,\n              rising || (markOf(key) !== 'area' && chart.hidden.includes(key)),\n              markOf(key) === 'area',\n            ),\n          // The lit category's dot marks each line and area; a bar or a point is its own mark.\n          topOf: (key, stop) => (traced(key) ? frameTop(model, key, stop) : null),\n          bottomOf: (key, stop) =>\n            markOf(key) === 'area' && rangeOf(chart.valueOf(key, stop))\n              ? frameTop(model, key, stop, true)\n              : null,\n          restOf: (key) => frameRest(model, key),\n        }}\n        description={description}\n        empty={empty}\n        placeholder={\n          placeholder &&\n          (hasBars ? (\n            <BarPlaceholder chart={chart} />\n          ) : (\n            <LinePlaceholder name='composed-chart' fill={marks.includes('area')} />\n          ))\n        }\n        className={className}\n        ref={ref}\n        {...figureProps(props, composedChartKeys)}\n      >\n        {children}\n      </LineFamilyFrame>\n    </BarFrameContext>\n  )\n}\n\n/**\n * Throws for an axis the config and `axes` disagree on: a series read\n * against an axis `axes` doesn't name, or `axes` naming the chart's own,\n * which the root's own props fit.\n */\nfunction checkAxes(\n  config: ComposedChartConfig,\n  axes: Readonly<Record<string, NamedValueAxis>>,\n) {\n  if (VALUE_AXIS in axes) {\n    throw new Error(\n      `\\`axes\\` names \\`${VALUE_AXIS}\\`, the chart's own value axis: fit it with the chart's own \\`domain\\`, \\`tickCount\\` and \\`format\\`, and title it with its \\`ChartYAxis\\`'s \\`label\\`.`,\n    )\n  }\n  for (const [key, series] of Object.entries(config)) {\n    const axis = series.axis\n    if (axis !== undefined && axis !== VALUE_AXIS && !(axis in axes)) {\n      throw new Error(\n        `The series \\`${key}\\` is read against the axis \\`${axis}\\`, which \\`axes\\` doesn't name. Add it, such as \\`axes={{ ${axis}: { format: { style: 'percent' } } }}\\`.`,\n      )\n    }\n  }\n}\n\ntype PointsProps = Omit<ComponentProps<'div'>, 'children'> & {\n  /** The key of this series' values in the data, and of its entry in the config. */\n  dataKey: string\n  /**\n   * The series' own rows, on an x of times, in place of the chart's: each\n   * with the chart's `index` and the series' `dataKey`, in time order. Its\n   * points stand at their own times.\n   */\n  data?: readonly ChartDatum[]\n  /** Each point's radius, in px. */\n  radius?: number\n}\n\n/**\n * One series' points: a mark at each category's value, in the series'\n * colour and in a shape of its own, as a target beside each bar is. They\n * glide to new values on `layout`, grow in as they arrive and shrink away\n * as they leave or the series hides. The lit category's point stays bright\n * while the rest step back.\n */\nfunction Points(props: PointsProps) {\n  const { dataKey, radius = 5, data, ...rest } = props\n  useDeclared('Points', Points.declare(props), cartesianSignature)\n  const chart = useCartesian()\n  const family = useLineFamily()\n  const { spotlit } = useChartActive()\n  if (chart.status !== 'ready' && chart.status !== 'hidden') return null\n  const hidden = chart.hidden.includes(dataKey)\n  const rising = !chart.entered && family.entrance === 'rise'\n  const rtl = chart.direction === 'rtl'\n  const placed = family.pointsOf(dataKey, rising).flatMap((point): PlacedPoint[] => {\n    if (!point.defined || point.key === null) return []\n    return [\n      {\n        key: point.key,\n        x: rtl ? 1 - point.x : point.x,\n        y: point.y,\n        radius,\n        depth: 0,\n        leaving: hidden,\n      },\n    ]\n  })\n  // A series with no row at the lit stop lights its nearest own one, as the tooltip reads it.\n  const litStop = spotlit === null || hidden ? undefined : chart.current[spotlit]\n  const litKey = (litStop && chart.nearOf(dataKey, litStop)?.key) ?? null\n  const pointKeys = chart.series.filter((key) => chart.config[key]?.mark === 'point')\n  return (\n    <PointSeries\n      name='composed-chart'\n      dataKey={dataKey}\n      data-own={data ? '' : undefined}\n      points={placed}\n      lit={placed.find((point) => point.key === litKey)}\n      seriesIndex={chart.series.indexOf(dataKey)}\n      marker={markerAt(pointKeys.indexOf(dataKey))}\n      dim={spotlit !== null || (chart.emphasis !== null && chart.emphasis !== dataKey)}\n      around={(marks) => (\n        <ChartLiveLayer live={chart.frame.live} direction={chart.direction} reach='plot'>\n          <EntranceWipe draws={family.entrance === 'draw'}>{marks}</EntranceWipe>\n        </ChartLiveLayer>\n      )}\n      {...rest}\n    />\n  )\n}\n\nPoints.declare = ({ dataKey, data }: PointsProps): ChartPartDeclaration => ({\n  series: { key: dataKey, mark: 'point' },\n  ...(data === undefined ? {} : { rows: data }),\n})\n\nexport {\n  Area,\n  type AreaProps,\n  Bar,\n  type BarProps,\n  BarTotals,\n  ComposedChart,\n  type ComposedChartConfig,\n  type ComposedChartProps,\n  type ComposedSeries,\n  Line,\n  type LineProps,\n  Points,\n  type PointsProps,\n}\n",
      "type": "registry:component",
      "target": "@components/kino/composed-chart.tsx"
    }
  ],
  "type": "registry:component"
}