1
在上面的代碼中我的.append(「text」)dosent work.I沒有在我的鏈接上插入任何文本。 我試過使用link.append(「路徑」)..使用這個我可以看到文本,但不是鏈接。我想使用 link.insert(「路徑」),並且仍然能夠添加文本,並能夠與鏈接 文本一起摺疊和展開節點。請幫助無法在d3可摺疊樹中的鏈接上添加文本
var link = svg.selectAll("path.link")
.data(links, function (d) { return d.target.id; });
// Enter any new links at the parent's previous position.
// var link1=link.enter();
link.enter().insert("path", "g")
.attr("class", "link")
.attr("d", function (d) {
var o = { x: source.x0, y: source.y0 };
return diagonal({ source: o, target: o });
});
link.enter()
.append("g")
.attr("class", "link")
.append("text")
.attr("font-family", "Arial, Helvetica, sans-serif")
.attr("fill", "Black")
.style("font", "normal 12px Arial")
.attr("transform", function(d) {
return "translate(" +
((d.source.y + d.target.y)/2) + "," +
((d.source.x + d.target.x)/2) + ")";
})
.attr("dy", ".35em")
.attr("text-anchor", "middle")
.text(function(d) {
console.log(d.target.name);
return d.target.name;
});
看起來像這個例子被用作上述的基礎:http://bl.ocks.org/mbostock/4339083 – 2014-09-02 13:30:23