Skip to content

TextBox

Creates a wrapped text box for each point in an array of data.

Wraps three strings inside 200px-wide boxes: inline <i>, <u>, and <b> tags render as styled SVG, while the Chinese and Burmese lines show wrapping still works for scripts that don’t separate words with spaces.

The font size in pixels. Inferred from the DOM selection by default.

The line height, which is 1.2 times the font size by default.

The width for each text box.

Three equally-sized boxes hold text of very different lengths. With fontResize: true each string is scaled between fontMin and fontMax to fill its box — the single word is sized way up while the long passage shrinks to fit. Toggle fontResize off in the controls to see them all snap back to one fixed size.

Toggles font resizing — a static boolean, or an accessor returning a boolean.

The maximum font size in pixels, used when dynamically resizing fonts.

The minimum font size in pixels, used when dynamically resizing fonts.

textAnchor aligns the wrapped lines horizontally within the box (start, middle, or end) and verticalAlign places the block at the top, middle, or bottom of its height; both accept accessors, so each datum here chooses its own.

textAnchor

The horizontal alignment of each line in its box. "start", "middle", and "end" align lines to the box's start edge, center, or end edge (the start is the right edge in right-to-left text). "justify" spreads each line's words across the box, or its characters in CJK text, leaving a paragraph's last line and any line that ends at a newline start-aligned. A percentage such as "25%" places each line that share of the way across the box, so "0%", "50%", and "100%" match start, middle, and end; percentages follow the writing direction.

ƒ (function)
verticalAlign

The vertical alignment of the lines in each box.

ƒ (function)

textAnchor: "justify" spreads the words of each line across the box so both edges stay straight. The last line of a paragraph, and any line that ends at a newline (\n), keeps its natural spacing and sits at the start. Scripts written without spaces, such as Chinese and Japanese, are justified between characters instead. In right-to-left text, lines start at the right edge, and a run of left-to-right words (a name, say) keeps its own order.

textAnchor

The horizontal alignment of each line in its box. "start", "middle", and "end" align lines to the box's start edge, center, or end edge (the start is the right edge in right-to-left text). "justify" spreads each line's words across the box, or its characters in CJK text, leaving a paragraph's last line and any line that ends at a newline start-aligned. A percentage such as "25%" places each line that share of the way across the box, so "0%", "50%", and "100%" match start, middle, and end; percentages follow the writing direction.

ƒ (function)

The font size in pixels. Inferred from the DOM selection by default.

The width for each text box.

A percentage places each line that share of the way across the box: the space left beside a line is split so "25%" of it sits before the line. "0%", "50%", and "100%" match start, middle, and end. Percentages follow the writing direction, so in right-to-left text "0%" is the right edge, like start.

The font size in pixels. Inferred from the DOM selection by default.

rotate turns the whole box by that many degrees around rotateAnchor (its center by default), the same mechanism axes use for slanted tick labels and charts use for vertical bar labels.

rotate

The rotation of each text box, in degrees about its rotateAnchor.

ƒ (function)

Text that does not fit is cut at the last whole line that fits the height, and maxLines caps the line count regardless of height. The cut line is passed through ellipsis, a function (text, line) => string that by default drops a trailing period or comma and appends ..., and can be replaced to end truncated text any other way.

maxLines

Restricts the maximum number of lines to wrap onto; null (unlimited) by default.

ƒ (function)

The height for each text box.

The ellipsis is measured with the line: when the line and its ellipsis are too wide for the box, words come off the end of the line until they fit.

By default truncated text only breaks between words, and a first word too wide for the box draws nothing. truncateWord: true lets truncation cut a word instead: the word that did not fit (or a first word wider than the box) is cut to the longest start that fits with the ellipsis. It cuts between whole characters, so an emoji or an accented letter is never split.

truncateWord

Lets truncated text cut a word: the word that did not fit (or a first word wider than the box) is cut to the longest start that fits with the ellipsis, by whole characters, so emoji and accented letters are never split. Defaults to false, which only breaks between words, and draws nothing when the first word does not fit.

ƒ (function)

The width for each text box.

A single word wider than the box cannot be wrapped. By default the wrapper stops there and truncates; overflow: true lets the word run past the box’s right edge instead, so nothing is lost when the box is only a sizing hint.

overflow

Whether text is allowed to overflow its bounding box.

ƒ (function)

The width for each text box.

textPath takes an SVG path d (or an accessor returning one), in coordinates relative to the box’s x and y, and draws the text along it as one line with its baseline on the path. The text wraps to the path’s length past startOffset (a distance in pixels, or a percentage of the path’s length such as "25%"), so text too long for the path is cut short with the ellipsis, and with truncateWord mid-word.

