function wrap(text, width) {
console.log(text, width);
text.each(function () {
const text = d3.select(this);
console.log(text);
const words = text.text().split(/\s+/).reverse();
console.log(words);
let word = "";
let line = [];
const lineHeight = 1.1;
const x = text.attr("x");
let tspan = text.text(null).append("tspan").attr("x", x);
while ((word = words.pop())) {
line.push(word);
tspan.text(line.join(" "));
if (tspan.node().getComputedTextLength() > width) {
line.pop();
tspan.text(line.join(" "));
line = [word];
tspan = text
.append("tspan")
.attr("x", x)
.attr("dy", lineHeight + "em")
.text(word);
}
}
});
const breaks = text.selectAll("tspan").size();
console.log(breaks);
text.attr("y", function () {
return +text.attr("y") + -6 * (breaks - 1);
});
}