UNPKG

recharts

Version:
245 lines (244 loc) • 10.8 kB
import * as React from 'react'; import { PropsWithoutRef, ReactNode, SVGProps } from 'react'; import { LabelContentType, LabelPosition, LabelFormatter } from './Label'; import { CartesianViewBoxRequired, DataKey, PolarViewBoxRequired, TrapezoidViewBox } from '../util/types'; import { LabelProps } from '../index'; import { ZIndexable } from '../zIndex/ZIndexLayer'; import { RenderableText, TextBackgroundProps, TextHaloProps } from './Text'; import { AxisId } from '../state/cartesianAxisSlice'; /** * @public */ export interface LabelListEntry<DataPointItem = any> { /** * Value is what renders in the UI as the label content. * If undefined, then the LabelList will pull it from the payload using the dataKey. */ value: unknown; /** * Payload is the source data object for this entry. The shape of this depends on what the user has passed * as the data prop to the chart. */ payload: DataPointItem; fill: string | undefined; /** * Index of the data entry that this label belongs to. * Some graphical items do not render every data entry (Bar skips bars without a size, for example), * so this can differ from the position of the entry in the list. * If undefined, the position in the list is the index. */ originalDataIndex?: number; } /** * This is public API because we expose it as the valueAccessor parameter. * * The properties of "viewBox" are repeated as the root props of the entry object. * So it doesn't matter if you read entry.x or entry.viewBox.x, they are the same. * * It's not necessary to pass redundant data, but we keep it for backward compatibility. */ export interface CartesianLabelListEntry extends LabelListEntry, TrapezoidViewBox { /** * The bounding box of the graphical element that this label is attached to. * This will be an individual Bar for example. */ viewBox: TrapezoidViewBox; parentViewBox?: CartesianViewBoxRequired; } export interface PolarLabelListEntry extends LabelListEntry { viewBox: PolarViewBoxRequired; parentViewBox?: PolarViewBoxRequired; clockWise?: boolean; } interface LabelListProps extends ZIndexable { /** * Unique identifier of this component. * Used as an HTML attribute `id`. */ id?: string; /** * The accessor function to get the value of each label. Is ignored if dataKey is specified. * @param entry * @param index the index of the data entry that the label belongs to */ valueAccessor?: (entry: CartesianLabelListEntry | PolarLabelListEntry, index: number) => RenderableText; /** * The parameter to calculate the view box of label in radial charts. */ clockWise?: boolean; /** * * 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. */ dataKey?: DataKey<any>; /** * 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 * * @example <LabelList content={CustomizedLabel} /> * * @see {@link https://recharts.github.io/en-US/examples/BarChartWithMinHeight/ Customized content of LabelList in a BarChart} */ content?: LabelContentType; textBreakAll?: boolean; /** * The position of label relative to the view box. */ position?: LabelPosition; /** * The offset to the specified "position". * Direction of the offset depends on the position. */ offset?: LabelProps['offset']; /** * Text rotation angle in degrees. * Positive values rotate clockwise, negative values rotate counterclockwise. * * @defaultValue 0 */ angle?: number; /** * 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; /** * 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; /** * 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. * * @see {@link https://recharts.github.io/en-US/guide/label-colors/ Label colors guide} * @example <LabelList position="inside" fill="#000" /> */ fill?: string; /** * 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. * * @since 3.11 * @example <LabelList position="inside" background /> * @example <LabelList position="inside" background={{ fill: '#fff', rx: 2, padding: 4 }} /> */ background?: boolean | TextBackgroundProps; /** * 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). * * @since 3.11 * @example <LabelList position="inside" halo /> * @example <LabelList position="inside" halo={{ stroke: '#fff', strokeWidth: 4 }} /> */ halo?: boolean | TextHaloProps; } /** * LabelList props do not allow refs because the same props are reused in multiple elements so we don't have a good single place to ref to. */ type SvgTextProps = PropsWithoutRef<SVGProps<SVGTextElement>>; /** * @public */ export type Props = Omit<SvgTextProps, 'children'> & LabelListProps; /** * This is the type accepted for the `label` prop on various graphical items. * It accepts: * * boolean: * true = labels show, * false = labels don't show * React element: * will be cloned with extra props * function: * is used as <Label content={function} />, so this will be called once for each individual label (so typically once for each data point) * object: * the props to be passed to a LabelList component * * @inline * @public */ export type ImplicitLabelListType = boolean | LabelContentType | Props; export declare const CartesianLabelListContextProvider: React.Provider<readonly CartesianLabelListEntry[] | (() => ReadonlyArray<CartesianLabelListEntry> | undefined) | undefined>; export declare const PolarLabelListContextProvider: React.Provider<readonly PolarLabelListEntry[] | undefined>; /** * Graphical items that can be clipped (for example when an axis has `allowDataOverflow`) render this around their labels. * The labels are not clipped, because the label of a point on the edge of the domain is often drawn outside of the clip area. * Instead, LabelList skips the labels of elements that are completely outside of the clip area. */ export declare function SetLabelListClipArea({ xAxisId, yAxisId, children, }: { xAxisId: AxisId; yAxisId: AxisId; children: ReactNode; }): React.JSX.Element; /** * @consumes LabelListContext * @public */ export declare function LabelList({ valueAccessor, ...restProps }: Props): React.JSX.Element | null; export declare namespace LabelList { var displayName: string; } export declare function LabelListFromLabelProp({ label, pointerEvents, }: { label?: ImplicitLabelListType; /** * The `pointer-events` of the rendered labels. * * Charts where a label can end up on top of a shape that has its own pointer events - such as the * Sankey, where a node label sits on the node rectangle - use this to keep the labels from * intercepting the events of the shape underneath them. */ pointerEvents?: string; }): React.JSX.Element | null; export {};