UNPKG

recharts

Version:
263 lines (262 loc) • 11.4 kB
import * as React from 'react'; import { CSSProperties, SVGProps } from 'react'; interface Words { words: Array<string>; width: number | undefined; } /** * @inline * @public */ export type TextAnchor = 'start' | 'middle' | 'end' | 'inherit'; export declare function isValidTextAnchor(value: string | undefined): value is TextAnchor; /** * @inline * @public */ export type TextVerticalAnchor = 'start' | 'middle' | 'end'; /** * @inline * @public */ export type RenderableText = string | number | boolean | null | undefined; export declare function isRenderableText(val: unknown): val is RenderableText; /** * Props of the shape that is drawn behind the text. * Accepts the usual SVG `<rect>` attributes, such as `fill`, `fillOpacity`, `stroke`, `rx` and `className`. * Straight text uses a rectangle; text on a path uses a stroked copy of that path. * The position and size are measured from the rendered text. * * @since 3.11 * @public */ export type TextBackgroundProps = Omit<SVGProps<SVGRectElement>, 'x' | 'y' | 'width' | 'height' | 'ref' | 'children' | 'transform'> & { /** * Space between the text and the edge of the background, in pixels. * A number applies the same padding on all sides, an object sets horizontal and vertical padding separately. * * @defaultValue { x: 4, y: 2 } */ padding?: number | { x?: number; y?: number; }; /** * zIndex layer of the background in a `Label` or `LabelList` inside a chart. * `Text` used on its own ignores it and draws the background right before the text, * because `Text` has no zIndex either. Without knowing which layer the text renders in, * a background in any other layer could end up above its own text, so right before the text is the only safe place. * In a label with a nonzero zIndex, 0 becomes -1, because layer 0 renders inside the label's own layer. * * @defaultValue 100 below the label zIndex, which is `DefaultZIndexes.labelBackground` for default labels */ zIndex?: number; }; /** * Props of the halo (text outline) drawn around the letters, as a lighter alternative to `background`. * Accepts a subset of the SVG `<text>` stroke attributes. * * @since 3.11 */ export type TextHaloProps = Pick<SVGProps<SVGTextElement>, 'stroke' | 'strokeWidth' | 'strokeOpacity' | 'strokeLinejoin'>; interface TextProps { /** * When true, scales the text to fit within the specified width. * The text will be scaled down proportionally to fit the available space. * * **Important interactions:** * - Requires `width` to be defined to have any effect. If width is undefined, scaleToFit does nothing. * - When enabled, `maxLines` restrictions are bypassed and ellipsis truncation is disabled. * - Uses the first line's width to calculate the scale factor. * - The scaling transform is applied as `scale(width / lineWidth)`. * * @defaultValue false */ scaleToFit?: boolean; /** * Text rotation angle in degrees. * Positive values rotate clockwise, negative values rotate counterclockwise. * * @defaultValue 0 */ angle?: number; /** * Horizontal text alignment within the text element. * - 'start': Text starts at the x coordinate (left-aligned for LTR text) * - 'middle': Text is centered on the x coordinate * - 'end': Text ends at the x coordinate (right-aligned for LTR text) * - 'inherit': Inherits the text-anchor from parent element * * **Note:** This controls horizontal alignment only and does not affect RTL text behavior. * This prop is also used when `textPath` is set. * @defaultValue 'start' */ textAnchor?: TextAnchor; /** * Vertical text alignment relative to the y coordinate. * - 'start': Text baseline starts at y coordinate (text appears below the y position) * - 'middle': Text is vertically centered on the y coordinate * - 'end': Text baseline ends at y coordinate (text appears above the y position) * * **Note:** This controls vertical positioning only and does not affect RTL (right-to-left) text behavior. * The alignment calculation uses capHeight and lineHeight to determine the starting dy offset. * Ignored when `textPath` is set because the path controls the text's positioning. * * @defaultValue 'end' */ verticalAnchor?: TextVerticalAnchor; /** * CSS styles to apply to the text element. * These styles are used for text measurement calculations when width constraints or scaleToFit are used. * Font-related properties (fontSize, fontFamily, fontWeight, etc.) are particularly important for accurate measurements. * When `textPath` is set, styles still apply to the rendered text, but only `scaleToFit` uses them for measurement. */ style?: CSSProperties; /** * Line height for multi-line text. * Can be a number (height in pixels) or a string with CSS units. * Used to calculate spacing between lines when text wraps to multiple lines. * Also used in verticalAnchor calculations for positioning the text block. * Ignored when `textPath` is set because the text is rendered along a single path. * @defaultValue '1em' */ lineHeight?: number | string; /** * When true, enables character-level breaking instead of word-level breaking. * - false: Text breaks at word boundaries (spaces, tabs, etc.) * - true: Text can break between any characters, useful for languages without spaces * * **Note:** Only effective when `width` is defined to enable line breaking. * Ignored when `textPath` is set because path text is not split into lines. * @defaultValue false */ breakAll?: boolean; /** * The text content to render. * Can be a string or number. Numbers will be converted to strings. * undefined or null values will result in no text being rendered. * When `textPath` is set, the content is rendered directly inside the SVG `textPath` element. */ children?: RenderableText; /** * Maximum number of lines to display when text wrapping is enabled. * When text exceeds this limit, it will be truncated with an ellipsis (…). * * **Important requirements for ellipsis truncation:** * - `width` must be defined (no effect when width is undefined) * - `scaleToFit` must be false (when scaleToFit is true, maxLines is bypassed) * - Text must actually overflow the specified maxLines or width constraints * * **Truncation behavior:** * - Uses binary search to find the optimal truncation point * - Adds ellipsis (…) at the end of the truncated text * - Ensures the truncated text + ellipsis fits within the constraints * * **Interaction with other props:** * - When `scaleToFit` is true, this property is ignored * - Requires `width` to be set for line breaking to occur * - Ignored when `textPath` is set because path text is rendered directly without truncation */ maxLines?: number; /** * When width is specified, the text will automatically wrap by calculating the width of text. * When `textPath` is set, this prop does not wrap or constrain the path text; it only affects scaling when `scaleToFit` is enabled. */ width?: number | string; /** * When set, renders the text along the SVG path described by this `d` attribute. * In path mode, `breakAll`, `lineHeight`, `maxLines`, and `verticalAnchor` are ignored. * * @since 3.11 */ textPath?: string; /** * Draws a background behind the text, for example to keep a label readable on top of a filled shape. * * - `true` draws the background with default styles. * 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`, a `padding` and a `zIndex`. * - `false` or `undefined` draws no background. * * The background is sized after the text renders, so it follows the content, font, line breaks and rotation. * For `textPath`, the background follows the same path as the text. * It is not rendered during server-side rendering. * * Inside a `Label` or `LabelList` 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 <Text background>Label</Text> * @example <Text background={{ fill: '#fff', stroke: '#000', rx: 2, padding: { x: 6, y: 3 } }}>Label</Text> */ background?: boolean | TextBackgroundProps; /** * Draws an outline around the letters of the text, 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, so it is drawn during server-side rendering. * * @since 3.11 * @example <Text halo>Label</Text> * @example <Text halo={{ stroke: '#fff', strokeWidth: 4 }}>Label</Text> */ halo?: boolean | TextHaloProps; } /** * @public */ export type Props = Omit<SVGProps<SVGTextElement>, 'textAnchor' | 'verticalAnchor'> & TextProps; type GetWordsByLinesProps = Pick<Props, 'width' | 'scaleToFit' | 'children' | 'style' | 'breakAll' | 'maxLines'>; export declare const getWordsByLines: ({ width, scaleToFit, children, style, breakAll, maxLines }: GetWordsByLinesProps) => readonly Words[]; /** * The geometry of the text bounding box, in the text's own coordinate system. */ export type TextBox = { x: number; y: number; width: number; height: number; }; export type TextPathMetrics = { pathLength: number; textLength: number; textHeight: number; startOffset: number; }; export type TextPathBackgroundGeometry = { start: number; length: number; strokeWidth: number; }; export declare function resolveBackgroundPadding(padding: TextBackgroundProps['padding']): { x: number; y: number; }; /** * Returns the rectangle of the text background, or null if there is no text to put a background behind. * @param textBox bounding box of the text element * @param padding padding around the text * @returns background rectangle */ export declare function getTextBackgroundRect(textBox: TextBox, padding: TextBackgroundProps['padding']): TextBox | null; export declare function getTextPathBackgroundGeometry(metrics: TextPathMetrics, padding: TextBackgroundProps['padding'], textAnchor: TextAnchor): TextPathBackgroundGeometry | null; export declare const textDefaultProps: { readonly angle: 0; readonly breakAll: false; readonly capHeight: "0.71em"; readonly lineHeight: "1em"; readonly scaleToFit: false; readonly textAnchor: "start"; readonly verticalAnchor: "end"; readonly x: 0; readonly y: 0; }; /** * @public */ export declare const Text: React.ForwardRefExoticComponent<Omit<Props, "ref"> & React.RefAttributes<SVGTextElement>>; export {};