Published
Edited
Apr 9, 2020
2 forks
3 stars
Insert cell
Insert cell
chart = {
const root = tree(data);

setColor(root);
var myColor = d3.scaleOrdinal().domain(data)
.range(d3.schemeSet2);
let x0 = Infinity;
let x1 = -x0;
root.each(d => {
if (d.x > x1) x1 = d.x;
if (d.x < x0) x0 = d.x;
});

//fix viewBox à l'arrache
const svg = d3.create("svg")
.attr("viewBox", [-50, 0, width +150, x1 - x0 + root.dx * 2]);
const g = svg.append("g")
.attr("font-family", "sans-serif")
.attr("font-size", 10)
.attr("transform", `translate(${root.dy / 3},${root.dx - x0})`);
const link = g.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.linkHorizontal()
.x(d => d.y)
.y(d => d.x))
.each(function(d) { d.target.linkNode = this; })
.attr("stroke", d => d.target.color);;
const node = g.append("g")
.attr("stroke-linejoin", "round")
.attr("stroke-width", 3)
.selectAll("g")
.data(root.descendants())
.join("g")
.attr("transform", d => `translate(${d.y},${d.x})`);

node.append("circle")
.attr("fill", function(d){return myColor(d.depth) } )
.attr("r", 4);

node.append("text")
.attr("dy", "0.31em")
.attr("x", d => d.children ? -6 : 6)
.attr("text-anchor", d => d.children ? "end" : "start")
.text(function(d) {return d.data.r_syn == null ? d.data.name : d.data.name+' | '+d.data.r_syn})
.clone(true).lower()
.attr("stroke", "white");
return svg.node();
}
Insert cell
color = d3.scaleOrdinal()
.domain(["Acrobatie", "Jonglerie et magie","Clowns","Dressage"])
.range(d3.schemeCategory10)


Insert cell
// Set the color of each node by recursively inheriting.
function setColor(d) {
var name = d.data.name;
d.color = color.domain().indexOf(name) >= 0 ? color(name) : d.parent ? d.parent.color : null;
if (d.children) d.children.forEach(setColor);
}
Insert cell
data = FileAttachment("disciplinesCnacICiMab2.1.json").json()
Insert cell
tree = data => {
const root = d3.hierarchy(data);
root.dx = 10;
root.dy = width / (root.height + 1);
return d3.tree().nodeSize([root.dx, root.dy])(root);
}
Insert cell
width = 994
Insert cell
d3 = require("d3@5")
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more