useYAxisDomain

Returns the calculated domain of a Y-axis.

The domain can be numerical: [min, max], or categorical: ['a', 'b', 'c'].

The type of the domain is defined by the type prop of the YAxis.

The values of the domain are calculated based on the data and the dataKey of the axis.

Does not interact with Brushes, as Y-axes do not support brushing.

Available since Recharts 3.2

import { Line, XAxis, YAxis, CartesianGrid, Tooltip, Legend, createHorizontalChart } from 'recharts';
import { generateMockData, MockDataType, RechartsDevtools } from '@recharts/devtools';

const data: Array<MockDataType> = generateMockData(6, 26213);

const Typed = createHorizontalChart<MockDataType, string, number>()({ XAxis, YAxis, Tooltip, Line });

export default function Example() {
  return (
    <Typed.LineChart
      style={{ width: '100%', maxWidth: '700px', height: '100%', maxHeight: '70vh', aspectRatio: 1.618 }}
      responsive
      data={data}
      margin={{
        top: 5,
        right: 0,
        left: 0,
        bottom: 5,
      }}
    >
      <CartesianGrid />
      <Typed.XAxis dataKey="label" />
      <Typed.YAxis width="auto" />
      <Tooltip />
      <Legend />
      <Typed.Line dataKey="x" />
      <Typed.Line dataKey="y" />
      <RechartsDevtools />
    </Typed.LineChart>
  );
}

Return value

CategoricalDomain | NumberDomain | undefined

The domain of the Y-axis, or undefined if it cannot be calculated or if used outside a chart context.

  • yAxisIdnumber | stringoptional

    The yAxisId of the Y-axis. Defaults to 0 if not provided.

    默认值:"defaultAxisId"