我正在尝试渲染d3树形图,以使数据集从中心节点的两侧扩展出来。我一直在研究类似this的示例,并希望保留拖放功能。我能找到的最接近的示例是:
Tree with children towards multiple side in d3.js (similar to family tree)
到目前为止,我已经尝试通过将d.y
函数中的viewerWidth
变量减去update(source)
值来将其切换为负值。这只是使d3树形图的方向仅从右向左反转。
function update(source) {
//Other code
// Set widths between levels based on maxLabelLength.
nodes.forEach(function(d) {
d.y = viewerWidth - (d.depth * (maxLabelLength * 10)); //maxLabelLength * 10px
// alternatively to keep a fixed scale one can set a fixed depth per level
// Normalize for fixed-depth by commenting out below line
// d.y = (d.depth * 500); //500px per level.
});
//Other code
}
如果我将其应用于nodes数组中的某些条目,则树确实会划分,但x值间隔已消失:
function update(source) {
//Other code
// Set widths between levels based on maxLabelLength.
nodes.forEach(function(d,i) {
if(i < nodes.length / 2) {
d.y = viewerWidth - (d.depth * (maxLabelLength * 10)); //maxLabelLength * 10px
}else {
d.y = (d.depth * (maxLabelLength * 10));
}
// alternatively to keep a fixed scale one can set a fixed depth per level
// Normalize for fixed-depth by commenting out below line
// d.y = (d.depth * 500); //500px per level.
});
//Other code
}
另一种解决方案是在一个方向上渲染树,然后尝试随后操纵节点的位置,但这并不理想。
如何使用一个数据集创建双向节点树?