在 D3 中包装文本 [英] Wrapping Text in D3
问题描述
我想让文本在下面的 D3 树上换行,而不是
I would like to get the text to wrap on the following D3 tree so that instead of
Foo is not a long word
每一行都换行
Foo is
not a
long word
我尝试将文本设为foreignObject"而不是文本对象,并且文本确实会换行,但它不会在树动画上移动,而是全部集中在左上角.
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.
代码位于
http://jsfiddle.net/mikeyai/X43X5/1/
Javascript:
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; });
}
推荐答案
您可以修改 Mike Bostock 的Wrapping Long"标签"示例 将
元素添加到您的
节点.向节点添加换行文本需要进行两项主要更改.我没有深入研究让文本在过渡期间更新其位置,但添加它应该不会太难.
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.
首先是添加一个函数 wrap
,基于上面例子中的函数.wrap
将负责添加
元素以使您的文本适合特定宽度:
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);
}
}
});
}
第二个变化是不需要设置每个节点的文本,而是需要为每个节点调用wrap
:
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
这篇关于在 D3 中包装文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!