question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Text-wrapping routine.

See original GitHub issue

It 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:closed
  • Created 10 years ago
  • Reactions:10
  • Comments:30 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
bunkatcommented, Mar 24, 2014

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.

if(line.length > 1) line.pop();
2reactions
emiguevaracommented, May 13, 2014

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:

if (tspan.node().getComputedTextLength() > width) { ... }

to:

if (tspan.node().getComputedTextLength() > width && line.length > 1) {...}
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found