# Synced Crosshair

> Move the crosshair and tooltip across several charts at once.

Source: https://www.honestui.com/docs/charts/ui/sync

## Usage [#usage]

Dashboards often stack charts that share a time axis: requests above errors, orders above basket size. Give each chart the same `syncId` and hovering any one of them moves the crosshair and tooltip on all of them to the same category, so a spike in one can be read against the others without lining them up by eye.

```tsx
import { BarChart, LineChart } from "honestui/charts";

<BarChart data={data} config={ordersConfig} xDataKey="day" syncId="store-week">
  <BarChart.Tooltip />
  <BarChart.Bar dataKey="orders" />
</BarChart>

<LineChart data={data} config={basketConfig} xDataKey="day" syncId="store-week">
  <LineChart.Tooltip />
  <LineChart.Line dataKey="basket" />
</LineChart>
```

Any string works as an id; charts with different ids, or none, stay independent. Each chart needs its own `<Tooltip />` to show anything when another chart is hovered.

## Examples [#examples]

### Two line charts [#two-line-charts]

Traffic and error rate over the same minutes. Hover either chart.

### syncId=&#x22;service-health&#x22;

```tsx
"use client";

import { LineChart, type ChartConfig } from "honestui/charts";

// Scenario: Service health, one row per signal, read at the same minute
const minutes = ["09:00", "09:05", "09:10", "09:15", "09:20", "09:25", "09:30", "09:35", "09:40", "09:45", "09:50", "09:55"];
const requests = [1240, 1310, 1295, 1420, 1580, 1720, 1690, 1610, 1540, 1480, 1450, 1390];
const errors = [0.4, 0.5, 0.4, 0.6, 1.1, 2.3, 1.9, 1.2, 0.8, 0.6, 0.5, 0.4];

const traffic = minutes.map((minute, i) => ({ minute, requests: requests[i] }));
const errorRate = minutes.map((minute, i) => ({ minute, errors: errors[i] }));

const trafficConfig = {
  requests: { label: "Requests/min", colors: { light: ["#2563eb"], dark: ["#60a5fa"] } },
} satisfies ChartConfig;

const errorConfig = {
  errors: { label: "Error rate %", colors: { light: ["#ea580c"], dark: ["#fb923c"] } },
} satisfies ChartConfig;

export function ExampleSyncedCrosshairLineChart() {
  return (
    <div className="grid h-full w-full grid-rows-2 gap-2 p-4">
      <LineChart data={traffic} config={trafficConfig} xDataKey="minute" syncId="service-health" className="min-h-0"> {/* [!code highlight] */}
        <LineChart.Grid />
        <LineChart.Tooltip />
        <LineChart.Line dataKey="requests" />
      </LineChart>
      <LineChart data={errorRate} config={errorConfig} xDataKey="minute" syncId="service-health" className="min-h-0"> {/* [!code highlight] */}
        <LineChart.Grid />
        <LineChart.XAxis />
        <LineChart.Tooltip />
        <LineChart.Line dataKey="errors" />
      </LineChart>
    </div>
  );
}

```

### Bar and line chart [#bar-and-line-chart]

Charts of different kinds sync the same way.

### syncId=&#x22;store-week&#x22;

```tsx
"use client";

import { BarChart, type ChartConfig } from "honestui/charts";
import { LineChart } from "honestui/charts";

// Scenario: Store orders and average basket size on the same days
const data = [
  { day: "Mon", orders: 182, basket: 38.2 },
  { day: "Tue", orders: 164, basket: 36.9 },
  { day: "Wed", orders: 201, basket: 41.5 },
  { day: "Thu", orders: 216, basket: 40.1 },
  { day: "Fri", orders: 274, basket: 44.8 },
  { day: "Sat", orders: 312, basket: 52.6 },
  { day: "Sun", orders: 248, basket: 49.3 },
];

const ordersConfig = {
  orders: { label: "Orders", colors: { light: ["#2563eb"], dark: ["#3b82f6"] } },
} satisfies ChartConfig;

const basketConfig = {
  basket: { label: "Avg. basket ($)", colors: { light: ["#c026d3"], dark: ["#e879f9"] } },
} satisfies ChartConfig;

export function ExampleSyncedCrosshairBarChart() {
  return (
    <div className="grid h-full w-full grid-rows-2 gap-2 p-4">
      <BarChart data={data} config={ordersConfig} xDataKey="day" syncId="store-week" className="min-h-0"> {/* [!code highlight] */}
        <BarChart.Grid />
        <BarChart.Tooltip />
        <BarChart.Bar dataKey="orders" />
      </BarChart>
      <LineChart data={data} config={basketConfig} xDataKey="day" syncId="store-week" className="min-h-0"> {/* [!code highlight] */}
        <LineChart.Grid />
        <LineChart.XAxis />
        <LineChart.Tooltip />
        <LineChart.Line dataKey="basket" />
      </LineChart>
    </div>
  );
}

```

### Candlestick and combo chart [#candlestick-and-combo-chart]

A price above its trading activity. `ComboChart`, `WaterfallChart`, and `CandlestickChart` take a `syncId` too.

