【发布时间】:2021-01-19 18:59:51
【问题描述】:
我有一个使用 d3.hierarchy 的树状图/集群图的根。我正在尝试用一个应该成为新头的选定节点来更新根,并在顶部用该节点绘制一棵新树。这应该替换旧树。步骤如下:
- 读入平面数据
- 使用 d3.stratify 转换为层次结构
- 将其转换为集群(带有坐标等)
- 使用新的 select.join 绘制(不再需要显式退出/删除)
- 用户点击节点的圈子
- 更新层次结构,将选定节点作为新根并删除父节点
- 重新绘制,数据中不再存在的节点(父级及以上)被连接移除
但是,它重新绘制了新的、更小的根和依赖项,但所有旧的 SVG 仍然存在。我已经尝试明确添加退出/删除,但这没有帮助。
我做错了什么?
可以在此处查看简化的、可重现的示例。我还在https://jsfiddle.net/colourblue/zp7ujra3/9/创建了一个小提琴
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://d3js.org/d3.v6.js"></script>
</head>
<body>
<div id="vis"></div>
<script>
let treeData = []
let currentTreeData = []
var flatData = [
{ "ID" : 1000, "name" : "The Root", "parentID":null},
{ "ID" : 1100, "name" : "Child 1", "parentID":1000 },
{ "ID" : 1110, "name" : "G.Child 1.1", "parentID":1100 },
{ "ID" : 1120, "name" : "G.Child 1.2", "parentID":1100 },
{ "ID" : 1130, "name" : "G.Child 1.3", "parentID":1100 },
{ "ID" : 1200, "name" : "Child 2", "parentID":1000 },
{ "ID" : 1210, "name" : "G.Child 2.1", "parentID":1200 },
{ "ID" : 1211, "name" : "G.G.Child 2.1.1", "parentID":1210 },
{ "ID" : 1212, "name" : "G.G.Child 2.2.2", "parentID":1210 },
{ "ID" : 12111, "name" : "G.G.G.Child 2.1.1.1", "parentID":1211 },
{ "ID" : 1300, "name" : "Child 3", "parentID":1000 }
];
function chart(thisTreeData) {
let root = clusterise(thisTreeData)
// Add nodes (links)
svg.append("g")
.attr("class", "node")
.attr("fill", "none")
.attr("stroke", "#555")
.attr("stroke-opacity", 0.3)
.selectAll("path")
.data(root.links(), function(d) { return "Link" + ":" + d.target.data.id })
.join("path")
.attr("d", d3.linkRadial()
.angle(d => d.x)
.radius(d => d.y));
// Add circles
svg.append("g")
.attr("class", "node")
.selectAll("circle")
.data(root.descendants(), function(d) { return "Circle" + d.data.id; })
.join("circle")
.attr("transform", d => `
rotate(${d.x * 180 / Math.PI - 90})
translate(${d.y},0)
`)
.attr("r", 3)
.on('click', click);
// Add text
svg.append("g")
.attr("class", "node")
.selectAll("text")
.data(root.descendants(), function(d) { return "Text" + d.data.id; })
.join("text")
.attr("transform", d => `
rotate(${d.x * 180 / Math.PI - 90})
translate(${d.y},0)
rotate(${d.x >= Math.PI ? 180 : 0})
`)
.attr("text-anchor", d => d.x < Math.PI === !d.children ? "start" : "end")
.text(d => d.data.data.name);
}
// Switch tree on click so centre is now selected node
function click(event,d) {
currentTreeData = findNode(treeData, d.data.id)
chart(currentTreeData);
}
// HELPER FUNCTIONS
// ----------------
// Function to Strafify flat CSV data into a tree
function convertToHierarchy(data) {
var stratify = d3.stratify()
.parentId(function (d) {
return d.parentID;
})
.id(function (d) {
return d.ID;
});
let treeData = stratify(data);
return (treeData)
}
// Function to Create d3 cluster with coordinates etc from stratified data
function clusterise(treeData) {
tree = d3.cluster().size([2 * Math.PI, radius - 100])
let root = tree(d3.hierarchy(treeData)
.sort((a, b) => d3.ascending(a.name, b.name)));
return (root)
}
function findNode(root, id) {
console.log(root);
let selected = root.find(obj => obj.id === id);
selected.parent= null;
console.log(selected);
return(selected)
}
width = 800
height = 600
radius = width / 2
let svg = d3.select("#vis")
.append('svg')
.attr('width', width)
.attr('height', height)
.append('g')
.attr('transform', 'translate(' + width / 2 + ',' + height / 2 + ')');
treeData = convertToHierarchy(flatData)
currentTreeData = treeData
chart(currentTreeData);
</script>
</body>
</html>
【问题讨论】:
-
您是否只想覆盖您的图表并重新绘制?只需在绘图函数的顶部添加一个 `svg.selectAll('*').remove();`。见here。
-
非常感谢您回复我,马克。我可以看到工作有点工作,但我不明白为什么我的版本没有更新以反映新数据(所选节点上方的所有内容都已被删除)。我打算向它添加一些过渡,如果我执行 selectAll(*").remove,整个树将被删除并重建,而不是删除已删除的部分并移动更新的部分。谢谢你,克里斯。
-
您的问题有两个方面。您根本不处理输入,更新,退出。并且您的函数内的包装器
gs (每次单击都会附加)意味着您的后续 .selectAll将永远找不到最后附加的项目。请参阅下面的答案。 -
它运行良好,我可以看到在函数中添加组的问题。但是,在进入、更新、退出方面,我原以为这些的默认实现现在包含在下面的文章中的新连接方法中,但也许我错了。我将看看单独移动包装组的效果是什么。再次感谢您的帮助,@ChrisWoodsSays。 fabiofranchino.com/blog/…
-
从docs,如果你只是做
.join('path').attr...,它相当于the identity function and calling selection.remove, respectively。对于更高级的转换,您需要明确提供这些功能。老实说,我不喜欢新的.join;太多的魔法。人们应该学习旧模式。
标签: javascript d3.js hierarchy