我想制作一个垂直的树状图,但是所有分支都彼此重叠,我不明白为什么。
这是基于耀斑数据集的树状图... 该代码旨在进入一个闪亮的应用程序,这就是为什么我使用r2d3。不知道这是否会带来很大的不同,因为.js无论如何都不会显示正确的树。
我现在使用的代码是:
// !preview r2d3 data = read.csv("flare.csv"), d3_version = 4
// Based on: https://bl.ocks.org/mbostock/4063570
var g = svg.append("g").attr("transform", "translate(300,10)");
var tree = d3.tree()
.size([0,200])
//.size([height, width - 160]);
.separation(function separation(a, b) {return (a.parent == b.parent ? 1 : 1) ;});
var stratify = d3.stratify()
.parentId(function(d) { return d.id.substring(0, d.id.lastIndexOf(".")); });
r2d3.onRender(function(data, svg, w, h, options) {
var root = stratify(data)
.sort(function(a, b) { return (a.height - b.height) || a.id.localeCompare(b.id); });
root = tree(root);
var y_spanning = 0.4;
var link = g.selectAll(".link")
.data(root.descendants().slice(1))
.enter().append("path")
.attr("class", "link")
.attr("d", function(d) {
return "M" + (d.x) + "," + (d.y) / y_spanning
//return "M" + d.y + "," + d.x
+ "C" + (d.parent.x ) + "," + (d.y ) / y_spanning
//+ "C" + (d.parent.y + 100) + "," + d.x
+ " " + (d.parent.x ) + "," + (d.parent.y ) / y_spanning
//+ " " + (d.parent.y + 100) + "," + d.parent.x
+ " " + (d.parent.x ) + "," + d.parent.y / y_spanning;
//+ " " + d.parent.y + "," + d.parent.x;
});
// link.append("text")
// .attr("x", function(d) {return d.x; })
// .attr("y", function(d) {return d.y; })
// .attr("font-size", 2 + 4 * height / 500)
// .attr("text-anchor", "middle")
// .text(function(d) { return d.id.substring(d.id.lastIndexOf(".") + 1);
// });
// var linkText = g.selectAll(".link")
// .append("text")
// .attr("class", "link-label")
// //.attr("dy", ".35em")
// .attr("text-anchor", "middle")
// .text(function(d) {
// return d.parentId;
// });
var node = g.selectAll(".node")
.data(root.descendants())
.enter().append("g")
.attr("class", function(d) { return "node" + (d.children ? " node-- internal" : " node--leaf"); })
.attr("transform", function(d) { return "translate(" + d.x + "," + d.y / y_spanning + ")"; })
node.append("circle")
.attr("r", 2.5);
node.append("text")
.attr("dx", 15)
.attr("dy", 4)
.attr("font-size", 2 + 4 * height / 500)
.attr("x", function(d) { return d.children ? -20 : 20; })
//.style("text-anchor", function(d) { return d.children ? "end" : "start"; })
.text(function(d) { return d.id.substring(d.id.lastIndexOf(".") + 1); });
});