Skip to main content
Hooks for converting between data values and pixel coordinates.

useXAxisScale

Returns a function to convert data values to pixel coordinates for an X-axis. Useful for positioning annotations, custom shapes, or other elements at specific data points on the chart.
AxisId
default:"0"
The ID of the X-axis. Defaults to 0 if not provided.
ScaleFunction | undefined
A scale function that maps data values to pixel coordinates, or undefined if used outside a chart context or if the axis doesn’t exist.
Since: 3.8

useYAxisScale

Returns a function to convert data values to pixel coordinates for a Y-axis. Useful for positioning annotations, custom shapes, or other elements at specific data points on the chart.
AxisId
default:"0"
The ID of the Y-axis. Defaults to 0 if not provided.
ScaleFunction | undefined
A scale function that maps data values to pixel coordinates, or undefined if used outside a chart context or if the axis doesn’t exist.
Since: 3.8

useCartesianScale

Converts a data point (in data coordinates) to pixel coordinates. This is a convenience hook that combines useXAxisScale and useYAxisScale together in a single call.
CartesianDataPoint
required
The data point with x and y values in data coordinates.
AxisId
default:"0"
The ID of the X-axis. Defaults to 0 if not provided.
AxisId
default:"0"
The ID of the Y-axis. Defaults to 0 if not provided.
Coordinate | undefined
Object with pixel x and y coordinates, or undefined if conversion is not possible.
Since: 3.8

Inverse scale hooks

Convert pixel coordinates back to data values.

useXAxisInverseScale

Returns a function to convert pixel coordinates back to data values for an X-axis. Useful for implementing interactions like click-to-add-annotation, drag interactions, or tooltips that need to determine what data point corresponds to a mouse position.
AxisId
default:"0"
The ID of the X-axis. Defaults to 0 if not provided.
InverseScaleFunction | undefined
An inverse scale function that maps pixel coordinates to data values, or undefined.
Since: 3.8 Behavior:
  • For continuous (numerical) scales: returns an interpolated value
  • For categorical scales: returns the closest category in the domain (same as useXAxisInverseDataSnapScale)

useYAxisInverseScale

Returns a function to convert pixel coordinates back to data values for a Y-axis.
AxisId
default:"0"
The ID of the Y-axis. Defaults to 0 if not provided.
InverseScaleFunction | undefined
An inverse scale function that maps pixel coordinates to data values, or undefined.
Since: 3.8

Snap scale hooks

Snap to the closest data point or tick instead of interpolating.

useXAxisInverseDataSnapScale

Returns a function to convert pixel coordinates back to data values, snapping to the closest data point. 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.
AxisId
default:"0"
The ID of the X-axis. Defaults to 0 if not provided.
InverseScaleFunction | undefined
An inverse scale function that maps pixel coordinates to the closest data value, or undefined.
Since: 3.8

useYAxisInverseDataSnapScale

Returns a function to convert pixel coordinates back to data values for a Y-axis, snapping to the closest data point.
AxisId
default:"0"
The ID of the Y-axis. Defaults to 0 if not provided.
InverseScaleFunction | undefined
An inverse scale function that maps pixel coordinates to the closest data value, or undefined.
Since: 3.8

useXAxisInverseTickSnapScale

Returns a function to convert pixel coordinates back to data values, snapping to the closest axis tick. Similar to useXAxisInverseScale, but instead of returning the exact data value at the pixel position (interpolation), it returns the value of the closest tick.
AxisId
default:"0"
The ID of the X-axis. Defaults to 0 if not provided.
InverseScaleFunction | undefined
An inverse scale function that maps pixel coordinates to the closest tick value, or undefined.
Since: 3.8

useYAxisInverseTickSnapScale

Returns a function to convert pixel coordinates back to data values for a Y-axis, snapping to the closest axis tick.
AxisId
default:"0"
The ID of the Y-axis. Defaults to 0 if not provided.
InverseScaleFunction | undefined
An inverse scale function that maps pixel coordinates to the closest tick value, or undefined.
Since: 3.8

Types

ScaleFunction

A function that converts data values to pixel coordinates.
  • value - The data value to convert (number, string, or category)
  • options.position - Position within a band: ‘start’, ‘middle’, or ‘end’
  • Returns the pixel coordinate, or undefined if the value is not in the domain

InverseScaleFunction

A function that converts pixel coordinates back to data values.
  • pixelValue - The pixel coordinate to convert
  • Returns the closest data value in the domain

CartesianDataPoint

Data point with x and y values that can be converted to pixel coordinates. The x and y values should be in the same format as your chart data.

Coordinate