ReferenceLine

Draws a line on the chart connecting two points.

This component, unlike line, is aware of the cartesian coordinate system, so you specify the dimensions by using data coordinates instead of pixels.

ReferenceLine will calculate the pixels based on the provided data coordinates.

If you prefer to render using pixels rather than data coordinates, consider using the line SVG element instead.

import { AreaChart, ReferenceLine, Area, XAxis, YAxis, CartesianGrid, Tooltip } from 'recharts';
import { RechartsDevtools } from '@recharts/devtools';

// #region Sample data
const data = [
  {
    name: 'Page A',
    uv: 4000,
    pv: 2400,
    amt: 2400,
  },
  {
    name: 'Page B',
    uv: 3000,
    pv: 1398,
    amt: 2210,
  },
  {
    name: 'Page C',
    uv: 2000,
    pv: 9800,
    amt: 2290,
  },
  {
    name: 'Page D',
    uv: 2780,
    pv: 3908,
    amt: 2000,
  },
  {
    name: 'Page E',
    uv: 1890,
    pv: 4800,
    amt: 2181,
  },
  {
    name: 'Page F',
    uv: 2390,
    pv: 3800,
    amt: 2500,
  },
  {
    name: 'Page G',
    uv: 3490,
    pv: 4300,
    amt: 2100,
  },
];

// #endregion
const ReferenceLineExample = ({ isAnimationActive = true }: { isAnimationActive?: boolean }) => (
  <AreaChart
    style={{ width: '100%', maxWidth: '700px', maxHeight: '70vh', aspectRatio: 1.618 }}
    responsive
    data={data}
    margin={{
      top: 20,
      right: 0,
      left: 0,
      bottom: 0,
    }}
  >
    <XAxis dataKey="name" />
    <YAxis width="auto" />
    <CartesianGrid />
    <Tooltip />
    <Area type="monotone" dataKey="uv" fillOpacity={0.3} isAnimationActive={isAnimationActive} />
    <ReferenceLine
      x="Page C"
      stroke="green"
      label={{ value: 'prop `x` creates vertical lines', position: 'insideTop', offset: 70 }}
    />
    <ReferenceLine
      y={4000}
      label={{ value: 'prop `y` creates horizontal lines', position: 'insideTopRight' }}
      stroke="red"
      strokeDasharray="3 3"
    />
    <ReferenceLine
      label={{ value: 'prop `segment` connects two arbitrary points', position: 'middle' }}
      stroke="green"
      strokeDasharray="3 3"
      segment={[
        { x: 'Page D', y: 0 },
        { x: 'Page F', y: 1500 },
      ]}
    />
    <RechartsDevtools />
  </AreaChart>
);

export default ReferenceLineExample;
import { ReferenceLine, XAxis, YAxis, CartesianGrid, Tooltip, BarChart, Bar } from 'recharts';
import { RechartsDevtools } from '@recharts/devtools';

// #region Sample data
const smallData = [
  {
    name: 'Page A',
    pv: 2400,
  },
  {
    name: 'Page B',
    pv: 1398,
  },
  {
    name: 'Page C',
    pv: 9800,
  },
];

// #endregion
export const ReferenceLinePositionExample = ({ isAnimationActive = true }: { isAnimationActive?: boolean }) => (
  <BarChart
    style={{ width: '100%', maxWidth: '700px', maxHeight: '70vh', aspectRatio: 1.618 }}
    responsive
    data={smallData}
  >
    <XAxis dataKey="name" />
    <YAxis width="auto" />
    <CartesianGrid />
    <Tooltip />
    <ReferenceLine x="Page B" label={{ value: 'position: start' }} position="start" />
    <ReferenceLine x="Page B" label={{ value: 'position: middle' }} position="middle" />
    <ReferenceLine x="Page B" label={{ value: 'position: end' }} position="end" />
    <Bar dataKey="pv" fillOpacity={0.3} isAnimationActive={isAnimationActive} />
    <RechartsDevtools />
  </BarChart>
);

export default ReferenceLinePositionExample;

父组件

ReferenceLine consumes context provided by these components:

子组件

ReferenceLine provides context for these components:

  • classNamestringoptional

  • ifOverflow"discard" | "extendDomain" | "hidden" | "visible"optional

    Defines how to draw this component if it falls partly outside the canvas:

    • discard: the whole component will not be drawn at all
    • hidden: the component will be clipped to the chart plot area
    • visible: the component will be drawn completely
    • extendDomain: the domain of the overflown axis will be extended such that the whole component fits into the plot area

    默认值:"discard"

  • label(union of 6 variants)optional

    Renders a single label.

    • false: no labels are rendered
    • string | number: the content of the label
    • object: the props of a single Label component, such as value and position
    • ReactElement: a custom SVG label element, such as <text> or <g>. HTML elements such as <div> are not valid inside the chart SVG and may trigger React DOM warnings.
    • function: a render function of custom label

    默认值:false

    格式:

    <ReferenceLine y={20} label={{ value: 'Target', position: 'insideTop' }} />
  • position"end" | "middle" | "start"optional

    The position of the reference line when the axis has bandwidth (e.g., a band scale). This determines where within the band the line is drawn.

    默认值:"middle"

  • segment[{ x?: XValueType | undefined; y?: YValueType | undefined; }, { x?: XValueType | undefined; y?: YValueType | undefined; }]optional

    Tuple of coordinates. If defined, renders a diagonal line segment.

  • shapeFunction | ReactNodeoptional

  • strokeWidthnumber | stringoptional

    The width of the stroke

  • xnumber | stringoptional

    If defined, renders a vertical line on this position.

    This value is using your chart's domain, so you will provide a data value instead of a pixel value. ReferenceLine will internally calculate the correct pixel position.

    格式:

    <ReferenceLine x="Monday" />
  • xAxisIdnumber | stringoptional

    The id of x-axis which is corresponding to the data. Required when there are multiple XAxes.

    默认值:0

  • ynumber | stringoptional

    If defined, renders a horizontal line on this position.

    This value is using your chart's domain, so you will provide a data value instead of a pixel value. ReferenceLine will internally calculate the correct pixel position.

    格式:

    <ReferenceLine y="Page D" />
  • yAxisIdnumber | stringoptional

    The id of y-axis which is corresponding to the data. Required when there are multiple YAxes.

    默认值:0

  • 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.

    Recharts 自 3.4

    默认值:400