【发布时间】:2013-05-05 10:55:06
【问题描述】:
我正在尝试做一个强制导向的布局,其中链接是指向节点的箭头(如 here 和 here 所示的示例中所示),并且具有子节点的节点也是可折叠的(如图所示在 Mike Bostock 的示例中:here 或 here)。
到目前为止,折叠节点工作正常,但我无法理解箭头是如何包含在路径中的。这是我的代码的一部分,基于上面的例子:
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)");)。
但我可能会遗漏一些东西,因为在我的图表中显示的是路径,而不是箭头。
感谢您的帮助!
【问题讨论】:
-
好问题!天哪,我与那些箭头作斗争。我很期待看到这里会发生什么。
-
您在 SVG 标记定义中附加的
path负责绘制实际的箭头。您是否在注释掉的情况下运行代码(第一个代码块)? -
谢谢@LarsKotthoff,我在发布问题后就想通了。之前没有注释时它不起作用,因为我不明白它做了什么,所以我把它注释掉了。写
path.link几乎解决了它。我把我的答案放在下面。但是,仍然有一些东西阻止它完全正常工作......