我正在构建一个D3 treemap 可视化,但遇到了一些问题。
我已经能够填充我的初始树状图,但我想通过点击交互引入修改。为了做到这一点,我需要在每次点击时重新计算新的布局(然后稍后对其进行一些处理),使用先前(当前显示)数据的子集来执行此操作。
我编写了一个sum函数,它按照我想要的方式工作,并填充了
这段话的意思是:“这应该会在 new_node 和其所有子节点上填充计算出来的 x0、x1、y0 和 y1 值。然而,new_node 和其所有子节点都是 NaN。我已经上传了我的代码到 Gist 上,请点击此处查看。”
我已经能够填充我的初始树状图,但我想通过点击交互引入修改。为了做到这一点,我需要在每次点击时重新计算新的布局(然后稍后对其进行一些处理),使用先前(当前显示)数据的子集来执行此操作。
我编写了一个sum函数,它按照我想要的方式工作,并填充了
new_root
对象中每个Node
的value
属性。据我所知,这是在new_root
上运行d3.treemap
的唯一先决条件。以下是代码片段:let treemap = d3.treemap()
.tile(d3.treemapResquarify)
.size([800, 400])
.round(true)
.paddingInner(1);
// later...
function object_is_direct_child(root, obj) {
return (root.children.filter(child => (child.id === obj.name)).length > 0);
}
// ...much later...
// Create and bind the click event.
let click = function(d) {
if (d.data.children.length > 0) {
// Use the hover ID to get the underlying name attr (e.g. "Noise-hover" -> "Noise")
let name = this.getAttribute("id").slice(0,-6);
// Select the new root node.
let new_root = root.children.find(c => c.id === name);
// Sum.
new_root = new_root.sum(node => ((object_is_direct_child(new_root, node)) ? node.n : 0));
// Here is the problem area. This doesn't work as expected.
treemap(new_root);
}
}
hover_rects.on("click", click);
这段话的意思是:“这应该会在 new_node 和其所有子节点上填充计算出来的 x0、x1、y0 和 y1 值。然而,new_node 和其所有子节点都是 NaN。我已经上传了我的代码到 Gist 上,请点击此处查看。”