LabelList

import {
  BarChart,
  Bar,
  XAxis,
  YAxis,
  CartesianGrid,
  Label,
  LabelList,
  createHorizontalChart,
  LabelProps,
  LabelListProps,
} from 'recharts';
import { generateMockData, MockDataType, RechartsDevtools } from '@recharts/devtools';

// #region Sample data and styles
const data = generateMockData(6, 892134);
const Typed = createHorizontalChart<MockDataType>()({ BarChart, XAxis, YAxis, Bar, LabelList });
const chartStyle = { width: '100%', maxWidth: '700px', maxHeight: '70vh', aspectRatio: 1.618 };
const labelProps: LabelProps = {
  value: 'pv of page',
  angle: -90,
  position: 'insideLeft',
  textAnchor: 'middle',
};
const positionTop: LabelListProps = { position: 'top' };
// #endregion

const LabelListChartExample = ({ isAnimationActive = true }: { isAnimationActive?: boolean }) => (
  <Typed.BarChart style={chartStyle} responsive data={data}>
    <CartesianGrid />
    <Typed.XAxis dataKey="label">
      <Label value="Pages of my website" offset={0} position="insideBottom" />
    </Typed.XAxis>
    <Typed.YAxis width="auto" label={labelProps} />
    <Typed.Bar dataKey="x" isAnimationActive={isAnimationActive}>
      <Typed.LabelList dataKey="label" position="insideTopLeft" textAnchor="start" angle={45} dx={5} />
    </Typed.Bar>
    <Typed.Bar dataKey="y" isAnimationActive={isAnimationActive} label={positionTop} />
    <RechartsDevtools />
  </Typed.BarChart>
);

export default LabelListChartExample;
import { Bar, BarChart, CartesianGrid, LabelList, TextBackgroundProps, XAxis, YAxis } from 'recharts';
import { RechartsDevtools } from '@recharts/devtools';

// #region Sample data
const data = [
  {
    name: 'Page A',
    uv: 4000,
    pv: 2400,
  },
  {
    name: 'Page B',
    uv: 3000,
    pv: 1398,
  },
  {
    name: 'Page C',
    uv: 2000,
    pv: 9800,
  },
  {
    name: 'Page D',
    uv: 2780,
    pv: 3908,
  },
];

// #endregion
const pillBackground: TextBackgroundProps = {
  rx: 10,
  padding: { x: 8, y: 3 },
  fillOpacity: 0.9,
};

const BarChartWithLabelBackground = ({ isAnimationActive = true }: { isAnimationActive?: boolean }) => (
  <BarChart
    style={{ width: '100%', maxWidth: '700px', maxHeight: '70vh', aspectRatio: 1.618 }}
    responsive
    data={data}
    margin={{
      top: 15,
      right: 0,
      left: 0,
      bottom: 5,
    }}
  >
    <CartesianGrid />
    <XAxis dataKey="name" />
    <YAxis width="auto" />
    <Bar dataKey="pv" stackId="a" isAnimationActive={isAnimationActive}>
      {/* `background` with default styles: a rounded rectangle in the chart background color */}
      <LabelList dataKey="pv" position="inside" background />
    </Bar>
    <Bar dataKey="uv" stackId="a" isAnimationActive={isAnimationActive}>
      {/* `background` with custom rectangle props */}
      <LabelList dataKey="uv" position="inside" background={pillBackground} />
    </Bar>
    <RechartsDevtools />
  </BarChart>
);

export default BarChartWithLabelBackground;

Parent Components

