javascript - 从右到左的冰柱树
问题描述
如何镜像 d3 树?我已经使用了这些代码https://observablehq.com/@d3/zoomable-icicle但我不能把父亲放在正确的位置。我认为是attr("transform", d => translate(${d.y0},${d.x0}))
和我尝试过的功能
positions(d){
console.log(d.depth)
if (d.depth == 0){
d = d.children[0].children[0].children[0];
return (d.x0);
}
else if(d.depth == 1){
d = d.children[0];
return (d.x0);
}
else if(d.depth == 2){
d = d.parent;
return (d.x0);
}
else{
return 0;
}
}
但它不起作用。
解决方案
这是一个例子。
我们可以定义一个函数来获取镜像的水平位置:
function getFlippedPosition(d) {
return width - ((d.depth + 1) * (d.y1 - d.y0));
}
然后创建组变成
const cell = svg
.selectAll("g")
.data(root.descendants())
.join("g")
.attr("transform", d => `translate(${getFlippedPosition(d)},${d.x0})`)
类似地,clicked
函数可以更新如下:
function clicked(event, p) {
focus = focus === p ? p = p.parent : p;
root.each(d => d.target = {
x0: (d.x0 - p.x0) / (p.x1 - p.x0) * height,
x1: (d.x1 - p.x0) / (p.x1 - p.x0) * height,
y0: d.y0 - p.y0,
y1: d.y1 - p.y0,
// this line is new:
depth: d.depth - p.depth,
});
const t = cell.transition().duration(750)
// this line is updated:
.attr("transform", d => `translate(${getFlippedPosition(d.target)},${d.target.x0})`)
rect.transition(t).attr("height", d => rectHeight(d.target));
text.transition(t).attr("fill-opacity", d => +labelVisible(d.target));
tspan.transition(t).attr("fill-opacity", d => labelVisible(d.target) * 0.7);
}
推荐阅读
- python - 合并在熊猫中返回空数据框
- haskell - 无法编译 xmonad.hs 文件
- javascript - 尝试使用 JS 动态显示隐藏在样式表中的元素
- slack - 用于在工作区中安装应用程序的 Slack API
- java - 数据库类型程序中的枚举与类?
- amazon-web-services - 如何使用 Elastic Load Balancer 和 EC2/Nginx 解决 HTTPS 请求的 502 Bad Gateway 错误?
- android - 如何为 AWS EC2 上托管的 Parse 服务器获取 APP_ID 和 CLIENT_KEY
- python - 根据每个主索引的最后一个二级索引选择所有行
- javascript - ReactJS 状态更新在第二个 onChange
- java - Validator 类中的依赖关系未在单元测试中初始化