Skip to content
EliteChart

nearestBar

Documentation


Documentation / @elitechart/core / nearestBar

code
function nearestBar(bars, t): Bar | null;

Defined in: renderer/ohlcv-legend.ts:71

Find the bar whose time is closest to t. null when bars is empty. Used when hovering — the cursor position maps to a time; we return the nearest bar's OHLCV to display.

Parameters

bars

readonly Bar[]

t

number

Returns

Bar | null

Example

code
import { nearestBar } from '@elitechart/core';
const hovered = nearestBar(bars, viewport.xToTime(cursorX) as unknown as number);
if (hovered !== null) showTooltip(hovered);