{"name":"reference","type":"registry:ui","files":[{"path":"reference.tsx","type":"registry:ui","content":"import {\n  normalizeColor,\n  withAlpha,\n  type ChartConfig,\n  type ResolvedColors,\n} from \"@/components/ui/charts/chart\";\nimport { LineChart as LineChartModule, type LineSeriesOption } from \"echarts/charts\";\nimport {\n  MarkAreaComponent,\n  MarkLineComponent,\n  MarkPointComponent,\n} from \"echarts/components\";\nimport { Children, isValidElement, type FC, type ReactNode } from \"react\";\nimport * as echarts from \"echarts/core\";\n\n// The marks ride on an invisible line series, so the line module is\n// registered here for charts that do not draw lines themselves.\necharts.use([LineChartModule, MarkLineComponent, MarkAreaComponent, MarkPointComponent]);\n\n/**\n * Reference marks use screen axes: `x` is a position on the horizontal axis\n * and `y` on the vertical axis, whatever the chart layout. On a category axis\n * pass the category as it appears in the data; on a value axis pass a number.\n */\nexport type ReferenceValue = string | number;\nexport type ReferenceStroke = \"dashed\" | \"solid\";\nexport type ReferenceLabelPosition = \"start\" | \"center\" | \"end\";\n\nexport interface ReferenceLineProps {\n  x?: ReferenceValue;\n  y?: ReferenceValue;\n  label?: string;\n  /** Where the label sits along the line. Defaults to `end` (right, or top). */\n  labelPosition?: ReferenceLabelPosition;\n  strokeVariant?: ReferenceStroke;\n  strokeWidth?: number;\n  /** A chart config key or any CSS color. Defaults to the muted foreground. */\n  color?: string;\n  /** Grow the value axis to keep the line in view. Defaults to true. */\n  extendDomain?: boolean;\n}\n\nexport interface ReferenceAreaProps {\n  x1?: ReferenceValue;\n  x2?: ReferenceValue;\n  y1?: ReferenceValue;\n  y2?: ReferenceValue;\n  label?: string;\n  labelPosition?: ReferenceLabelPosition;\n  color?: string;\n  extendDomain?: boolean;\n}\n\nexport interface ReferenceDotProps {\n  x: ReferenceValue;\n  y: ReferenceValue;\n  label?: string;\n  labelPosition?: \"top\" | \"bottom\" | \"left\" | \"right\";\n  color?: string;\n  /** Dot diameter in pixels. Defaults to 8. */\n  size?: number;\n  extendDomain?: boolean;\n}\n\nexport const ReferenceLine: FC<ReferenceLineProps> = () => null;\nReferenceLine.displayName = \"ReferenceLine\";\nexport const ReferenceArea: FC<ReferenceAreaProps> = () => null;\nReferenceArea.displayName = \"ReferenceArea\";\nexport const ReferenceDot: FC<ReferenceDotProps> = () => null;\nReferenceDot.displayName = \"ReferenceDot\";\n\nexport type ReferenceSlots = {\n  lines: ReferenceLineProps[];\n  areas: ReferenceAreaProps[];\n  dots: ReferenceDotProps[];\n};\n\nfunction isSlot(type: unknown, slot: unknown, name: string) {\n  if (type === slot) return true;\n  if (typeof type !== \"function\") return false;\n  const candidate = (type as FC).displayName ?? type.name;\n  return candidate === name || (typeof candidate === \"string\" && candidate.endsWith(`.${name}`));\n}\n\n/** Reads reference slots from a chart's children. Null when there are none. */\nexport function collectReferences(children: ReactNode): ReferenceSlots | null {\n  const slots: ReferenceSlots = { lines: [], areas: [], dots: [] };\n  Children.forEach(children, (child) => {\n    if (!isValidElement(child)) return;\n    if (isSlot(child.type, ReferenceLine, \"ReferenceLine\")) {\n      slots.lines.push(child.props as ReferenceLineProps);\n    } else if (isSlot(child.type, ReferenceArea, \"ReferenceArea\")) {\n      slots.areas.push(child.props as ReferenceAreaProps);\n    } else if (isSlot(child.type, ReferenceDot, \"ReferenceDot\")) {\n      slots.dots.push(child.props as ReferenceDotProps);\n    }\n  });\n  return slots.lines.length || slots.areas.length || slots.dots.length ? slots : null;\n}\n\n/** Config keys used as reference colors, so a chart resolves them with its series. */\nexport function referenceColorKeys(refs: ReferenceSlots | null, config: ChartConfig): string[] {\n  if (!refs) return [];\n  const keys = new Set<string>();\n  for (const item of [...refs.lines, ...refs.areas, ...refs.dots]) {\n    if (item.color && config[item.color]) keys.add(item.color);\n  }\n  return [...keys];\n}\n\n/**\n * Values on one screen axis that marks keep in view. For charts that set\n * their own axis bounds, such as the scatter chart, instead of the carrier.\n */\nexport function referenceValues(refs: ReferenceSlots | null, axis: \"x\" | \"y\"): number[] {\n  if (!refs) return [];\n  const values: unknown[] = [];\n  for (const line of refs.lines) {\n    if (line.extendDomain !== false) values.push(axis === \"x\" ? line.x : line.y);\n  }\n  for (const area of refs.areas) {\n    if (area.extendDomain !== false) {\n      values.push(...(axis === \"x\" ? [area.x1, area.x2] : [area.y1, area.y2]));\n    }\n  }\n  for (const dot of refs.dots) {\n    if (dot.extendDomain !== false) values.push(axis === \"x\" ? dot.x : dot.y);\n  }\n  return values\n    .filter((value) => value !== undefined && value !== null && value !== \"\")\n    .map(Number)\n    .filter(Number.isFinite);\n}\n\nfunction resolveColor(color: string | undefined, resolved: ResolvedColors, fallback: string) {\n  if (!color) return fallback;\n  const slot = resolved.series[color]?.[0];\n  if (slot) return slot;\n  const normalized = normalizeColor(color);\n  return normalized.startsWith(\"rgba(\") ? normalized : fallback;\n}\n\nconst LABEL_HEIGHT = 18;\n\nfunction labelPill(tokens: ResolvedColors[\"tokens\"]) {\n  return {\n    color: tokens.foreground,\n    fontSize: 10,\n    fontWeight: 500,\n    lineHeight: 12,\n    backgroundColor: tokens.background,\n    borderColor: tokens.border,\n    borderWidth: 1,\n    borderRadius: 4,\n    padding: [2, 5],\n  };\n}\n\nconst HORIZONTAL_LABEL: Record<ReferenceLabelPosition, string> = {\n  start: \"insideStartTop\",\n  center: \"insideMiddleTop\",\n  end: \"insideEndTop\",\n};\n\nconst AREA_LABEL: Record<ReferenceLabelPosition, string> = {\n  start: \"insideTopLeft\",\n  center: \"insideTop\",\n  end: \"insideTopRight\",\n};\n\n/**\n * Builds the invisible carrier series that draws every reference mark.\n * `valueAxes` names the axes that are numeric, so marks on them can widen\n * the axis: the carrier holds an invisible point at each such value.\n */\nexport function buildReferenceSeries(params: {\n  refs: ReferenceSlots | null;\n  resolved: ResolvedColors;\n  valueAxes: { x: boolean; y: boolean };\n  xAxisIndex?: number;\n  yAxisIndex?: number;\n  z?: number;\n}): LineSeriesOption | null {\n  const { refs, resolved, valueAxes, xAxisIndex = 0, yAxisIndex = 0, z = 5 } = params;\n  if (!refs) return null;\n  const { tokens } = resolved;\n  // A guide, not data: the muted ink, eased back so it stays quieter than a\n  // series in dark mode, where the muted ink is light.\n  const muted = withAlpha(tokens.mutedForeground, 0.75);\n\n  // Points that keep values in view. The other coordinate is the first\n  // category on a category axis, or empty on a value axis so that axis is\n  // left alone.\n  const extent: [ReferenceValue, ReferenceValue][] = [];\n  const keep = (axis: \"x\" | \"y\", value: ReferenceValue | undefined, extend?: boolean) => {\n    if (extend === false || value === undefined || !valueAxes[axis]) return;\n    const n = Number(value);\n    if (!Number.isFinite(n)) return;\n    const other = valueAxes[axis === \"x\" ? \"y\" : \"x\"] ? \"-\" : 0;\n    extent.push(axis === \"x\" ? [n, other] : [other, n]);\n  };\n\n  const lineData = refs.lines.flatMap((line) => {\n    const isVertical = line.x !== undefined;\n    if (!isVertical && line.y === undefined) return [];\n    keep(isVertical ? \"x\" : \"y\", isVertical ? line.x : line.y, line.extendDomain);\n    const color = resolveColor(line.color, resolved, muted);\n    const position = line.labelPosition ?? \"end\";\n    // Labels inside a line are drawn along it, which would stand a vertical\n    // label on its side. Vertical labels sit on the line's end instead, as a\n    // level pill pulled just inside the plot.\n    const label = line.label\n      ? isVertical\n        ? {\n            show: true,\n            formatter: line.label,\n            position: position === \"start\" ? \"start\" : \"end\",\n            distance: [0, -LABEL_HEIGHT - 2],\n            ...labelPill(tokens),\n          }\n        : {\n            show: true,\n            formatter: line.label,\n            position: HORIZONTAL_LABEL[position],\n            distance: 4,\n            ...labelPill(tokens),\n          }\n      : { show: false };\n    return [\n      {\n        ...(isVertical ? { xAxis: line.x } : { yAxis: line.y }),\n        lineStyle: {\n          color,\n          width: line.strokeWidth ?? 1,\n          type: line.strokeVariant === \"solid\" ? \"solid\" : [3, 3],\n          opacity: 1,\n        },\n        label,\n      },\n    ];\n  });\n\n  const areaData = refs.areas.flatMap((area) => {\n    const hasX = area.x1 !== undefined || area.x2 !== undefined;\n    const hasY = area.y1 !== undefined || area.y2 !== undefined;\n    if (!hasX && !hasY) return [];\n    keep(\"x\", area.x1, area.extendDomain);\n    keep(\"x\", area.x2, area.extendDomain);\n    keep(\"y\", area.y1, area.extendDomain);\n    keep(\"y\", area.y2, area.extendDomain);\n    const color = resolveColor(area.color, resolved, tokens.foreground);\n    const tint = area.color ? 0.1 : 0.045;\n    // An open edge runs to the edge of the plot.\n    const from: Record<string, unknown> = {\n      name: area.label ?? \"\",\n      itemStyle: { color: withAlpha(color, tint) },\n      label: {\n        show: !!area.label,\n        position: AREA_LABEL[area.labelPosition ?? \"start\"],\n        color: area.color ? color : tokens.mutedForeground,\n        fontSize: 10,\n        fontWeight: 500,\n        padding: [6, 6],\n      },\n    };\n    const to: Record<string, unknown> = {};\n    if (hasX) {\n      from.xAxis = area.x1 ?? \"min\";\n      to.xAxis = area.x2 ?? \"max\";\n    }\n    if (hasY) {\n      from.yAxis = area.y1 ?? \"min\";\n      to.yAxis = area.y2 ?? \"max\";\n    }\n    return [[from, to]];\n  });\n\n  const dotData = refs.dots.map((dot) => {\n    keep(\"x\", dot.x, dot.extendDomain);\n    keep(\"y\", dot.y, dot.extendDomain);\n    const color = resolveColor(dot.color, resolved, tokens.foreground);\n    return {\n      coord: [dot.x, dot.y],\n      symbol: \"circle\",\n      symbolSize: dot.size ?? 8,\n      itemStyle: { color, borderColor: tokens.background, borderWidth: 2 },\n      label: dot.label\n        ? {\n            show: true,\n            formatter: dot.label,\n            position: dot.labelPosition ?? \"top\",\n            distance: 6,\n            ...labelPill(tokens),\n          }\n        : { show: false },\n    };\n  });\n\n  return {\n    id: \"__reference\",\n    type: \"line\",\n    xAxisIndex,\n    yAxisIndex,\n    data: extent,\n    silent: true,\n    showSymbol: false,\n    symbol: \"none\",\n    lineStyle: { opacity: 0, width: 0 },\n    tooltip: { show: false },\n    emphasis: { disabled: true },\n    animation: false,\n    legendHoverLink: false,\n    z,\n    markLine: lineData.length\n      ? ({\n          silent: true,\n          animation: false,\n          symbol: [\"none\", \"none\"],\n          emphasis: { disabled: true },\n          data: lineData,\n        } as LineSeriesOption[\"markLine\"])\n      : undefined,\n    markArea: areaData.length\n      ? ({\n          silent: true,\n          animation: false,\n          emphasis: { disabled: true },\n          data: areaData,\n        } as LineSeriesOption[\"markArea\"])\n      : undefined,\n    markPoint: dotData.length\n      ? ({\n          silent: true,\n          animation: false,\n          emphasis: { disabled: true },\n          data: dotData,\n        } as LineSeriesOption[\"markPoint\"])\n      : undefined,\n  };\n}\n","target":"components/ui/reference.tsx"}],"dependencies":["echarts"],"registryDependencies":["https://www.honestui.com/r/chart.json"]}