可调整大小的矩形节点在D3折叠树
Resizable rectangles for nodes in D3 collapsible tree
我修改了可折叠树的例子,用矩形代替圆形。
<代码>代码> https://plnkr.co/edit/UKUufJItPQqIKH8DBGLx?p =
预览我的问题是如何根据节点的文本来调整矩形高度。如果你注意到顶部节点有溢出的文本而
的大小我知道。getbbox()和规范的包装的例子,但因为我仍然是新手D3,我想知道如何把它放在一起,让它的工作。
规范包装:
function wrap(text, width) {
text.each(function() {
var text = d3.select(this),
words = text.text().split(/'s+/).reverse(),
word,
line = [],
lineNumber = 0,
lineHeight = 1.1, // ems
y = text.attr("y"),
dy = parseFloat(text.attr("dy")),
tspan = text.text(null).append("tspan").attr("x", 0).attr("y", y).attr("dy", dy + "em");
while (word = words.pop()) {
line.push(word);
tspan.text(line.join(" "));
if (tspan.node().getComputedTextLength() > width) {
line.pop();
tspan.text(line.join(" "));
line = [word];
tspan = text.append("tspan").attr("x", 0).attr("y", y).attr("dy", ++lineNumber * lineHeight + dy + "em").text(word);
}
}
});
}
nodeHeight = 40, nodeWidth = 150;
nodeUpdate.selectAll("text").call(wrap,nodeWidth);
nodeUpdate.select('rect')
.attr('rx', 6)
.attr('ry', 6)
.attr('y', -(nodeHeight / 2))
.attr('width', nodeWidth)
.attr('height', nodeHeight)
.style('fill', function(d) { return d._children ? 'lightsteelblue' : '#fff'; });
使用getBBox () :
nodeUpdate.select('rect')
.attr('rx', 6)
.attr('ry', 6)
.attr('y', -(nodeHeight / 2))
.attr('width', function(d){
var textElement = d3.select(this.parentNode).select("text").node();
var bbox = textElement.getBBox();
var width = bbox.width;
return width*2;
})
.attr('height', nodeHeight)
.style('fill', function(d) { return d._children ? 'lightsteelblue' : '#fff'; });
砰砰作响:https://plnkr.co/edit/KtSfKp8mpwnMXElfpC9r?p=preview
相关文章:
- 为d3.js可折叠树设置背景
- d3.js强制可折叠图-输入数据是对象数组
- 使用 d3.xml 而不是 d3.json 绘制可折叠缩进树
- 从 CSV 数据创建 D3.js 可折叠树
- 如何使用合并数组到 D3 可折叠树
- D3 可折叠力布局混淆了孩子和父母
- 更改所选节点样式,d3.js可折叠缩进树
- D3 可折叠树:树的可滚动容器
- 如何将缩放添加到 d3 可折叠树示例中
- 具有非树数据的可折叠 D3 力定向图
- d3.js可折叠/可扩展的带标签的力图表
- d3强制可折叠布局-所有节点都折叠的起始页
- 将图折叠到D3中的单个点
- d3可折叠搜索树-在每次搜索中删除旧的stokes
- 启动d3强制布局折叠,文本标签重复
- D3.js中可折叠树中的链接
- D3可折叠树不同的节点颜色
- D3折叠集群布局
- 是否有一种方法可以自动打开D3折叠树节点逐个节点(对于有子节点的节点)
- 可调整大小的矩形节点在D3折叠树