{"name":"sync","type":"registry:ui","files":[{"path":"sync.ts","type":"registry:ui","content":"import type * as echarts from \"echarts/core\";\n\ntype EChartsInstance = ReturnType<typeof echarts.init>;\n\n/**\n * Synced crosshair. Charts that share a `syncId` show their crosshair and\n * tooltip at the same category while the pointer is over any one of them,\n * matched by category value (so the charts may hold different ranges) and\n * falling back to nothing when a chart lacks that category.\n *\n * This is deliberately narrower than `echarts.connect`, which would also\n * replay highlight, selection, and data zoom actions between charts whose\n * series ids happen to match. Brush and zoom ranges stay per chart; mirror\n * them with each Brush's `onChange` when charts should zoom together.\n */\n\ntype Member = {\n  chart: EChartsInstance;\n  /** Category labels in axis order. */\n  categories: () => string[];\n  /** Which screen axis holds the categories. */\n  categoryAxis: \"x\" | \"y\";\n  /** True while the pointer is over this chart. */\n  hovering: boolean;\n  /** The category this chart last showed at another chart's request. */\n  remote: string | null;\n};\n\nconst groups = new Map<string, Set<Member>>();\n\nfunction showAt(member: Member, category: string) {\n  const { chart } = member;\n  if (chart.isDisposed()) return;\n  const index = member.categories().indexOf(category);\n  if (index < 0) {\n    hide(member);\n    return;\n  }\n  if (member.remote === category) return;\n  member.remote = category;\n  try {\n    const axisFinder = member.categoryAxis === \"x\" ? { xAxisIndex: 0 } : { yAxisIndex: 0 };\n    const along = chart.convertToPixel(axisFinder, index);\n    if (typeof along !== \"number\" || !Number.isFinite(along)) return;\n    const grid = (\n      chart as unknown as {\n        getModel: () => {\n          getComponent: (\n            type: string,\n            index: number,\n          ) => { coordinateSystem?: { getRect: () => { x: number; y: number; width: number; height: number } } };\n        };\n      }\n    )\n      .getModel()\n      .getComponent(\"grid\", 0)\n      ?.coordinateSystem?.getRect();\n    const across = grid\n      ? member.categoryAxis === \"x\"\n        ? grid.y + grid.height / 2\n        : grid.x + grid.width / 2\n      : (member.categoryAxis === \"x\" ? chart.getHeight() : chart.getWidth()) / 2;\n    const [x, y] = member.categoryAxis === \"x\" ? [along, across] : [across, along];\n    chart.dispatchAction({ type: \"showTip\", x, y });\n  } catch {\n    // A chart mid-update has no layout to read; the next move catches up.\n  }\n}\n\nfunction hide(member: Member) {\n  if (member.remote === null || member.chart.isDisposed()) return;\n  member.remote = null;\n  member.chart.dispatchAction({ type: \"hideTip\" });\n}\n\n/**\n * Joins a chart to a sync group. Returns the function that leaves it, which\n * removes the listeners and forgets the group once its last chart leaves.\n */\nexport function joinChartSync(params: {\n  syncId: string;\n  chart: EChartsInstance;\n  categories: () => string[];\n  categoryAxis: () => \"x\" | \"y\";\n}): () => void {\n  const { syncId, chart, categories } = params;\n  const member: Member = {\n    chart,\n    categories,\n    get categoryAxis() {\n      return params.categoryAxis();\n    },\n    hovering: false,\n    remote: null,\n  };\n\n  let group = groups.get(syncId);\n  if (!group) {\n    group = new Set();\n    groups.set(syncId, group);\n  }\n  group.add(member);\n\n  const others = () => [...(groups.get(syncId) ?? [])].filter((m) => m !== member);\n\n  const onPointer = (event: unknown) => {\n    if (!member.hovering) return;\n    const axesInfo = (event as { axesInfo?: { axisDim?: string; axisIndex?: number; value?: unknown }[] })\n      .axesInfo;\n    const info = axesInfo?.find(\n      (axis) => axis.axisDim === member.categoryAxis && (axis.axisIndex ?? 0) === 0,\n    );\n    const index = typeof info?.value === \"number\" ? info.value : Number(info?.value);\n    const category = Number.isInteger(index) ? member.categories()[index] : undefined;\n    for (const other of others()) {\n      if (category === undefined) hide(other);\n      else showAt(other, category);\n    }\n  };\n\n  const zr = chart.getZr();\n  const onMove = () => {\n    if (member.hovering) return;\n    member.hovering = true;\n    member.remote = null;\n    for (const other of others()) other.hovering = false;\n  };\n  const onOut = () => {\n    if (!member.hovering) return;\n    member.hovering = false;\n    for (const other of others()) hide(other);\n  };\n\n  chart.on(\"updateAxisPointer\", onPointer);\n  zr.on(\"mousemove\", onMove);\n  zr.on(\"globalout\", onOut);\n\n  return () => {\n    if (!chart.isDisposed()) {\n      chart.off(\"updateAxisPointer\", onPointer);\n      zr.off(\"mousemove\", onMove);\n      zr.off(\"globalout\", onOut);\n    }\n    if (member.hovering) for (const other of others()) hide(other);\n    const current = groups.get(syncId);\n    current?.delete(member);\n    if (current && current.size === 0) groups.delete(syncId);\n  };\n}\n","target":"components/ui/sync.ts"}],"dependencies":["echarts"]}