Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrapping Text in D3

Tags:

I would like to get the text to wrap on the following D3 tree so that instead of

Foo is not a long word 

each line is wrapped to

Foo is not a long word 

I have tried making the text a 'foreignObject' rather than a text object and the text does indeed wrap, but it doesn't move on the tree animation and is all grouped in the upper left hand corner.

Code located at

http://jsfiddle.net/mikeyai/X43X5/1/

Javascript:

var width = 960,     height = 500;  var tree = d3.layout.tree()     .size([width - 20, height - 20]);  var root = {},     nodes = tree(root);  root.parent = root; root.px = root.x; root.py = root.y;  var diagonal = d3.svg.diagonal();  var svg = d3.select("body").append("svg")     .attr("width", width)     .attr("height", height)   .append("g")     .attr("transform", "translate(10,10)");  var node = svg.selectAll(".node"),     link = svg.selectAll(".link");  var duration = 750,     timer = setInterval(update, duration);  function update() {   if (nodes.length >= 500) return clearInterval(timer);    // Add a new node to a random parent.   var n = {id: nodes.length},       p = nodes[Math.random() * nodes.length | 0];   if (p.children) p.children.push(n); else p.children = [n];   nodes.push(n);    // Recompute the layout and data join.   node = node.data(tree.nodes(root), function(d) { return d.id; });   link = link.data(tree.links(nodes), function(d) { return d.source.id + "-" + d.target.id; });    // Add entering nodes in the parent’s old position.   node.enter().append("text")       .attr("class", "node")       .attr("x", function(d) { return d.parent.px; })       .attr("y", function(d) { return d.parent.py; })         .text('Foo is not a long word');    // Add entering links in the parent’s old position.   link.enter().insert("path", ".node")       .attr("class", "link")       .attr("d", function(d) {         var o = {x: d.source.px, y: d.source.py};         return diagonal({source: o, target: o});       });    // Transition nodes and links to their new positions.   var t = svg.transition()       .duration(duration);    t.selectAll(".link")       .attr("d", diagonal);    t.selectAll(".node")       .attr("x", function(d) { return d.px = d.x; })       .attr("y", function(d) { return d.py = d.y; }); } 
like image 997
user235236 Avatar asked Jul 16 '14 15:07

user235236


1 Answers

You can modify Mike Bostock's "Wrapping Long Labels" example to add <tspan> elements to your <text> nodes. There are two major changes required to add wrapped text to your nodes. I didn't delve into having the text update its position during transitions, but it shouldn't be too hard to add.

The first is to add a function wrap, based off of the function in the above example. wrap will take care of adding <tspan> elements to make your text fit within a certain width:

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             x = text.attr("x"),             y = text.attr("y"),             dy = 0, //parseFloat(text.attr("dy")),             tspan = text.text(null)                         .append("tspan")                         .attr("x", x)                         .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", x)                             .attr("y", y)                             .attr("dy", ++lineNumber * lineHeight + dy + "em")                             .text(word);             }         }     }); } 

The second change is that instead of setting the text of each node, you need to call wrap for each node:

// Add entering nodes in the parent’s old position. node.enter().append("text")     .attr("class", "node")     .attr("x", function (d) { return d.parent.px; })     .attr("y", function (d) { return d.parent.py; })     .text("Foo is not a long word")     .call(wrap, 30); // wrap the text in <= 30 pixels 
like image 165
mdml Avatar answered Sep 17 '22 19:09

mdml