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.

import { BarChart, Bar, XAxis, YAxis } from 'recharts';

const data = [
  { name: 'Mon', sales: 120 },
  { name: 'Tue', sales: 180, fill: '#e11d48' }, // this bar is red
  { name: 'Wed', sales: 90, strokeDasharray: '4 2', stroke: '#e11d48', fill: 'none' },
];

const chartStyle = { width: '100%', maxWidth: '500px', aspectRatio: 1.618 };

export default function DataStylesBarExample({ isAnimationActive = true }: { isAnimationActive?: boolean }) {
  return (
    <BarChart style={chartStyle} responsive data={data}>
      <XAxis dataKey="name" />
      <YAxis />
      <Bar dataKey="sales" fill="#8884d8" isAnimationActive={isAnimationActive} />
    </BarChart>
  );
}

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

ComponentThe props go on
Barthe bar rectangle
RadialBarthe sector
Piethe slice
Funnelthe trapezoid
Scatterthe point symbol
Line, Area, Radarthe 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)
Treemapthe node rectangle
SunburstChartthe sector
Sankeythe 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:

  • id is 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, cx and cy from 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 as r on 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:

  1. the data entry
  2. the props on the component (<Bar fill="…" stroke="…" />)
  3. 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.

import { BarChart, Bar, XAxis, YAxis } from 'recharts';

const data = [
  { name: 'A', value: 120 },
  { name: 'B', value: 180, fill: 'red' }, // red fill, keeps the #333 outline
  { name: 'C', value: 150, stroke: 'lime' }, // keeps the #8884d8 fill, lime outline
  { name: 'D', value: 90, strokeDasharray: '4 2' }, // dashed outline, component colors
];

const chartStyle = { width: '100%', maxWidth: '500px', aspectRatio: 1.618 };

export default function DataStylesPrecedenceExample({ isAnimationActive = true }: { isAnimationActive?: boolean }) {
  return (
    <BarChart style={chartStyle} responsive data={data}>
      <XAxis dataKey="name" />
      <YAxis />
      <Bar dataKey="value" fill="#8884d8" stroke="#333" strokeWidth={2} isAnimationActive={isAnimationActive} />
    </BarChart>
  );
}

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 fill or a stroke, 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, SunburstChart and Sankey, the Tooltip shows the color of the hovered entry: its fill, or its stroke if it has no fill.
  • For Line, Area and Radar, the Tooltip shows the color of the series, because the line is one shape. Styled dots do not change it.
  • Pie, RadialBar and Funnel show 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

import { LineChart, Line, XAxis, YAxis } from 'recharts';

const data = [
  { day: 'Mon', visitors: 320 },
  { day: 'Tue', visitors: 410 },
  { day: 'Wed', visitors: 980, fill: '#e11d48', r: 6 }, // highlight the outlier
  { day: 'Thu', visitors: 450 },
];

const chartStyle = { width: '100%', maxWidth: '500px', aspectRatio: 1.618 };

export default function DataStylesLineDotsExample({ isAnimationActive = true }: { isAnimationActive?: boolean }) {
  return (
    <LineChart style={chartStyle} responsive data={data}>
      <XAxis dataKey="day" />
      <YAxis />
      <Line dataKey="visitors" stroke="#8884d8" dot isAnimationActive={isAnimationActive} />
    </LineChart>
  );
}

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.