将链接附加到D3树布局中矩形的边上
Append links to side of rectangles in D3 tree layout
我正在尝试更改每个path
,以便它们从每个矩形的右侧开始并连接到左侧。目前,它们似乎"穿过"矩形,并在矩形内的某个地方有一个起点/终点,这并不能补偿rect
的大小。
我尝试过操纵这个代码:
var diagonal = d3.svg.diagonal()
.source(function (d) { return { "x": d.source.y, "y": d.source.x }; })
.target(function (d) { return { "x": d.target.y - 10, "y": d.target.x }; })
.projection(function (d) { return [d.y + 0, d.x + 0];
});
但这只会以灾难告终,但我相信答案就在那里。
这是一个脚本的JSFiddlehttp://jsfiddle.net/ra26wnbb/.
更新我已经看到D3树方形节点不在所需的位置,但我不确定它是否适用于我的文本包装。
首先,在wrap()
内部的工作完成后,您需要将生成的高度存储在文本的基准面上:d.height = 19 * (lineNumber + 1);
。这样,高度就可以用于任何需要它的人。例如,您可以使用它从wrap()
之外设置rect
高度,而不是parentNode.children[0]
,这是更好的关注点分离。无论如何,这就是wrap()
最终的样子:
function wrap(text, width) {
text.each(function (d) { // DIFF add param d
var text = d3.select(this),
// DIFF: use datum to get name, instead of element text
words = d.name.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);
}
}
// DIFF: store the height via the datum, d
d.height = 19 * (lineNumber + 1);
d3.select(this.parentNode.children[0]).attr('height', 19 * (lineNumber + 1));
});
}
});
既然有了d.height
,就可以使用它来计算对角线端点的必要偏移。
// calculate source and target offsets
// d.height gets set by the wrap() function
var diagonal = d3.svg.diagonal()
.source(function (d) {
return {
"x": d.source.x + d.source.height / 2,
"y": d.source.y + 150
};
})
.target(function (d) {
return {
"x": d.target.x + d.target.height / 2,
"y": d.target.y
};
})
.projection(function (d) { return [d.y + 0, d.x + 0];
});
参见修改后的fiddle
相关文章:
- 在链接d3强制布局中添加和删除类
- 使用jQuery库Packey来制作卡片布局,我可以用导航链接重新订购
- 树布局中的d3.js链接分离
- 将链接附加到D3树布局中矩形的边上
- 链接D3.js力布局网络的阈值函数中的笔划宽度
- 在从ruby on rails中的资产管道加载的布局上链接Javascript函数
- 在强制布局 D3.js 中保持节点之间的动态链接距离
- 缩放 D3 力布局链接标记上的箭头
- D3 群集布局中的链接和文本路径
- D3:方向图类似于树形布局,但带有反向链接
- 使用许多节点和链接加快d3部队布局
- d3.js:如何偏移树布局中链接的起点和终点
- D3力布局应在单击节点时添加节点和链接
- 在d3部队布局中设置链接
- OneDrive共享链接品牌或更改布局
- 自动生成链接在d3强制布局
- 我如何在不影响布局的情况下将链接放在选项1,选项2等
- 设置最大长度的链接在D3树布局
- D3可折叠力布局可变链接长度
- D3强制布局节点间固定链接