Demo
Synchronized charts
Documented in Chart interaction.
Synchronized charts
Two charts in a ChartSyncGroup share the hovered day (tooltip and crosshair), the x zoom range and the selected points; each kind of sync can be switched off.
Area chart: Visitors, 1 series, x from 10 to 60. Visitors: 60 points, minimum 644, maximum 1,207, trending up.
| x | Visitors |
|---|---|
| 1 | 800 |
| 2 | 854 |
| 3 | 905 |
| 4 | 953 |
| 5 | 995 |
| 6 | 1,030 |
| 7 | 1,057 |
| 8 | 1,074 |
| 9 | 1,082 |
| 10 | 1,079 |
| 11 | 1,067 |
| 12 | 1,046 |
| 13 | 1,017 |
| 14 | 981 |
| 15 | 940 |
| 16 | 895 |
| 17 | 849 |
| 18 | 804 |
| 19 | 761 |
| 20 | 723 |
| 21 | 691 |
| 22 | 666 |
| 23 | 650 |
| 24 | 644 |
| 25 | 647 |
| 26 | 660 |
| 27 | 683 |
| 28 | 715 |
| 29 | 754 |
| 30 | 800 |
| 31 | 850 |
| 32 | 903 |
| 33 | 957 |
| 34 | 1,010 |
| 35 | 1,060 |
| 36 | 1,104 |
| 37 | 1,142 |
| 38 | 1,173 |
| 39 | 1,194 |
| 40 | 1,206 |
| 41 | 1,207 |
| 42 | 1,199 |
| 43 | 1,182 |
| 44 | 1,156 |
| 45 | 1,122 |
| 46 | 1,083 |
| 47 | 1,040 |
| 48 | 994 |
| 49 | 948 |
| 50 | 904 |
| 51 | 864 |
| 52 | 829 |
| 53 | 801 |
| 54 | 781 |
| 55 | 771 |
| 56 | 770 |
| 57 | 779 |
| 58 | 798 |
| 59 | 826 |
| 60 | 863 |
Column chart: Orders, 1 series, x from 10 to 60. Orders: 60 points, minimum 30, maximum 64, trending up.
| x | Orders |
|---|---|
| 1 | 48 |
| 2 | 51 |
| 3 | 54 |
| 4 | 55 |
| 5 | 56 |
| 6 | 56 |
| 7 | 57 |
| 8 | 56 |
| 9 | 54 |
| 10 | 52 |
| 11 | 51 |
| 12 | 48 |
| 13 | 45 |
| 14 | 42 |
| 15 | 40 |
| 16 | 37 |
| 17 | 35 |
| 18 | 33 |
| 19 | 32 |
| 20 | 31 |
| 21 | 30 |
| 22 | 30 |
| 23 | 32 |
| 24 | 33 |
| 25 | 34 |
| 26 | 37 |
| 27 | 40 |
| 28 | 43 |
| 29 | 46 |
| 30 | 49 |
| 31 | 53 |
| 32 | 55 |
| 33 | 58 |
| 34 | 60 |
| 35 | 62 |
| 36 | 64 |
| 37 | 64 |
| 38 | 64 |
| 39 | 64 |
| 40 | 63 |
| 41 | 61 |
| 42 | 59 |
| 43 | 57 |
| 44 | 54 |
| 45 | 51 |
| 46 | 48 |
| 47 | 47 |
| 48 | 44 |
| 49 | 42 |
| 50 | 40 |
| 51 | 39 |
| 52 | 38 |
| 53 | 38 |
| 54 | 38 |
| 55 | 40 |
| 56 | 42 |
| 57 | 44 |
| 58 | 46 |
| 59 | 50 |
| 60 | 53 |
Selected days: visitors none, orders none
Demo.tsx
'use client';
import { useState } from 'react';
import { Checkbox } from '@salmonui/inputs/checkbox';
import { Chart, ChartSyncGroup } from '@salmonui/charts/chart';
import type { ChartPointRef } from '@salmonui/charts/chart';
const days = Array.from({ length: 60 }, (_, d) => ({
day: d + 1,
visitors: 800 + Math.round(250 * Math.sin(d / 5)) + d * 4,
orders: 40 + Math.round(15 * Math.sin(d / 5 + 0.6)) + Math.round(d / 4),
}));
export default function ChartSyncDemo() {
const [tooltip, setTooltip] = useState(true);
const [crosshair, setCrosshair] = useState(true);
const [zoom, setZoom] = useState(true);
const [selection, setSelection] = useState(true);
const [visitorSel, setVisitorSel] = useState<readonly ChartPointRef[]>([]);
const [orderSel, setOrderSel] = useState<readonly ChartPointRef[]>([]);
return (
<div className="demo-stack">
<div className="demo-toolbar">
<Checkbox checked={tooltip} onCheckedChange={(checked) => setTooltip(checked)}>Sync tooltip</Checkbox>
<Checkbox checked={crosshair} onCheckedChange={(checked) => setCrosshair(checked)}>Sync crosshair</Checkbox>
<Checkbox checked={zoom} onCheckedChange={(checked) => setZoom(checked)}>Sync zoom</Checkbox>
<Checkbox checked={selection} onCheckedChange={(checked) => setSelection(checked)}>Sync selection</Checkbox>
</div>
<ChartSyncGroup tooltip={tooltip} crosshair={crosshair} zoom={zoom} selection={selection}>
<Chart
title="Visitors"
data={days}
height={200}
legend={false}
tooltip
crosshair={{ lines: 'x' }}
zoom={{ mode: 'x' }}
selection
selectedPoints={visitorSel}
onSelectedPointsChange={setVisitorSel}
series={[{ type: 'area', name: 'Visitors', xField: 'day', yField: 'visitors' }]}
/>
<Chart
title="Orders"
data={days}
height={200}
legend={false}
tooltip
crosshair={{ lines: 'x' }}
zoom={{ mode: 'x' }}
selection
selectedPoints={orderSel}
onSelectedPointsChange={setOrderSel}
series={[{ type: 'column', name: 'Orders', xField: 'day', yField: 'orders' }]}
/>
</ChartSyncGroup>
<p className="demo-status" aria-live="polite">
Selected days: visitors <code>{visitorSel.map((p) => p.index + 1).join(', ') || 'none'}</code>, orders{' '}
<code>{orderSel.map((p) => p.index + 1).join(', ') || 'none'}</code>
</p>
</div>
);
}
Hover one chart and the other shows its tooltip and crosshair at the
same day; zoom one (drag or wheel) and the other follows; click a point
and the other chart proposes the same index through
onSelectedPointsChange with reason: 'sync'. The group's tooltip,
crosshair, zoom and selection props pick what is shared. Each group
instance has its own store, so several groups on a page stay independent.