function wrap(text, { pad, m }) {
text.each(function () {
const content = d3
.select(this)
.style("font-size", "1.2em")
.style("font-weight", "400")
.style("font-family", "Roboto"),
words = content.text().split(/\s+/).reverse();
let word;
let line = [],
lineNumber = 0;
const lineHeight = 1,
y = content.attr("y"),
dy = parseFloat(content.attr("dy"));
let tspan = content
.text(null)
.append("tspan")
.attr("x", m + pad / 2)
.attr("y", y)
.attr("dy", `${dy}em`)
.attr("text-anchor", "middle");
while ((word = words.pop())) {
line.push(word);
tspan.text(line.join(" "));
if (tspan.node().getComputedTextLength() > pad - m * 2) {
line.pop();
tspan.text(line.join(" "));
line = [word];
lineNumber = ++lineNumber;
tspan = content
.append("tspan")
.attr("x", m)
.attr("y", y)
.attr("text-anchor", "middle")
.attr("dy", () => content.attr("height") + lineHeight + "em")
.text(word);
}
}
content
.attr("fill", "rgba(0,0,0,0.87)")
.attr("opacity", 0.7)
.style(
"transform",
`translate(${-pad}px,-${(lineNumber * lineHeight) / 2}em)`
)
.attr("text-anchor", "middle");
});
}