### syncId=&#x22;share-day&#x22;

```tsx
"use client";

import { CandlestickChart } from "honestui/charts";
import { ComboChart, type ChartConfig } from "honestui/charts";

// Scenario: A share price and its trading activity on the same days
const data = (() => {
  const rows: {
    date: string;
    open: number;
    close: number;
    high: number;
    low: number;
    trades: number;
    average: number;
  }[] = [];
  let close = 182.4;
  const day = new Date(Date.UTC(2026, 8, 1));
  while (rows.length < 24) {
    const weekday = day.getUTCDay();
    if (weekday !== 0 && weekday !== 6) {
      const i = rows.length;
      const open = Math.round((close + Math.sin(i * 2.3) * 0.9) * 100) / 100;
      close = Math.round((open + Math.sin(i * 0.55) * 2.6 + Math.cos(i * 1.7) * 1.4) * 100) / 100;
      const trades = Math.round(4200 + Math.abs(close - open) * 1600 + Math.abs(Math.sin(i * 0.9)) * 1800);
      rows.push({
        date: day.toISOString().slice(0, 10),
        open,
        close,
        high: Math.round((Math.max(open, close) + 0.6 + Math.abs(Math.sin(i * 3.1)) * 1.8) * 100) / 100,
        low: Math.round((Math.min(open, close) - 0.6 - Math.abs(Math.cos(i * 2.7)) * 1.8) * 100) / 100,
        trades,
        average: 0,
      });
    }
    day.setUTCDate(day.getUTCDate() + 1);
  }
  rows.forEach((row, i) => {
    const window = rows.slice(Math.max(0, i - 4), i + 1);
    row.average = Math.round(window.reduce((sum, r) => sum + r.trades, 0) / window.length);
  });
  return rows;
})();

const priceConfig = {
  up: { label: "Up", colors: { light: ["#16a34a"], dark: ["#22c55e"] } },
  down: { label: "Down", colors: { light: ["#dc2626"], dark: ["#ef4444"] } },
} satisfies ChartConfig;

const activityConfig = {
  trades: { label: "Trades", colors: { light: ["#94a3b8"], dark: ["#475569"] } },
  average: { label: "5-day average", colors: { light: ["#2563eb"], dark: ["#60a5fa"] } },
} satisfies ChartConfig;

const shortDate = (value: string) =>
  new Date(`${value}T00:00:00Z`).toLocaleDateString("en-US", {
    month: "short",
    day: "numeric",
    timeZone: "UTC",
  });

export function ExampleSyncedCrosshairComboChart() {
  return (
    <div className="grid h-full w-full grid-rows-[3fr_2fr] gap-2 p-4">
      <CandlestickChart data={data} config={priceConfig} xDataKey="date" syncId="share-day" className="min-h-0"> {/* [!code highlight] */}
        <CandlestickChart.Grid />
        <CandlestickChart.YAxis position="left" tickFormatter={(value) => `$${value.toFixed(0)}`} />
        <CandlestickChart.Tooltip crosshair={false} labelFormatter={shortDate} valueFormatter={(value) => `$${value.toFixed(2)}`} />
        <CandlestickChart.Candles />
      </CandlestickChart>
      <ComboChart data={data} config={activityConfig} xDataKey="date" syncId="share-day" className="min-h-0"> {/* [!code highlight] */}
        <ComboChart.Grid />
        <ComboChart.XAxis tickFormatter={shortDate} />
        <ComboChart.YAxis tickFormatter={(value) => `${(value / 1000).toFixed(0)}k`} />
        <ComboChart.Tooltip labelFormatter={shortDate} />
        <ComboChart.Bar dataKey="trades" />
        <ComboChart.Line dataKey="average" />
      </ComboChart>
    </div>
  );
}

```

## How charts are matched [#how-charts-are-matched]

Charts are matched on the category value under the pointer, not its position. A chart covering only part of the range, or one in a different order, still shows the right row; a chart that lacks the hovered category hides its tooltip until the pointer reaches one it has. Category values must therefore be written the same way in every chart, so use one key format for dates across the group.

A horizontal bar chart syncs on its vertical category axis. The scatter chart has no category axis and does not take a `syncId`.

## What is not synced [#what-is-not-synced]

Only the crosshair and tooltip follow the pointer. Each chart keeps its own series selection, hover highlight, legend, and brush or zoom window. To zoom a group together, hold the range in state and pass it on from one chart's Brush or Zoom `onChange`. The charts deliberately do not use `echarts.connect`, which would also replay highlight and zoom actions between any charts whose series ids happen to match.

## Accessibility [#accessibility]

Syncing is a pointer convenience and adds nothing for keyboard or screen reader users. When readers need to relate the charts, provide a table with one row per category and a column per measure.

## Supported Charts [#supported-charts]

`AreaChart`, `BarChart`, and `LineChart` accept `syncId`, and charts of different kinds can share one.

## API Reference [#api-reference]

<ApiHeading>
  Root
</ApiHeading>


  ### `syncId`

type: `string`

Charts with the same id show their crosshair and tooltip at the same category while any one of
    them is hovered.
