Demo
Selection, highlight, point events and editing
Documented in Chart interaction.
Selection, highlight, point events and editing
Point, series and cluster selection with a controlled selectedPoints list, hover highlight, styling hooks, point and plot events, range and lasso selection on a scatter chart, and a line whose points are edited by drag or keyboard.
Column chart: Sales by region, 3 series, x from Q1 to Q6. North: 6 points, minimum 40, maximum 55, trending up. South: 6 points, minimum 30, maximum 55, trending up. West: 6 points, minimum 22, maximum 37, trending up.
| x | North | South | West |
|---|---|---|---|
| Q1 | 40 | 30 | 22 |
| Q2 | 53 | 47 | 29 |
| Q3 | 41 | 34 | 36 |
| Q4 | 54 | 51 | 23 |
| Q5 | 42 | 38 | 30 |
| Q6 | 55 | 55 | 37 |
selectedPoints: none
Scatter chart: Samples, 1 series, x from 0 to 80. Samples: 60 points, minimum 0, maximum 99, trending up.
| x | Samples |
|---|---|
| 0 | 0 |
| 37 | 61 |
| 74 | 22 |
| 11 | 83 |
| 48 | 44 |
| 85 | 5 |
| 22 | 66 |
| 59 | 27 |
| 96 | 88 |
| 33 | 49 |
| 70 | 10 |
| 7 | 71 |
| 44 | 32 |
| 81 | 93 |
| 18 | 54 |
| 55 | 15 |
| 92 | 76 |
| 29 | 37 |
| 66 | 98 |
| 3 | 59 |
| 40 | 20 |
| 77 | 81 |
| 14 | 42 |
| 51 | 3 |
| 88 | 64 |
| 25 | 25 |
| 62 | 86 |
| 99 | 47 |
| 36 | 8 |
| 73 | 69 |
| 10 | 30 |
| 47 | 91 |
| 84 | 52 |
| 21 | 13 |
| 58 | 74 |
| 95 | 35 |
| 32 | 96 |
| 69 | 57 |
| 6 | 18 |
| 43 | 79 |
| 80 | 40 |
| 17 | 1 |
| 54 | 62 |
| 91 | 23 |
| 28 | 84 |
| 65 | 45 |
| 2 | 6 |
| 39 | 67 |
| 76 | 28 |
| 13 | 89 |
| 50 | 50 |
| 87 | 11 |
| 24 | 72 |
| 61 | 33 |
| 98 | 94 |
| 35 | 55 |
| 72 | 16 |
| 9 | 77 |
| 46 | 38 |
| 83 | 99 |
Line chart: Planned units (drag a point, or focus the plot and press Up / Down), 1 series, x from Q1 to Q6. Units: 6 points, minimum 12, maximum 25, trending up.
| x | Units |
|---|---|
| Q1 | 12 |
| Q2 | 18 |
| Q3 | 15 |
| Q4 | 22 |
| Q5 | 19 |
| Q6 | 25 |
Demo.tsx
'use client';
import { useState } from 'react';
import { Button } from '@salmonui/buttons/button';
import { Checkbox } from '@salmonui/inputs/checkbox';
import { Select } from '@salmonui/dropdowns/select';
import { Chart } from '@salmonui/charts/chart';
import type { ChartPointRef } from '@salmonui/charts/chart';
const quarters = ['Q1', 'Q2', 'Q3', 'Q4', 'Q5', 'Q6'];
const regions = quarters.map((quarter, i) => ({ quarter, north: 40 + ((i * 13) % 25), south: 30 + ((i * 17) % 30), west: 22 + ((i * 7) % 20) }));
const samples = Array.from({ length: 60 }, (_, i) => ({ x: (i * 37) % 100, y: (i * 61) % 100 }));
type Mode = 'point' | 'series' | 'cluster' | 'range' | 'lasso';
type Highlight = 'off' | 'series' | 'point' | 'cluster';
// Selected, dimmed and highlighted marks carry data attributes for className overrides.
const css = `.demo-chart-selection [data-selected] { stroke: var(--salmon-text-primary); stroke-width: 2px; }
.demo-chart-selection [data-dimmed] { opacity: 0.25; }`;
export default function ChartSelectionDemo() {
const [mode, setMode] = useState<Mode>('point');
const [multiple, setMultiple] = useState(true);
const [highlight, setHighlight] = useState<Highlight>('series');
const [selected, setSelected] = useState<readonly ChartPointRef[]>([]);
const [scatterSelected, setScatterSelected] = useState<readonly ChartPointRef[]>([]);
const [units, setUnits] = useState([12, 18, 15, 22, 19, 25]);
const [log, setLog] = useState<string[]>([]);
const add = (line: string) => setLog((l) => [line, ...l].slice(0, 6));
const boxMode = mode === 'range' || mode === 'lasso';
return (
<div className="demo-stack demo-chart-selection">
<style>{css}</style>
<div className="demo-toolbar">
<label>
Column selection mode
<Select aria-label="Column selection mode" data={[{ value: 'point', label: 'point' }, { value: 'series', label: 'series' }, { value: 'cluster', label: 'cluster' }]} value={boxMode ? 'point' : mode} onValueChange={(value) => value != null && setMode(value as Mode)} />
</label>
<Checkbox checked={multiple} onCheckedChange={(checked) => setMultiple(checked)}>Multiple</Checkbox>
<label>
Hover highlight
<Select aria-label="Hover highlight" data={[{ value: 'off', label: 'false' }, { value: 'series', label: 'series' }, { value: 'point', label: 'point' }, { value: 'cluster', label: 'cluster' }]} value={highlight} onValueChange={(value) => value != null && setHighlight(value as Highlight)} />
</label>
<Button variant="outline" type="button" onClick={() => setSelected([])}>
Clear selection
</Button>
</div>
<Chart
title="Sales by region"
data={regions}
height={280}
selection={{ mode: boxMode ? 'point' : mode, multiple }}
selectedPoints={selected}
onSelectedPointsChange={(points, event) => {
setSelected(points);
add(`onSelectedPointsChange: ${points.length} points (${event.reason})`);
}}
highlight={highlight === 'off' ? false : highlight}
legend={{ highlight: true }}
onPointClick={(p) => add(`onPointClick: ${p.seriesName} ${p.xText} = ${p.yText}`)}
onPointDoubleClick={(p) => add(`onPointDoubleClick: ${p.seriesName} ${p.xText}`)}
onPointHover={(p) => {
if (p) add(`onPointHover: ${p.seriesName} ${p.xText}`);
}}
onPlotClick={(p) => add(`onPlotClick: x ${String(p.xValue)}, y ${typeof p.yValue === 'number' ? p.yValue.toFixed(1) : String(p.yValue)}`)}
onContextMenu={(e) => {
e.preventDefault();
add('onContextMenu on the chart root');
}}
series={[
{ type: 'column', name: 'North', xField: 'quarter', yField: 'north' },
{ type: 'column', name: 'South', xField: 'quarter', yField: 'south' },
{ type: 'column', name: 'West', xField: 'quarter', yField: 'west' },
]}
/>
<p className="demo-status" aria-live="polite">
selectedPoints: <code>{selected.map((p) => `${p.seriesId}[${p.index}]`).join(', ') || 'none'}</code>
</p>
<div className="demo-toolbar">
<label>
Scatter selection
<Select aria-label="Scatter selection" data={[{ value: 'range', label: 'range (drag a rectangle)' }, { value: 'lasso', label: 'lasso (draw a shape)' }]} value={boxMode ? mode : 'range'} onValueChange={(value) => value != null && setMode(value as Mode)} />
</label>
</div>
<Chart
title="Samples"
data={samples}
height={260}
legend={false}
selection={{ mode: mode === 'lasso' ? 'lasso' : 'range', axis: 'xy', multiple: true }}
selectedPoints={scatterSelected}
onSelectedPointsChange={setScatterSelected}
onRangeSelect={({ ranges, points }, event) => add(`onRangeSelect (${event.reason}): ${points.length} points${ranges.x ? `, x ${ranges.x.map((v) => v.toFixed(0)).join('–')}` : ''}`)}
series={[{ type: 'scatter', name: 'Samples', xField: 'x', yField: 'y' }]}
/>
<Chart
title="Planned units (drag a point, or focus the plot and press Up / Down)"
data={units.map((value, i) => ({ quarter: quarters[i], value }))}
height={240}
legend={false}
yAxis={{ min: 0, max: 40 }}
series={[{ type: 'line', name: 'Units', xField: 'quarter', yField: 'value', markers: true, editable: { min: 0, max: 40, step: 1 } }]}
onPointChange={({ index, value }, event) => {
setUnits((u) => u.map((v, i) => (i === index ? Math.round(value) : v)));
add(`onPointChange: ${quarters[index]} = ${Math.round(value)} (${event.reason})`);
}}
/>
<div className="demo-log" aria-label="Chart events">
<ol>
{log.map((line, i) => (
<li key={`${i}-${line}`}>{line}</li>
))}
</ol>
</div>
</div>
);
}
Click columns (Ctrl / Cmd adds with multiple, Enter selects the focused
point). selection.mode picks 'point', 'series' or 'cluster'; the
selection is the controlled selectedPoints triad, shown below the chart.
Selected marks carry data-selected and dimmed ones data-dimmed; the
demo styles them with a class. highlight fades everything but the
hovered series, point or cluster, and legend.highlight dims the other
series while a legend item is hovered.
The event log shows onPointClick, onPointDoubleClick, onPointHover,
onPlotClick (any click on the plot, with data values) and the root
onContextMenu DOM handler.
On the scatter chart, drag a rectangle (mode: 'range') or draw a lasso
(mode: 'lasso'); onRangeSelect reports the ranges and points.
The last chart sets series.editable: { min, max, step }: drag a point or
focus the plot and press Up / Down. onPointChange proposes the value and
the demo writes it into its data.