我试图做一个强制导向的布局,其中的链接是指向节点的箭头(如示例here和here所示),并且还有可以折叠的子节点(如Mike Bostock的示例所示: here或here)。D3js可定向路径的可折叠力布局?
到目前为止折叠节点工作正常,但我无法理解如何将箭头包含在路径中。下面是我的代码部分,根据上面的例子:
force.nodes(nodes)
.links(links)
.gravity(0.05)
.charge(-1500)
.linkDistance(100)
.friction(0.5)
.linkStrength(function(l, i) {return 1 })
.size([w, h])
.start();
// Append markers
vis.append("svg:defs").selectAll("marker")
.data(["end"])
.enter().append("svg:marker")
.attr("id", String)
.attr("viewBox", "0 -5 10 10")
.attr("refX", 15)
.attr("refY", -1.5)
.attr("markerWidth", 6)
.attr("markerHeight", 6)
.attr("orient", "auto")
//.append("svg:path") // <-- I not sure what this does
//.attr("d", "M0,-5L10,0L0,5");
var path = vis.selectAll("path")
.data(force.links());
// Enter new paths
path.enter().insert("svg:path")
.attr("class", "link")
.attr("marker-end", "url(#end)")
.style("stroke", "#ccc");
// Exit any old paths.
path.exit().remove();
// Update the nodes…
var node = vis.selectAll("g.node")
.data(nodes, function(d) { return d.id; })
node.select("circle")
.style("fill", color);
// Enter any new nodes.
var nodeEnter = node.enter().append("svg:g")
.attr("class", "node")
.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; })
.on("click", click)
.call(force.drag);
//Add an immage to the node
nodeEnter.append("svg:image")
.attr("xlink:href", function(d) { return d.image;})
.attr("x", function(d) { return (0 - Math.sqrt(d.size))/10 || 4.5;})
.attr("y", function(d) { return (0 - Math.sqrt(d.size))/10 || 4.5;})
.attr("height", 16)
.attr("width", 16);
// Exit any old nodes.
node.exit().remove();
// Re-select for update.
node = vis.selectAll("g.node");
path = vis.selectAll("path")
force.on("tick", function() {
// Draw curved links
path.attr("d", function(d) {
var dx = d.target.x - d.source.x,
dy = d.target.y - d.source.y,
dr = Math.sqrt(dx * dx + dy * dy);
return "M" + d.source.x + ","
+ d.source.y
+ "A" + dr + ","
+ dr + " 0 0,1 "
+ d.target.x + ","
+ d.target.y;
});
node.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; });
});
我明白的是,下面的代码段是负责绘制箭头头部,通过指定其中的箭头应指向的块(例如.data(["end"])
)
vis.append("svg:defs").selectAll("marker")
.data(["end"])
.enter().append("svg:marker")
.attr("id", String)
.attr("viewBox", "0 -5 10 10")
.attr("refX", 15)
.attr("refY", -1.5)
.attr("markerWidth", 6)
.attr("markerHeight", 6)
.attr("orient", "auto")
.append("svg:path")
.attr("d", "M0,-5L10,0L0,5");
被输入的路径时,这然后被引用(即.attr("marker-end", "url(#end)");
)。
但是我可能会错过一些东西,因为在我的图中显示了路径,但没有显示箭头。
感谢您的帮助!
好问题!天哪,我在和那些箭头奋斗。我期待着看到这里出现的情况。 – d3noob 2013-05-05 18:19:01
您在SVG标记定义中附加的“路径”负责绘制实际的箭头。你是否运行过你的代码(第一代码块)? – 2013-05-06 10:33:48
谢谢@LarsKotthoff,在我发布这个问题后,我发现了这一点。在它未被注释之前它没有工作,并且因为我不明白它做了什么,所以我评论了它。写'path.link'几乎可以修复它。我把我的答案放在下面。然而,仍然有一些东西阻止它完全正常工作...... – djjupa 2013-05-06 20:01:46