TextBox
Creates a wrapped text box for each point in an array of data.
Basic example
Section titled “Basic example”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.
Font resize
Section titled “Font resize”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.
Alignment
Section titled “Alignment”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.
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 vertical alignment of the lines in each box.
ƒ (function)Justified text
Section titled “Justified text”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.
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.
Percentages
Section titled “Percentages”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.
Rotation
Section titled “Rotation”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.
The rotation of each text box, in degrees about its rotateAnchor.
ƒ (function)Truncation
Section titled “Truncation”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.
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.
Cutting words
Section titled “Cutting words”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.
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.
Long words
Section titled “Long words”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.
Whether text is allowed to overflow its bounding box.
ƒ (function)The width for each text box.
Text on a path
Section titled “Text on a path”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.
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.
Hitbox
Section titled “Hitbox”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}});Font styling
Section titled “Font styling”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.
The font weight. Inferred from the DOM selection by default.
ƒ (function)The font color as an accessor function or static string. Inferred from the DOM selection by default.
ƒ (function)The font stroke color for the rendered text.
ƒ (function)Configuration
import {TextBox} from "@d3plus/core";
Extends BaseClass.
ariaHiddenbooleanHides the SVG from assistive technology when true (
aria-hidden).classNamestring|array.<string>|undefinedCSS 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 owntooltipConfig.className.colorDefaultscolordefaultsconfigOverrides the default colors used when assigning fills from data and choosing legible text colors:
darkandlight(the text colors picked for contrast against a background),missing(null/undefined values),on/off(true/falsevalues),sequential(the anchor hue for magnitude ramps), andscale(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.configd3plusconfigMethods that correspond to the key/value pairs and returns this class.
dataarray.<datapoint>The data array used to draw text boxes. A text box will be drawn for each object in the array.
delaynumberDefault0durationnumberDefault0Animation duration in ms.
ellipsisfunctionHandles 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|functionThe font color as an accessor function or static string. Inferred from the DOM selection by default.
fontFamilystring|array.<string>|functionThe 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.
fontMaxnumberThe maximum font size in pixels, used when dynamically resizing fonts.
fontMinnumberThe minimum font size in pixels, used when dynamically resizing fonts.
fontOpacitynumber|functionThe font opacity as an accessor function or static number between 0 and 1.
fontResizeboolean|functionToggles font resizing — a static boolean, or an accessor returning a boolean.
fontSizenumber|functionThe font size in pixels. Inferred from the DOM selection by default.
fontStrokecsscolor|functionThe font stroke color for the rendered text.
fontStrokeWidthnumber|functionThe font stroke width for the rendered text.
fontWeightstring|number|functionThe font weight. Inferred from the DOM selection by default.
heightnumber|functionThe height for each text box.
hitboxboolean|object|functionDraws 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 tofalse.htmlboolean|recordConfigures 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 stylesidaccessorfnUnique-id accessor per datum (used for keyed enter/update/exit).
lineHeightnumber|functionThe line height, which is 1.2 times the font size by default.
localestring|objectThe 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|functionRestricts the maximum number of lines to wrap onto; null (unlimited) by default.
onstringEvent listener for the specified event typenames. Mirrors the core d3-selection behavior.
overflowboolean|functionWhether text is allowed to overflow its bounding box.
paddingstring|number|functionThe padding as a CSS shorthand string or number. Defaults to 0.
parentunknownParent config used by the wrapper.
pointerEvents"auto"|"none"|functionWhether the text receives pointer events (
"none"lets them through to what is underneath).renderfunctionRenders the text boxes. If a callback is specified, it will be called once the shapes are done drawing.
rotatenumber|functionThe rotation of each text box, in degrees about its
rotateAnchor.rotateAnchor[number, number]|functionThe anchor point around which to rotate the text box.
selectstring|htmlelementThe 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"|functionThe outline text wraps within,
"square"or"circle", or a(datum, index)accessor.shapeConfigd3plusconfigConfiguration object with key/value pairs applied as method calls on each shape.
side"left"|"right"|functionWhich side of the
textPaththe 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.splitfunctionThe word split function: given a string, returns it split into an array of words.
startOffsetnumber|`${number}%`|functionWhere along the
textPaththe text's room begins: a distance in pixels, or a percentage of the path's length such as"25%". Defaults to 0.textstring|functionThe text content for each box.
textAnchor"start"|"middle"|"end"|"justify"|`${number}%`|functionThe 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|functionAn SVG path
d(or a(datum, index)accessor returning one) for the text to follow, in coordinates relative to the box'sxandy. The text is drawn as one line with its baseline on the path, wrapped to the path's length paststartOffset(and cut short with theellipsisandtruncateWord).textAnchorplaces 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.translatefunctionDefines 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|functionLets 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 tofalse, which only breaks between words, and draws nothing when the first word does not fit.verticalAlign"top"|"middle"|"bottom"|functionThe vertical alignment of the lines in each box.
widthnumber|functionThe width for each text box.
xnumber|functionThe x position (left edge) for each text box.
ynumber|functionThe y position (top edge) for each text box.