Synchronizing charts

Share active observations between independent owners.

Explicit group

Create a group with createChartSyncGroup and pass it as synchronization to the createChart calls. Each group synchronizes the charts you have connected to it.

mount-linked-charts.ts
import { createChart, createChartSyncGroup, type ChartController, type ChartData } from '@neural-charts/web';

export function mountLinkedCharts(entries: readonly { host: HTMLElement; data: ChartData; id: string }[]) {
  const synchronization = createChartSyncGroup();
  const charts: ChartController[] = [];
  function dispose() {
    for (const chart of charts) chart.destroy();
    synchronization.destroy();
  }

  for (const { host, data, id } of entries) {
    const result = createChart(host, data, {}, { instanceId: id, synchronization });
    if (result.status === 'invalid') {
      dispose();
      throw new Error(result.diagnostic.message);
    }
    charts.push(result.value);
  }
  return dispose;
}

End of life

Controllers leave the group when destroyed. The group owner calls synchronization.destroy() when it is no longer needed. An unmatched datum never activates an unrelated observation.