textAnchor places the line along that stretch of path: start at its beginning, middle centered, end at the path’s end, and a percentage that share of the way along. justify reads as start, because the one line ends its paragraph. In right-to-left text the start is the path’s end. side: "right" follows the path backwards, so the text sits on its other side; on an arc that curves over the top, that puts it beneath the arc, upside down. Draw the path the other way to read it the right way up. Both renderers draw it the same way: SVG with a <textPath>, and Canvas glyph by glyph along the path, each turned to the path’s direction.

The horizontal alignment of each line in its box. "start", "middle", and "end" align lines to the box's start edge, center, or end edge (the start is the right edge in right-to-left text). "justify" spreads each line's words across the box, or its characters in CJK text, leaving a paragraph's last line and any line that ends at a newline start-aligned. A percentage such as "25%" places each line that share of the way across the box, so "0%", "50%", and "100%" match start, middle, and end; percentages follow the writing direction.

Where along the textPath the text's room begins: a distance in pixels, or a percentage of the path's length such as "25%". Defaults to 0.

side

Which side of the textPath the text sits on, read in the path's direction: "left" (the default) follows the path as drawn, with the text above a left-to-right path, and "right" follows it backwards, so the text sits on its other side.

The font size in pixels. Inferred from the DOM selection by default.

A closed path, such as a circle, takes text all the way around it.

Where along the textPath the text's room begins: a distance in pixels, or a percentage of the path's length such as "25%". Defaults to 0.

The font size in pixels. Inferred from the DOM selection by default.

Only a label’s glyphs pick it up in SVG, so a hover or click between its words or lines passes through to whatever is behind it. hitbox: true draws a transparent rect behind each box that covers the whole box (padding included) and carries the same datum, so the gaps count too. {padding} (a number or CSS shorthand) reaches further than the box. Text on a path gets the path’s bounds grown by a font size. Keyboard navigation skips the rect, so a label with a hitbox doesn’t become a stop of its own.

new d3plus.Treemap()
.data(data)
.groupBy("id")
.shapeConfig({labelConfig: {hitbox: true}});

fontFamily, fontWeight, fontColor, and fontStroke/fontStrokeWidth all take values or accessors. A thin dark stroke around light text is how chart labels stay legible over any fill.

fontWeight

The font weight. Inferred from the DOM selection by default.

ƒ (function)
fontColor

The font color as an accessor function or static string. Inferred from the DOM selection by default.

ƒ (function)
fontStroke

The font stroke color for the rendered text.

ƒ (function)

Configuration

import {TextBox} from "@d3plus/core";

Extends BaseClass.

ariaHiddenboolean

Hides the SVG from assistive technology when true (aria-hidden).

classNamestring | array.&lt;string&gt; | undefined

CSS classes added to the outermost element this chart or component draws into, next to its fixed d3plus classes, so page CSS can target it: a chart's root <svg class="d3plus-viz"> (its <canvas> with the Canvas renderer) and a standalone component's <svg>. Takes a space-separated string or an array. A new value replaces the classes the previous one added; unset adds none. Tooltips take their own tooltipConfig.className.

colorDefaultscolordefaultsconfig

Overrides the default colors used when assigning fills from data and choosing legible text colors: dark and light (the text colors picked for contrast against a background), missing (null/undefined values), on/off (true/false values), sequential (the anchor hue for magnitude ramps), and scale (the categorical palette, given as a d3 ordinal scale or an array of colors). Keys are merged into the current defaults, and a Viz passes its overrides down to the shapes and components it draws.

configd3plusconfig

Methods that correspond to the key/value pairs and returns this class.

dataarray.&lt;datapoint&gt;

The data array used to draw text boxes. A text box will be drawn for each object in the array.

delaynumberDefault 0
durationnumberDefault 0

Animation duration in ms.

ellipsisfunction

Handles truncated lines, returning the new line value. Passed the line's text and number; by default drops the line's trailing periods, commas, and spaces and appends "...", except for a first word that cannot fit (which returns ""). The ended line is measured, and words come off its end until it fits the box.

fontColorcsscolor | function

The font color as an accessor function or static string. Inferred from the DOM selection by default.

fontFamilystring | array.&lt;string&gt; | function

The font-family to use: a font name, a comma-separated list of fallbacks, an array of fallbacks, or an accessor returning a string or array. The first available font on the client is used.

fontMaxnumber

The maximum font size in pixels, used when dynamically resizing fonts.

fontMinnumber

The minimum font size in pixels, used when dynamically resizing fonts.

fontOpacitynumber | function

The font opacity as an accessor function or static number between 0 and 1.

