Coordinate Systems in Recharts

When working with Recharts, you'll encounter three different coordinate systems. Understanding when to use each one is essential for creating custom annotations, shapes, and interactive features.

Different Rechart components accept, or provide, coordinate from different systems.

Overview

Coordinate SystemDescriptionExample components
Domain CoordinatesValues in your data domain (e.g., x="March", y=5000) can be provided directly to some components and are converted to pixel positions automatically.ReferenceDot, ReferenceLine, ReferenceArea
Pixel/Chart-Range CoordinatesPixel positions relative to the chart's viewBox (e.g., x=100, y=50). The top-left corner of the chart area is (0, 0).Dot, Rectangle, Cross, custom SVG shapes
Browser eventsMouse and touch events have several of their own coordinate systems: viewport, page, client, screen. Recharts provides helper methods to convert to chart-based coordinates.Mouse and touch handlers

1. Domain Coordinates

Domain coordinates are the most intuitive—they use values from your actual data. When you specify x="March" on a ReferenceLine, Recharts automatically converts that to the correct pixel position based on your XAxis and YAxis scale.

You can also optionally pass position prop. This is important for Bar charts, where the axis scale typically has bandwidth: meaning that a single data point spans multiple pixels. The position prop controls where within that bandwidth the element is placed (e.g., start, middle, end).

Other charts, like Line charts, have no bandwidth (each data point maps to a single pixel). In those cases, the position prop is ignored.

Components that use domain coordinates:

Advantages:

  • Automatically adjusts when chart size changes
  • Responds to axis domain changes (zoom, brush filtering)
  • Works with both categorical and numerical axes

2. Pixel/Chart-Range Coordinates

Pixel coordinates are positions measured in pixels from the top-left corner of the chart. Use the useOffset() hook to get the plot area dimensions, then position elements relative to that area.

Key hooks:

When to use:

  • Annotations that should stay at fixed positions regardless of data
  • Decorative elements like watermarks or logos
  • Custom legend, tooltip, or label positioning

3. Mouse Event Coordinates

Mouse events from the browser provide coordinates in the browser's viewport. The getRelativeCoordinate function converts these to chart-relative coordinates, accounting for:

  • Chart position on the page
  • Scroll offset
  • CSS transforms (scale, rotate)
  • Browser zoom in or zoom out

Key function:

This function works with both HTML elements (like <div>) and SVG elements. The returned coordinates are relative to the top-left corner of the element that received the event.

Converting Between Coordinate Systems

Often you need to convert between coordinate systems. For example, you might want to position a custom marker at a specific data point, or determine which data point the user clicked on.

Data → Pixels:

  • useXAxisScale - returns a function to convert X data values to pixel positions
  • useYAxisScale - returns a function to convert Y data values to pixel positions
  • useCartesianScale - convenience hook for converting both at once

Pixels → Data:

Accessing Ticks:

Choosing the Right Coordinate System

I want to...Use
Mark a specific data valueData coordinates (ReferenceArea,ReferenceLine, ReferenceDot)
Show a target line that moves with zoom/brushData coordinates (ReferenceLine)
Add a watermark at a fixed positionPixel coordinates (useOffset + custom SVG)
Create a crosshair that follows the mouseMouse coordinates (getRelativeCoordinate)
Click to add an annotation at a data pointMouse → Data conversion (getRelativeCoordinate + useXAxisInverseScale)
Draw custom shapes at data positionsData → Pixel conversion (useXAxisScale + useYAxisScale)