Javascript 如何更新链接并强制d3.cluster中的多个父级?
d3新手,如果有什么不合理/非常愚蠢的事情,请提前道歉 我目前在一个可折叠的集群布局中有一个系统发育树,并且正在尝试解决多个父母的问题。我没有足够的点数来发布图片,所以 在截图中,左侧的孤立节点(胡子草莺等)从右侧不再连接节点的链接移动。我想重新绘制这些链接,并将孤立节点连接到其第二个父节点(15601、15602等)。当前,节点移动到其新位置,但两个链接都没有绘制 我发现分层布局并不适用于具有多个父节点的节点,但我对d3的理解还不够透彻,无法找出原因。有没有办法绕过这个问题?我已经相当重视集群布局可视化数据的方式,所以我真的更愿意保留它,而不是像前面所描述的那样从一个force布局开始 那么,我的问题是:在d3.cluster中是否可以将两个父链接绘制到一个节点?看起来应该是这样的。如果是,怎么做? 如果没有,请提供更多资源,了解如何复制有效的集群布局 这里是更新函数-如果需要更多的代码部分,请告诉我:Javascript 如何更新链接并强制d3.cluster中的多个父级?,javascript,d3.js,Javascript,D3.js,d3新手,如果有什么不合理/非常愚蠢的事情,请提前道歉 我目前在一个可折叠的集群布局中有一个系统发育树,并且正在尝试解决多个父母的问题。我没有足够的点数来发布图片,所以 在截图中,左侧的孤立节点(胡子草莺等)从右侧不再连接节点的链接移动。我想重新绘制这些链接,并将孤立节点连接到其第二个父节点(15601、15602等)。当前,节点移动到其新位置,但两个链接都没有绘制 我发现分层布局并不适用于具有多个父节点的节点,但我对d3的理解还不够透彻,无法找出原因。有没有办法绕过这个问题?我已经相当重视集群
function update(source) {
// Compute the new tree layout.
var nodes = tree.nodes(root);
links = tree.links(nodes);
nodes.forEach(function(d) {
add_parents(d);
});
// Normalize for fixed-depth.
nodes.forEach(function(d) {
if (d.child_count === 0) {
d.y = h - 150
} else {
//do nothing
}
});
// Update the nodes…
var node = svg.selectAll("g.node")
.data(nodes, function(d) { return d.id || (d.id = ++i); });
// Enter any new nodes at the parent's previous position.
var nodeEnter = node.enter().append("g")
.attr("class", "node")
.attr("transform", function(d) { return "translate(" + source.y0 + "," + source.x0 + ")"; })
//this APPEARS to just cause it to update twice for no reason
.on("click", function(d) { toggle(d); update(root); });
nodeEnter.append("circle")
.attr("r", node_size)
.style("fill", function(d) { return (d.child_count > 0 && d._children) ? "#66cccc" : "#fff"; });
//switch out commented stuff in here for vertical vs horizontal tree
nodeEnter.append("text")
//.attr("x", function(d) { return d.children || d._children ? -10 : 10; })
//.attr("dy", ".35em")
//.attr("text-anchor", function(d) { return d.children || d._children ? "end" : "start"; })
.attr("dy", ".5em")
.attr("dx", function(d) { return ( (node_size(d)/16 + (d.name.length/4) + 1) + "em"); })
.attr("text-anchor", "middle")
.attr("transform", "rotate(45)")
.text(function(d) { return d.name; })
.style("fill-opacity", 1e-6);
// Transition nodes to their new position.
var nodeUpdate = node.transition()
.duration(duration)
.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; });
nodeUpdate.select("circle")
.attr("r", node_size)
.style("fill", function(d) { return (d.child_count > 0 && d._children) ? "#66cccc" : "#fff"; });
nodeUpdate.select("text")
.style("fill-opacity", 1);
// Transition exiting nodes to the parent's new position.
var nodeExit = node.exit().transition()
.duration(duration)
.attr("transform", function(d) { return "translate(" + source.y + "," + source.x + ")"; })
.remove();
nodeExit.select("circle")
.attr("r", 1e-6);
nodeExit.select("text")
.style("fill-opacity", 1e-6);
// Update the links…
var link = svg.selectAll("path.link")
//this one prevents new link from being drawn to moved node, but links enter from the right place
.data(links, function(d) { return d.target.id; });
// Enter any new links at the parent's previous position.
link.enter().insert("path", "g")
.attr("class", "link")
.style("stroke-opacity", link_opacity)
//.style("stroke-width", link_width)
.style("stroke", link_color)
.attr("d", function(d) {
var o = {x: source.x0, y: source.y0};
return diagonal({source: o, target: o});
});
// Transition links to their new position.
link.transition()
.duration(duration)
.attr("d", diagonal);
// Transition exiting nodes to the parent's new position.
link.exit().transition()
.duration(duration)
.attr("d", function(d) {
var o = {x: source.x, y: source.y};
return diagonal({source: o, target: o});
})
.remove();
// Stash the old positions for transition.
nodes.forEach(function(d) {
d.x0 = d.x;
d.y0 = d.y;
});
} // function update()
让你的问题尽可能简短。它使读者的生活变得轻松。