LabelList consumes context provided by these components:

  • anglenumberoptional

    Text rotation angle in degrees. Positive values rotate clockwise, negative values rotate counterclockwise.

    DEFAULT: 0

  • backgroundTextBackgroundProps | false | trueoptional

    Draws a rectangle behind each label, for example to keep labels readable on top of filled shapes.

    • true draws the background with the styles from the theme's graphicalItems[n].label.background. If neither sets a fill, the fill is the pageBackground color from the theme, or white when no theme is set.
    • An object accepts SVG <rect> attributes such as fill, stroke and rx, and a padding. It is merged over the theme styles key by key.
    • false draws no background, even if the theme turns backgrounds on.
    • undefined uses the theme's graphicalItems[n].label.background, and draws no background without a theme.

    Each background is sized from its rendered label and follows curved label paths. It is not drawn for custom content.

    In a chart, the rectangles render in a zIndex layer below the labels, so overlapping labels never cover each other's text.

    Available since Recharts 3.11

    FORMAT:

    <LabelList position="inside" background />
    <LabelList position="inside" background={{ fill: '#fff', rx: 2, padding: 4 }} />
  • clockWisebooleanoptional

    The parameter to calculate the view box of label in radial charts.

  • contentFunction | ReactNodeoptional

    If set a React element, the option is the customized React element of rendering each label. Use an SVG element or component, such as <text> or <g>. HTML elements such as <div> are not valid inside the chart SVG and may trigger React DOM warnings. If set to a function, the function is called once for each item

    FORMAT:

    <LabelList content={CustomizedLabel} />
  • dataKeyFunction | number | stringoptional

    Decides how to extract the value of each label from the data:

    • string: the name of the field in the data object;
    • number: the index of the field in the data;
    • function: a function that receives the data object and returns the value of each label.

    If set, then valueAccessor will be ignored.

    Scatter requires this prop to be set. Other graphical components will show the same value as the dataKey of the component by default.

  • fillstringoptional

    The text color of every label. It wins over the theme.

    Without it, and without a theme, each label takes the fill of its data entry, if there is one.

    With a theme, a fill in the theme's graphicalItems[n].label.style applies first. Otherwise each label picks a color automatically:

    • A label on top of a filled shape (Bar, Funnel, Pie, RadialBar) takes the pageBackground color, or the typography color if there is no page background. A shape with fill="none" or transparent is not filled, so its labels count as next to it.
    • A label next to its shape, and every label of Line, Area, Radar and Scatter, takes the color of its shape.
    • A label with a background or halo takes the color of its shape too.
    • A label at an { x, y } position takes the typography color.

    Custom content receives the resolved color as its fill prop.

    FORMAT:

    <LabelList position="inside" fill="#000" />
  • formatterFunctionoptional

    Function to customize how content is serialized before rendering.

    This should return a renderable text - something that the Text component can render. Typically, a string or number. Custom components are not supported here - use the content prop instead.

  • haloTextHaloProps | false | trueoptional

    Draws an outline around the letters of each label, as a lighter alternative to background.

    • true draws the halo with default styles. The stroke is the pageBackground color from the theme, or white when no theme is set.
    • An object accepts stroke, strokeWidth, strokeOpacity and strokeLinejoin to set them explicitly.
    • false or undefined draws no halo.

    Unlike background, a halo needs no measurement, works during server-side rendering, and works for labels that follow a curved path (insideStart, insideEnd and end in polar charts).

    Available since Recharts 3.11

    FORMAT:

    <LabelList position="inside" halo />
    <LabelList position="inside" halo={{ stroke: '#fff', strokeWidth: 4 }} />
  • idstringoptional

    Unique identifier of this component. Used as an HTML attribute id.

  • offsetnumber | stringoptional

    The offset to the specified "position". Direction of the offset depends on the position.

    DEFAULT: 5

  • position"bottom" | "center" | "centerBottom" | "centerTop" | "end" | "inside" | "insideBottom" | "insideBottomLeft" | "insideBottomRight" | "insideEnd" | "insideLeft" | "insideRight" | "insideStart" | "insideTop" | "insideTopLeft" | "insideTopRight" | "left" | "middle" | "outside" | "right" | "top" | { x?: string | number | undefined; y?: string | number | undefined; }optional

    The position of label relative to the view box.

    DEFAULT: "middle"

  • textBreakAllbooleanoptional

    DEFAULT: false

  • valueAccessorFunctionoptional

    The accessor function to get the value of each label. Is ignored if dataKey is specified.

  • zIndexnumberoptional

    Z-Index of this component and its children. The higher the value, the more on top it will be rendered. Components with higher zIndex will appear in front of components with lower zIndex. If undefined or 0, the content is rendered in the default layer without portals.

    Available since Recharts 3.4

    DEFAULT: 2000