d3.js treemap错误的矩形大小
我目前正在研究一个互动d3.js treemap,从 https:// observablehq.com上强烈启发。 /@d3/Zoomable-Treemap 。但是,我在“ Maxillopoda”组的组成部分上的内部矩形大小遇到了问题,如您所见 https://jsfiddle.net/charlottdle/charlottean/charlotteandre/rjy2pb4x/rjy2pb4x/4/4 x即使我的价值为1,小组也比许多小组大。问题可能来自此功能
function position(group, root) {
group.selectAll("g")
.attr("transform", d => d === root ? `translate(0,-30)` : `translate(${x(d.x0)},${y(d.y0)})`)
.select("rect")
.attr("width", d => d === root ? width : x(d.x1) - x(d.x0))
.attr("height", d => d === root ? 30 : y(d.y1) - y(d.y0));
}
i'm currently working on an interactive D3.js Treemap, strongly inspired from https://observablehq.com/@d3/zoomable-treemap. However I got a problem with the size of inner rectangle on the composing part of "Maxillopoda" group as you can see in
https://jsfiddle.net/CharlotteAndre/rjy2pb4x/4/ the last "Halopitilus" group is larger than many groups even if I got a value of 1. The problem might be coming from this function
function position(group, root) {
group.selectAll("g")
.attr("transform", d => d === root ? `translate(0,-30)` : `translate(${x(d.x0)},${y(d.y0)})`)
.select("rect")
.attr("width", d => d === root ? width : x(d.x1) - x(d.x0))
.attr("height", d => d === root ? 30 : y(d.y1) - y(d.y0));
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发现使用节点值和Sort函数的
treemap = data => d3.treemap()
.tile(tile)(d3.HierArchy(数据)
.sum(d => d.值)
。
我更改了您的数据以删除与父母相关的值。您要么分配儿童价值观,要么不分配。这种逻辑会弄乱您的层次结构。在下面运行代码,让我知道您想要的是否足够。我已经更改了宽度和高度以更好地观看。
更新而不更改数据
I found issue with your treemap building using value of nodes and sort function
let treemap = data => d3.treemap()
.tile(tile)(d3.hierarchy(data)
.sum(d => d.value)
.sort((a, b) => b.value - a.value))
When you are sorting based on sum of values, dont assign 0 value to parent nodes. I changed your data to remove the values associated with parents. Either you assign sum of children values or dont assign. This logic will mess up your hierarchy. Run the code below and let me know if this suffice on what you want. I have changed the width and height for better viewing.
UPDATE without changing data