我正在像这样将节点添加到力布局图中:
var node = vis.selectAll("circle.node")
.data(nodes)
.enter()
.append("circle")
.attr("class", "node")
.attr("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; })
.attr("r", 5)
.style("fill", function(d) { return fill(d.group); })
.call(force.drag);
有没有办法将复合 SVG 元素添加为节点? IE。我想为每个圈子添加一个超链接,所以我需要这样的东西:
<a href="whatever.com"><circle ...></circle></a>
最佳答案
创建“复合”元素就像将一个或多个子元素附加到另一个元素一样简单。在您的示例中,您希望将数据绑定(bind)到选择的 <a>
。元素,并给每个 <a>
单个<circle>
child 。
首先,您需要选择"a.node"
而不是 "circle.node"
.这是因为您的超链接将成为父元素。如果没有明显的父元素,而您只想为每个数据添加多个元素,请使用 <g>
, SVG 的组元素。
然后,您要附加一个 <a>
元素到输入选择中的每个节点。这将创建您的超链接。设置每个超链接的属性后,您要给它一个 <circle>
child 。简单:只需调用 .append("circle")
.
var node = vis.selectAll("a.node")
.data(nodes);
// The entering selection: create the new <a> elements here.
// These elements are automatically part of the update selection in "node".
var nodeEnter = node.enter().append("a")
.attr("class", "node")
.attr("xlink:href", "http://whatever.com")
.call(force.drag);
// Appends a new <circle> element to each element in nodeEnter.
nodeEnter.append("circle")
.attr("r", 5)
.style("fill", function(d) { return fill(d.group); })
node.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; });
请记住,D3 主要对节点的选择 进行操作。所以打电话.append()
在进入选择 时意味着选择中的每个节点都有一个新的子节点。强大的东西!
还有一件事:SVG 有 its own <a>
element ,这就是我上面提到的。这与 HTML 不同!通常,您只将 SVG 元素与 SVG 一起使用,将 HTML 与 HTML 一起使用。
感谢@mbostock 建议我澄清变量命名。
关于javascript - 如何在 D3 强制布局中添加复合节点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9206732/