问题
I want to make a vertical dendogram, however all branches are on top of each other, and I don't understand why.
enter image description here
this is a dendogram based on the flare dataset... The code is meant to go into a shiny app, and that is why i use r2d3. Not sure if this makes a huge difference as the .js doesn't show the proper tree anyway.
the code I use right now is:
// !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); });
});
回答1:
You have just swap the 'x' and 'y' coords and tweak the curve between.
A screenshot of the result:
I did not manage to get a running snippet while loading the original CSV used in the bl.ocks.org sample
No 'Access-Control-Allow-Origin' header is present on the requested resource.
Just run the example from own local web server.
<!DOCTYPE html>
<!-- horizontal version https://bl.ocks.org/mbostock/4063570 -->
<meta charset="utf-8">
<style>
.node circle {
fill: #999;
}
.node text {
font: 10px sans-serif;
}
.node--internal circle {
fill: #555;
}
.node--internal text {
text-shadow: 0 1px 0 #fff, 0 -1px 0 #fff, 1px 0 0 #fff, -1px 0 0 #fff;
}
.link {
fill: none;
stroke: #555;
stroke-opacity: 0.4;
stroke-width: 1.5px;
}
</style>
<svg width="2000" height="960"></svg>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>
var svg = d3.select("svg"),
width = +svg.attr("width"),
height = +svg.attr("height"),
g = svg.append("g").attr("transform", "translate(40,40)");
var tree = d3.cluster()
.size([width-40, height -160]);
var stratify = d3.stratify()
.parentId(function(d) { return d.id.substring(0, d.id.lastIndexOf(".")); });
d3.csv("flare.csv", function(error, data) {
if (error) throw error;
var root = stratify(data)
.sort(function(a, b) { return (a.height - b.height) || a.id.localeCompare(b.id); });
tree(root);
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
+ "C" + d.x + "," + (d.parent.y + 100)
+ " " + d.parent.x + "," + (d.parent.y + 100)
+ " " + d.parent.x + "," + d.parent.y;
});
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 + ")"; });
node.append("circle")
.attr("r", 2.5);
node.append("text")
.attr("dy", 3)
.attr("x", function(d) { return d.children ? -8 : 8; })
.style("text-anchor", function(d) { return d.children ? "end" : "start"; })
.attr("transform", function(d) { return "rotate(" + (d.children ? "45" : "90") + ")"; })
.text(function(d) { return d.id.substring(d.id.lastIndexOf(".") + 1); });
});
</script>
回答2:
I actually started from a horizontal to a vertical tree, by switching the x and y.
I found the problem: It was about sorting the data properly. parents and childs should be in the correct order, otherwise it doesn't work properly.
来源:https://stackoverflow.com/questions/51632127/d3-all-branches-on-top-of-each-other-vertical-dendogram-r2d3-shiny