fontResizeboolean | function

Toggles font resizing — a static boolean, or an accessor returning a boolean.

fontSizenumber | function

The font size in pixels. Inferred from the DOM selection by default.

fontStrokecsscolor | function

The font stroke color for the rendered text.

fontStrokeWidthnumber | function

The font stroke width for the rendered text.

fontWeightstring | number | function

The font weight. Inferred from the DOM selection by default.

heightnumber | function

The height for each text box.

hitboxboolean | object | function

Draws a transparent rect behind each box, so a hover or click anywhere in the box (padding included), not just on its glyphs, reaches the box's datum. true, or {padding} (a number or CSS shorthand) to reach further than the box. Text on a path gets the path's bounds grown by a font size. Defaults to false.

htmlboolean | record

Configures the ability to render simple HTML tags. Defaults to supporting <b>, <strong>, <i>, and <em>, set to false to disable or provide a mapping of tags to svg styles

idaccessorfn

Unique-id accessor per datum (used for keyed enter/update/exit).

lineHeightnumber | function

The line height, which is 1.2 times the font size by default.

localestring | object

The locale used for all text and number formatting. Supports the locales defined in d3plus-format. The locale can be a complex Object, a locale code (like "en-US"), or a 2-digit language code (like "en"). If a 2-digit code is provided, the "findLocale" function is used to identify the most approximate locale.

maxLinesnumber | null | function

Restricts the maximum number of lines to wrap onto; null (unlimited) by default.

onstring

Event listener for the specified event typenames. Mirrors the core d3-selection behavior.

overflowboolean | function

Whether text is allowed to overflow its bounding box.

paddingstring | number | function

The padding as a CSS shorthand string or number. Defaults to 0.

parentunknown

Parent config used by the wrapper.

pointerEvents"auto" | "none" | function

Whether the text receives pointer events ("none" lets them through to what is underneath).

renderfunction

Renders the text boxes. If a callback is specified, it will be called once the shapes are done drawing.

rotatenumber | function

The rotation of each text box, in degrees about its rotateAnchor.

rotateAnchor[number, number] | function

The anchor point around which to rotate the text box.

selectstring | htmlelement

The SVG container element as a d3 selector or DOM element. If not specified, an SVG element will be added to the page.

shape"square" | "circle" | function

The outline text wraps within, "square" or "circle", or a (datum, index) accessor.

shapeConfigd3plusconfig

Configuration object with key/value pairs applied as method calls on each shape.

side"left" | "right" | function

Which side of the textPath the text sits on, read in the path's direction: "left" (the default) follows the path as drawn, with the text above a left-to-right path, and "right" follows it backwards, so the text sits on its other side.

splitfunction

The word split function: given a string, returns it split into an array of words.

startOffsetnumber | `${number}%` | function

Where along the textPath the text's room begins: a distance in pixels, or a percentage of the path's length such as "25%". Defaults to 0.

textstring | function

The text content for each box.

textAnchor"start" | "middle" | "end" | "justify" | `${number}%` | function

The horizontal alignment of each line in its box. "start", "middle", and "end" align lines to the box's start edge, center, or end edge (the start is the right edge in right-to-left text). "justify" spreads each line's words across the box, or its characters in CJK text, leaving a paragraph's last line and any line that ends at a newline start-aligned. A percentage such as "25%" places each line that share of the way across the box, so "0%", "50%", and "100%" match start, middle, and end; percentages follow the writing direction.

textPathstring | false | null | function

An SVG path d (or a (datum, index) accessor returning one) for the text to follow, in coordinates relative to the box's x and y. The text is drawn as one line with its baseline on the path, wrapped to the path's length past startOffset (and cut short with the ellipsis and truncateWord). textAnchor places it along that stretch of path: "middle" centers it, "end" ends it at the path's end, a percentage places it that share along, and "justify" reads as "start", since the one line ends its paragraph. Falsy draws the text in its box.

translatefunction

Defines how informational text strings should be displayed. By default, this function will try to find the string in question (which is the first argument provided to this function) inside of an internally managed translation Object. If you'd like to override to use custom text, simply pass this method your own custom formatting function.

truncateWordboolean | function

Lets truncated text cut a word: the word that did not fit (or a first word wider than the box) is cut to the longest start that fits with the ellipsis, by whole characters, so emoji and accented letters are never split. Defaults to false, which only breaks between words, and draws nothing when the first word does not fit.

verticalAlign"top" | "middle" | "bottom" | function

The vertical alignment of the lines in each box.

widthnumber | function

The width for each text box.

xnumber | function

The x position (left edge) for each text box.

ynumber | function

The y position (top edge) for each text box.