Crosshair
The crosshair, its modes (normal, magnet, hidden) and axis labels.
"use client";
import { ChartCanvas, Pane } from "@reincharts/core";
import { LineSeries } from "@reincharts/series";
import { Grid, XAxis, YAxis } from "@reincharts/axes";
import { Crosshair, type CrosshairMode } from "@reincharts/coordinates";
import { useState } from "react";
import { Segmented, WithToolbar } from "@/components/preview/controls";
import { weather, type WeatherDay } from "@/lib/data-general";
import { useDocsChartTheme } from "@/lib/use-docs-theme";
export default function CrosshairModes() {
const theme = useDocsChartTheme();
const [mode, setMode] = useState<CrosshairMode>("magnet");
return (
<WithToolbar
toolbar={
<Segmented
label="mode"
value={mode}
options={["normal", "magnet", "hidden"] as const}
onChange={setMode}
/>
}
>
<ChartCanvas data={weather} theme={theme} barSpacing={10}>
<Pane>
<Grid />
<LineSeries<WeatherDay> name="High" y="high" color="#f97316" width={1} />
<LineSeries<WeatherDay> name="Mean" y="temp" />
<LineSeries<WeatherDay> name="Low" y="low" color="#06b6d4" width={1} />
<YAxis tickFormat={(t) => `${t.label}°`} />
</Pane>
<XAxis />
{/* magnet: the horizontal line snaps to the nearest line value at the hovered day */}
<Crosshair mode={mode} />
</ChartCanvas>
</WithToolbar>
);
}Usage
Place one <Crosshair> directly inside <ChartCanvas>: its vertical line spans every pane, the horizontal line and
the price labels follow the hovered pane, and the time label sits on the x axis.
import { Crosshair } from "@reincharts/coordinates";
<ChartCanvas data={bars}>
<Pane>{/* ... */}</Pane>
<XAxis />
<Crosshair mode="magnet" />
</ChartCanvas>;Modes
| Mode | Behavior |
|---|---|
"normal" | Lines follow the pointer; x snaps to the hovered bar in index mode. |
"magnet" | The horizontal line snaps to the closest series value of the bar (e.g. the nearest line, or open/high/low/close of a candle). |
"hidden" | Nothing is drawn, but the crosshair state (legends, tooltips, sync) still updates. |
Turn individual parts off with vertical, horizontal, xLabel and yLabel, and format the labels with formatX /
formatY. Charts that share a syncId show a synced vertical line (Syncing charts).
The crosshair is drawn on the chart's "top" layer, so moving the pointer never redraws the series.