recharts
Version:
React charts
204 lines (203 loc) • 8.27 kB
TypeScript
import * as React from 'react';
import { ReactElement, ReactNode, SVGProps } from 'react';
import { RenderableText, TextAnchor, TextBackgroundProps, TextHaloProps, TextVerticalAnchor } from './Text';
import { CartesianViewBoxRequired, DataKey, PolarViewBoxRequired, TrapezoidViewBox, ViewBox } from '../util/types';
import { ZIndexable } from '../zIndex/ZIndexLayer';
import { CartesianLabelPosition } from '../cartesian/getCartesianPosition';
/**
* @inline
* @public
*/
export type LabelContentType = ReactElement | ((props: Props) => RenderableText | ReactElement);
type PolarLabelPosition = 'insideStart' | 'insideEnd' | 'end';
/**
* @inline
* @public
*/
export type LabelPosition = CartesianLabelPosition | PolarLabelPosition;
/**
* @inline
*/
export type LabelFormatter = (label: RenderableText) => RenderableText;
interface LabelProps extends ZIndexable {
/**
* The box of viewing area. Used for positioning.
* If undefined, viewBox will be calculated based on surrounding context.
*/
viewBox?: ViewBox;
parentViewBox?: ViewBox;
/**
* Function to customize how content is serialized before rendering.
*
* This should return a renderable text - something that the {@link Text} component can render.
* Typically, a string or number.
* Custom components are not supported here - use the `content` prop instead.
*/
formatter?: LabelFormatter;
/**
* The value of label can be set as children or as the `value` prop
*
* @example <Label value="foo" />
*/
value?: RenderableText;
/**
* The offset to the specified "position". Direction of the offset depends on the position.
*
* @defaultValue 5
*/
offset?: number;
/**
* The position of label relative to the view box.
*
* @defaultValue middle
*/
position?: LabelPosition;
/**
* The value of label can be set as children or as the `value` prop
*
* @example <Label>foo</Label>
*/
children?: RenderableText;
className?: string;
/**
* If set a React element, the option is the custom React element of rendering 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.
* To render HTML, wrap it in an SVG `<foreignObject>` with a `width` and a `height`.
* The wrapper's `x` and `y` place its top-left corner. To center a 100-by-50 wrapper
* at `(x, y)`, place it at `(x - 50, y - 25)`.
*
* Pass `x` and `y` to `<Label>` to position a custom element, or return a wrapper
* from a `content` function to set its size and position directly.
* For a `<LabelList>` inside a `<Bar>`, the function receives the bar's `x` and `y` by default.
* Use `viewBox` to find a different position, such as the center of the bar.
*
* @example <Label content={CustomizedLabel} />
* @example
* const renderCustomLabel = (props) => <text {...props}>Custom Label</text>;
* <Label content={renderCustomLabel} />
*/
content?: LabelContentType;
/**
* @defaultValue false
*/
textBreakAll?: boolean;
/**
* Text rotation angle in degrees.
* Positive values rotate clockwise, negative values rotate counterclockwise.
*
* @defaultValue 0
*/
angle?: number;
index?: number;
labelRef?: React.RefObject<SVGTextElement> | null;
/**
* 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.
*
* @since 3.4
* @defaultValue 2000
* @see {@link https://recharts.github.io/en-US/guide/zIndex/ Z-Index and layers guide}
*/
zIndex?: number;
/**
* Unique identifier of this component.
* Used as an HTML attribute `id`.
*/
id?: string;
/**
* Draws a background behind the label, for example to keep it readable on top of a filled shape.
*
* - `true` draws the background with the styles from the theme's `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 `label.background`, and draws no background without a theme.
*
* The background is sized from the rendered text and follows curved label paths.
* It is not drawn for custom `content`.
*
* In a chart, the rectangle renders in a zIndex layer below the label,
* so overlapping labels never cover each other's text.
*
* @since 3.11
* @example <Label position="inside" background />
* @example <Label position="inside" background={{ fill: '#fff', rx: 2, padding: 4 }} />
*/
background?: boolean | TextBackgroundProps;
/**
* Draws an outline around the letters of the 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).
*
* @since 3.11
* @example <Label position="inside" halo />
* @example <Label position="inside" halo={{ stroke: '#fff', strokeWidth: 4 }} />
*/
halo?: boolean | TextHaloProps;
}
/**
* @public
*/
export type Props = Omit<SVGProps<SVGTextElement>, 'viewBox'> & LabelProps;
/**
* @public
*/
export type ImplicitLabelType = boolean | string | number | ReactElement<SVGElement> | ((props: any) => RenderableText | ReactElement) | (Props & {
dataKey?: DataKey<any>;
});
export declare const CartesianLabelContextProvider: ({ x, y, upperWidth, lowerWidth, width, height, children, }: TrapezoidViewBox & {
children: ReactNode;
}) => React.JSX.Element;
export declare const PolarLabelContextProvider: ({ cx, cy, innerRadius, outerRadius, startAngle, endAngle, clockWise, children, }: PolarViewBoxRequired & {
children: ReactNode;
}) => React.JSX.Element;
export declare const usePolarLabelContext: () => PolarViewBoxRequired | undefined;
export declare const isLabelContentAFunction: (content: unknown) => content is (props: Props) => React.ReactNode;
/**
* Returns true if the label renders in a polar view box, which decides how its `position` is interpreted.
*/
export declare const isPolarViewBox: (viewBox: CartesianViewBoxRequired | TrapezoidViewBox | PolarViewBoxRequired | undefined) => viewBox is PolarViewBoxRequired;
export type LabelPositionAttributes = {
x: number;
y: number;
textAnchor: TextAnchor;
verticalAnchor: TextVerticalAnchor;
width?: number;
height?: number;
};
export declare const defaultLabelProps: {
readonly angle: 0;
readonly offset: 5;
readonly zIndex: 2000;
readonly position: "middle";
readonly textBreakAll: false;
};
/**
* @consumes CartesianViewBoxContext
* @consumes PolarViewBoxContext
* @consumes CartesianLabelContext
* @consumes PolarLabelContext
* @public
*/
export declare function Label(outerProps: Props): React.JSX.Element | null;
export declare namespace Label {
var displayName: string;
}
export declare function CartesianLabelFromLabelProp({ label, labelRef, }: {
label: ImplicitLabelType | undefined;
labelRef?: React.RefObject<SVGTextElement> | null;
}): React.JSX.Element | null;
export declare function PolarLabelFromLabelProp({ label }: {
label: ImplicitLabelType | undefined;
}): React.JSX.Element | null;
export {};