Styling data points
Most charts draw one shape per data entry: one bar, one pie slice, one scatter point. Instead of styling the whole series, you can style each shape by putting SVG props right into the data array.
There is no extra component to add and nothing to configure. If the entry has a fill, that bar is drawn with that fill.
Which components support it
| Component | The props go on |
|---|---|
| Bar | the bar rectangle |
| RadialBar | the sector |
| Pie | the slice |
| Funnel | the trapezoid |
| Scatter | the point symbol |
| Line, Area, Radar | the dot for that entry (the line or area itself is one shape for the whole series, so it is styled with props on the component) |
| Treemap | the node rectangle |
| SunburstChart | the sector |
| Sankey | the node (entries in data.nodes) and the link (entries in data.links) |
Data entry properties work in all of these components since Recharts 3.11. In older versions, support was incomplete.
What you can put in an entry
Any valid SVG attribute: fill, stroke, strokeWidth, strokeDasharray, fillOpacity, strokeOpacity, opacity, and so on. You can also use className, style and data-* attributes, for example to hook up your own CSS or tests.
Keys that are not SVG attributes are ignored, so your entries can carry any other fields your app needs.
There are two exceptions:
idis always removed. One entry can end up on more than one element (a Pie slice and its label, for example), and an HTML id must be unique on the page.- Position comes from Recharts. Recharts computes
x,y,width,height,cxandcyfrom your values and the axes, and these win over the same keys in an entry. Size props that the shape reads directly still apply, such asron a dot (see the Line example below).
Watch out for name clashes. Because the props are read from the same object as your values, a field that happens to be named like an SVG attribute (fill,name,color,radius,opacity…) styles the shape even if you meant it as data. If your data has such a field, rename it or map your data before passing it to the chart.
How data props combine with component props
For each shape, Recharts takes the props in this order, and the first one that sets a property wins:
- the data entry
- the props on the component (
<Bar fill="…" stroke="…" />) - the theme, or the legacy defaults if there is no theme (see below)
Entry props and component props merge. With <Bar fill="#8884d8" stroke="#333" /> and an entry { fill: 'red' }, that bar is red with a #333 outline. With { stroke: 'lime' }, it keeps the #8884d8 fill and gets a lime outline.
One exception: a style prop is CSS, and CSS beats SVG attributes. A fill in <Bar style={{ fill: 'blue' }} /> overrides the fill of every entry.
With a theme
A theme provides colors as a matched set, so they are not mixed with yours. For colors (fill and stroke) the rule is:
- If neither the entry nor the component sets a color, the shape uses the theme colors.
- If either of them sets a
fillor astroke, the theme colors are not used for that shape at all. You get your colors and nothing else.
Props that are not colors, such as strokeWidth, strokeDasharray or opacity, do not count. { strokeDasharray: '4 2' } gives a dashed shape in the theme colors.
Note what this means for an entry that sets only one color. { stroke: 'red' } on a Bar in a themed chart, with no fill on the <Bar>, draws a bar with a red outline and the SVG default fill, which is black. For an outline only, add fill: 'none'. If you want both colors, set both.
With a theme, the Tooltip shows the color the shape is actually drawn with, and so does the Legend for components that have one Legend item per entry:
- For
Bar,RadialBar,Pie,Funnel,Scatter,Treemap,SunburstChartandSankey, the Tooltip shows the color of the hovered entry: itsfill, or itsstrokeif it has no fill. - For
Line,AreaandRadar, the Tooltip shows the color of the series, because the line is one shape. Styled dots do not change it. Pie,RadialBarandFunnelshow one Legend item per entry, in that entry's color. The other components show one Legend item per series.
Without a theme
Data props work the same way: they win over the component props, and anything they do not set comes from the component props or from the component's own default (for example the grey #808080 of a Pie slice).
In this mode, Tooltip and Legend colors are not guaranteed to match per-entry styles. If that matters to you, add a theme.
Styled dots on Line, Area and Radar
The entry props go on that entry's dot. The line keeps its series color. A dot element or function you pass yourself receives the entry in its props and decides on its own what to render.
When to use shape instead
Data props are static values. If a style depends on something only known at render time, such as whether the shape is active or the value compared to a threshold computed elsewhere, use the shape prop (or content on Treemap) and set the props in your own component. This is also the replacement for the deprecated Cell.