Text-wrapping routine.
See original GitHub issueIt might be useful to expose this as d3.svg.textWrap?
function wrap(text, width) {
text.each(function() {
var text = d3.select(this),
words = text.text().split(/\s+/).reverse(),
word,
line = [],
lineNumber = 0,
lineHeight = 1.1, // ems
y = text.attr("y"),
dy = parseFloat(text.attr("dy")),
tspan = text.text(null).append("tspan").attr("x", 0).attr("y", y).attr("dy", dy + "em");
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", 0).attr("y", y).attr("dy", ++lineNumber * lineHeight + dy + "em").text(word);
}
}
});
}
Live example: http://bl.ocks.org/mbostock/7555321
Issue Analytics
- State:
- Created 10 years ago
- Reactions:10
- Comments:30 (2 by maintainers)
Top Results From Across the Web
How to wrap text in Excel automatically and manually - Ablebits
This tutorial shows how to wrap text in a cell automatically and how to insert a line break manually. You will also learn...
Read more >Combining text wrapping and shrinking in WPF - Stack Overflow
The magic happens in the final routine: Update . In WPF, if a given element is being clipped (i.e. it's bigger than the...
Read more >Word wrap - Rosetta Code
Show your routine working on a sample of text at two different wrap columns. Extra credit. Wrap text using a more sophisticated algorithm ......
Read more >PL/SQL Source Text Wrapping - Database - Oracle Help Center
To produce a wrapped file, use either the PL/SQL Wrapper utility or a DBMS_DDL subprogram. The PL/SQL Wrapper utility wraps the source text...
Read more >The 25-Minute Workout You Can Do On Your Lunch Break
[Text Wrapping Break]; Plan the work(out) and work the plan. Before you begin each exercise, you should consider what muscle group you'll be ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
If this does get added, there is a bug that I noticed when I used this recently. If the first word is wider than the specified width, a blank line is produced instead. Just making sure you don’t pop when there is only one word in the line solves the problem.
A follow up on bunkat’s comment, indeed first words that are longer than the provided width generate an empty tspan . An extra condition must be added to prevent this, from:
to: