I have created the code below based on Mike Bostock's example in Observable (which I know is not the same as raw d3/ javascript) https://observablehq.com/@d3/radial-dendrogram
However, it:
- only displays if I remove the return svg.attr("viewBox", autoBox).node(); at the end of the chart function. I suspect that is causing the issues below
- is tightly cropped
- looks like the centre is at coordinate 0,0 in the top left of the screen, so 3 quarters of the viz is off the screen. I have tried adding a transform translate but that doesn't help
Any ideas warmly welcomed...
index.html
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="//fonts.googleapis.com/css?family=Open+Sans" />
<script src="https://d3js.org/d3.v6.js"></script>
<link rel="shortcut icon" href="#">
<title>Radial Dendrogram</title>
</head>
<body>
<div id="vis">
</div>
<script src = 'script.js'></script>
</body>
</html>
script.js
function chart(birdDataSet) {
const root = tree(d3.hierarchy(birdDataSet)
.sort((a, b) => d3.ascending(a.data.name, b.data.name)));
svg.append("g")
.attr("fill", "none")
.attr("stroke", "#555")
.attr("stroke-opacity", 0.4)
.attr("stroke-width", 1.5)
.selectAll("path")
.data(root.links())
.join("path")
.attr("d", d3.linkRadial()
.angle(d => d.x)
.radius(d => d.y));
svg.append("g")
.selectAll("circle")
.data(root.descendants())
.join("circle")
.attr("transform", d => `
rotate(${d.x * 180 / Math.PI - 90})
translate(${d.y},0)
`)
.attr("fill", d => d.children ? "#555" : "#999")
.attr("r", 2.5);
svg.append("g")
.attr("font-family", "sans-serif")
.attr("font-size", 10)
.attr("stroke-linejoin", "round")
.attr("stroke-width", 3)
.selectAll("text")
.data(root.descendants())
.join("text")
.attr("transform", d => `
rotate(${d.x * 180 / Math.PI - 90})
translate(${d.y},0)
rotate(${d.x >= Math.PI ? 180 : 0})
`)
.attr("dy", "0.31em")
.attr("x", d => d.x < Math.PI === !d.children ? 6 : -6)
.attr("text-anchor", d => d.x < Math.PI === !d.children ? "start" : "end")
.text(d => d.data.name)
.clone(true).lower()
.attr("stroke", "white");
//return svg.attr("viewBox", autoBox).node();
}
function autoBox() {
document.body.appendChild(this);
const {x, y, width, height} = this.getBBox();
document.body.removeChild(this);
return [x, y, width, height];
}
width = 975
radius = width / 2
tree = d3.cluster().size([2 * Math.PI, radius - 100])
d3.json("data/flare-2.json")
.then(function(data) {
console.log(chart(data));
})
.catch(function(error) {
console.warn(error);
});
flare-2.json
from D3 Radial Dendrogram only displaying part
No comments:
Post a Comment