useXAxisInverseDataSnapScale

Returns a function to convert pixel coordinates back to data values for an XAxis, but snapping to the closest data point.

This is similar to useXAxisInverseScale, but instead of returning the exact data value at the pixel position (interpolation), it returns the value of the closest data point.

This is useful for implementing interactions where you want to select the closest data point rather than an exact value or a tick.

This hook must be used within a chart context (inside a LineChart, BarChart, etc.). Returns undefined if used outside a chart context, or if the axes don't exist.

Available since Recharts 3.8

Return value

InverseScaleFunction | undefined

An inverse scale function that maps pixel coordinates to the closest data value, or undefined.

Props

  • xAxisIdnumber | stringoptional

    The xAxisId of the XAxis. Defaults to 0 if not provided.

    DEFAULT: "defaultAxisId"