Crosshair modes
Crosshair
A crosshair that moves freely, snaps to prices, or is hidden, switched with buttons.
Interactive example — scroll, zoom, and hover the chart.
Code
// Lightweight Charts™ Example: Crosshair modes
// https://tradingview.github.io/lightweight-charts/examples/crosshair/crosshair-modes
const container = document.getElementById('container');
// The chart takes the space above the buttons, which are added further down.
container.style.display = 'flex';
container.style.flexDirection = 'column';
const chartElement = document.createElement('div');
chartElement.style.flex = '1';
chartElement.style.minHeight = '0';
container.appendChild(chartElement);
/** @type {import('lightweight-charts').IChartApi} */
const chart = createChart(chartElement, chartOptions);
const series = chart.addSeries(CandlestickSeries, {
upColor: '#26a69a',
downColor: '#ef5350',
borderVisible: false,
wickUpColor: '#26a69a',
wickDownColor: '#ef5350',
});
series.setData(generateBars());
chart.timeScale().fitContent();
function setMode(mode) {
chart.applyOptions({ crosshair: { mode } });
}
const modes = [
['Normal', CrosshairMode.Normal],
['Magnet', CrosshairMode.Magnet],
['Magnet OHLC', CrosshairMode.MagnetOHLC],
['Hidden', CrosshairMode.Hidden],
];
modes.forEach(([label, mode]) => {
const button = document.createElement('button');
button.innerText = label;
button.addEventListener('click', () => setMode(mode));
buttonsContainer.appendChild(button);
});
container.appendChild(buttonsContainer);
// Start in magnet OHLC mode, which snaps to the nearest of the four prices.
setMode(CrosshairMode.MagnetOHLC);
// Sample data: daily bars, a seeded random walk, so the chart looks the same
// on every load.
function generateBars(count = 60) {
let seed = 19;
const random = () => (seed = (seed * 16807) % 2147483647) / 2147483647;
const bars = [];
let time = Date.UTC(2024, 0, 1) / 1000;
let close = 100;
for (let i = 0; i < count; i++) {
const open = close;
close = open + (random() - 0.48) * 4;
const high = Math.max(open, close) + random() * 2;
const low = Math.min(open, close) - random() * 2;
bars.push({ time, open, high, low, close });
time += 24 * 60 * 60;
}
return bars;
}
The code expects the library’s exports, such as createChart, to be imported and a div with the id container on the page. Copy as HTML gives a page that runs on its own.
How it works
The crosshair mode decides where the horizontal line of the crosshair goes. Hover the chart after each button to see the difference.
- The buttons call
setMode, which passescrosshair.modeto the chart'sapplyOptions. CrosshairModelists the modes:Normalfollows the cursor freely;Magnetsnaps to the close price of the bar;MagnetOHLCsnaps to the open, high, low, or close, whichever is nearest to the cursor;Hiddendoes not draw the crosshair.
The chart starts in MagnetOHLC mode. The call is highlighted in the code.
Tips
- On a line or area series, both magnet modes snap to the value of the point.
Hiddenonly stops the drawing.subscribeCrosshairMovestill reports the cursor, so a custom tooltip keeps working.- To style the lines instead, set
crosshair.vertLineandcrosshair.horzLine, for example theircolor,style, andlabelVisible.
Resources
CrosshairOptionsCrosshairMode- Related examples: Sync crosshairs, Floating tooltip