Hook

useLastReading

A series is often shorter than its axis on purpose — a trading session still in progress, a forecast that has not started — so its last slot is not its last reading. This walks back to where the data really ends, which is where a "now" marker belongs. It returns null when the series has no readings at all. Unlike useChartScrub it reads nothing but the arrays you pass it, so it is on every entry point, web included.

tsx
const live = useLastReading(range.categories, range.values)

<Chart.Line
  annotations={
    live
      ? [
          annotation.point({
            color: '#ffffff',
            glow: glow({ color: '#ff3b4a', opacity: 0.25, radius: 3 }),
            halo: halo({ color: '#ff3b4a', size: 11 }),
            id: 'live',
            pulse: true,
            size: 4.7,
            x: live.category,
            y: live.value,
          }),
        ]
      : []
  }
  categories={range.categories}
  series={[{ id: 'price', label: 'Price', values: range.values }]}
/>

Arguments

PropTypeDefaultDescription
categories*readonly string[]The categories the series is plotted against.
values*readonly (number | null)[]The series values. Trailing null entries are skipped, as are gaps.
tsx
type ChartReading = {
  category: string
  index: number
  value: number
}
The native renderers already agree with it. A scrub stops at the same point this returns, so a "now" marker placed here is exactly where the finger can no longer go — the chart and the marker cannot disagree about where the data ends.