useChartScrub

Connect the hook to onInteraction and read the current selection outside the plot.

tsx
import { Chart, useChartScrub, zyplot } from '@hzblj/zyplot'

export function PriceChart({ categories, values }) {
  const { onInteraction, selection } = useChartScrub()
  const value = selection ? values[selection.index] : values.at(-1)
  const chart = zyplot(z => ({
    categories,
    series: [z.series({ id: 'price', label: 'Price', values })],
    interaction: z.interaction.scrub({ selection: 'single' }),
    onInteraction,
  }))

  return (
    <>
      <strong>{value}</strong>
      <Chart.Line {...chart} />
    </>
  )
}

Returned values

NameTypeDefaultDescription
selectionChartScrubSelection | null—Current datum index, category, and value.
rangeChartInteractionRange | null—Two-finger range on native.
geometryChartGeometry | null—Measured plot and annotation positions.
onInteractionChartInteractionHandler—Pass directly to the chart.
reset() => void—Clear the current selection.