textWrap
Based on the defined styles and dimensions, breaks a string into an array of strings for each line of text.
Basic example
Section titled “Basic example”textWrap() returns a generator: configure it, then call it with a string. width(200) is the only setting here, so the sentence is broken into as many 200px lines as it needs at the default 10px font, and widths reports the measured pixel width of each line.
import {textWrap} from "@d3plus/text";
const wrap = textWrap().width(200);
wrap(
"The quick brown fox jumps over the lazy dog while the sun sets behind the hills.",
);
Circle shape
Section titled “Circle shape”shape("circle") limits each line to the chord of the circle at that line’s height, so lines near the top and bottom are shorter than those through the middle. This is how labels fit inside Pack and Pie shapes.
import {textWrap} from "@d3plus/text";
const wrap = textWrap().shape("circle").width(220).height(220).fontSize(14);
wrap(
"The quick brown fox jumps over the lazy dog while the sun sets behind the hills.",
);
Font styling
Section titled “Font styling”fontSize, fontWeight, and fontFamily feed the measurement, so a bold 18px Inter line holds fewer words than the 10px default. The result re-measures once the web font has loaded.
import {textWrap} from "@d3plus/text";
const wrap = textWrap()
.width(240)
.fontSize(18)
.fontWeight(700)
.fontFamily("Inter");
wrap(
"The quick brown fox jumps over the lazy dog while the sun sets behind the hills.",
);
Max lines truncation
Section titled “Max lines truncation”maxLines(2) keeps only the first two lines and sets truncated: true so a caller such as TextBox knows to add an ellipsis.
import {textWrap} from "@d3plus/text";
const wrap = textWrap().width(160).maxLines(2);
wrap(
"The quick brown fox jumps over the lazy dog while the sun sets behind the hills.",
);
Ellipsis and cut words
Section titled “Ellipsis and cut words”ellipsis ends a truncated wrap’s last line: a string to append, or a function (text, line) => string. The ended line is measured, and words come off its end until it fits the width. truncateWord(true) cuts the word that did not fit (or a first word wider than the line) to the longest start that fits with the ellipsis, between whole characters so an emoji is never split. nextWord is the index in words of the first word that did not fit.
import {textWrap} from "@d3plus/text";
const byWord = textWrap().width(115).maxLines(1).ellipsis("...");
byWord("Keep going until the box runs out of room");
const byCharacter = textWrap()
.width(115)
.maxLines(1)
.ellipsis("...")
.truncateWord(true);
byCharacter("Keep going until the box runs out of room");
Words for justified text
Section titled “Words for justified text”lineWords splits each line into the units justified text spreads apart, a word with the spaces after it or a single CJK character or emoji, with their widths (not counting those spaces). hardBreaks marks the lines that end at a newline, which justified text leaves at the start. TextBox uses both for textAnchor: "justify".
import {textWrap} from "@d3plus/text";
const wrap = textWrap().width(160);
wrap("Justified text spreads the words of each line apart.").lineWords;
Parameters
import {textWrap} from "@d3plus/text";
Takes no parameters.