我试图在D3中创建一个可折叠的树,它结合了水平(第一级和第二级)和垂直(3级)布局。这是我到目前为止的jsfiddle。除了一件事,它几乎就在那里。当两个兄弟部分扩展时,它们之间的距离增加得比应有的多。
我相信这种情况正在发生,因为D3认为子元素将以水平方式定位,因此它为它们分配了更多空间,但我在代码中覆盖它们的位置,以便它们垂直显示。我觉得我现在需要覆盖计算父节点之间距离的函数,但是不能确定哪一个是那个。
这是我的JS代码:
var margin = {
top: 20,
right: 20,
bottom: 20,
left: 20
},
width = 1140 - margin.right - margin.left,
height = 800 - margin.top - margin.bottom;
var i = 0,
duration = 750;
var tree = d3.layout.tree()
.size([height, width]);
var diagonal = d3.svg.diagonal()
.source(function (d) {
return {
"x": d.source.x,
"y": d.source.y
};
})
.target(function (d) {
return {
"x": d.target.x,
"y": d.target.y
};
});
var svg = d3.select("#body").append("svg")
.attr("width", "100%")
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
root.x0 = height / 2;
root.y0 = 0;
function collapse(d) {
if (d.children) {
d._children = d.children;
d._children.forEach(collapse);
d.children = null;
}
}
root.children.forEach(function (parent) {
collapse(parent);
});
update(root);
function update(source) {
// Compute the new tree layout.
var nodes = tree.nodes(root),
links = tree.links(nodes),
position = [];
nodes.forEach(function (d) {
if (d.parent) {
position[d.parent.id] = position[d.parent.id] || 1;
d.position = position[d.parent.id]++;
if (d.children && d.depth > 1) {
d.children.forEach(function (entry) {
position[d.parent.id]++;
});
}
}
if (d.depth < 2) {
d.y = d.depth * 60;
if (d.parent) {
columns = d.parent.children.length;
//d.x = (d.position - 1 + columns / 12) * width / columns;
} else {
columns = d.children.length;
//d.x = (width * (4 * columns - 1)) / (8 * columns);
// d.x = width / 2;
}
} else {
d.y = d.parent.y + d.position * 20;
d.x = d.parent.x + 20;
}
});
// Update the nodes…
var node = svg.selectAll("g.node")
.data(nodes, function (d) {
return d.id || (d.id = ++i);
});
// Enter any new nodes at the parent's previous position.
var nodeEnter = node.enter().append("g")
.attr("class", "node")
.attr("transform", function (d) {
return "translate(" + source.x0 + "," + source.y0 + ")";
})
.on("click", click);
nodeEnter.append("circle")
.attr("r", 1e-6)
.style("fill", function (d) {
return d._children ? "lightsteelblue" : "#fff";
});
nodeEnter.append("text")
.attr("x", function (d) {
return 10;
})
.attr("dy", ".35em")
.attr("text-anchor", function (d) {
return "start";
})
.text(function (d) {
//return d.name + " ("+d.x+","+d.y+")";
return d.name;
})
.each(function (d) {
d.width = this.getBBox().width;
})
.style("fill-opacity", 1e-6);
// Transition nodes to their new position.
var nodeUpdate = node.transition()
.duration(duration)
.attr("transform", function (d) {
return "translate(" + d.x + "," + d.y + ")";
});
nodeUpdate.select("circle")
.attr("r", 4.5)
.style("fill", function (d) {
return d._children ? "lightsteelblue" : "#fff";
});
nodeUpdate.select("text")
.style("fill-opacity", 1);
// Transition exiting nodes to the parent's new position.
var nodeExit = node.exit().transition()
.duration(duration)
.attr("transform", function (d) {
return "translate(" + source.x + "," + source.y + ")";
})
.remove();
nodeExit.select("circle")
.attr("r", 1e-6);
nodeExit.select("text")
.style("fill-opacity", 1e-6);
// Update the links…
var link = svg.selectAll("path.link")
.data(links, function (d) {
return d.target.id;
});
// Enter any new links at the parent's previous position.
link.enter().insert("path", "g")
.attr("class", "link")
.attr("d", function (d) {
var o = {
x: source.x0,
y: source.y0
};
return diagonal({
source: o,
target: o
});
});
// Transition links to their new position.
link.transition()
.duration(duration)
.attr("d", diagonal);
// Transition exiting nodes to the parent's new position.
link.exit().transition()
.duration(duration)
.attr("d", function (d) {
var o = {
x: source.x,
y: source.y
};
return diagonal({
source: o,
target: o
});
})
.remove();
// Stash the old positions for transition.
nodes.forEach(function (d) {
d.x0 = d.x;
d.y0 = d.y;
});
}
// Toggle children on click.
function click(d) {
if (d.children) {
d._children = d.children;
d.children = null;
} else {
d.children = d._children;
d._children = null;
}
update(d);
}