Cartesian Grid
The Cartesian Grid is a component in Recharts that provides a visual reference for data points in charts. It helps users to better interpret the data by adding horizontal and vertical lines across the chart area.
Simple Example
The stroke_dasharray
prop in Recharts is used to create dashed or dotted lines for various chart elements like lines, axes, or grids. It's based on the SVG stroke-dasharray attribute. The stroke_dasharray
prop accepts a comma-separated string of numbers that define a repeating pattern of dashes and gaps along the length of the stroke.
stroke_dasharray="5,5"
: creates a line with 5-pixel dashes and 5-pixel gapsstroke_dasharray="10,5,5,5"
: creates a more complex pattern with 10-pixel dashes, 5-pixel gaps, 5-pixel dashes, and 5-pixel gaps
Here's a simple example using it on a Line component:
Hidden Axes
A cartesian_grid
component can be used to hide the horizontal and vertical grid lines in a chart by setting the horizontal
and vertical
props to False
. This can be useful when you want to show the grid lines only on one axis or when you want to create a cleaner look for the chart.
Custom Grid Lines
The horizontal_points
and vertical_points
props allow you to specify custom grid lines on the chart, offering fine-grained control over the grid's appearance.
These props accept arrays of numbers, where each number represents a pixel offset:
- For
horizontal_points
, the offset is measured from the top edge of the chart - For
vertical_points
, the offset is measured from the left edge of the chart
Important: The values provided to these props are not directly related to the axis values. They represent pixel offsets within the chart's rendering area.
Here's an example demonstrating custom grid lines in a scatter chart:
Use these props judiciously to enhance data visualization without cluttering the chart. They're particularly useful for highlighting specific data ranges or creating visual reference points.
API Reference
rx.recharts.CartesianGrid
A CartesianGrid component in Recharts.