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

const svg = d3.create("svg");
setColor(root);
var myColor = d3.scaleOrdinal().domain(data)
.range(d3.schemeSet2);

svg.append("g")
.attr("fill", "none")
//.attr("stroke", function(d){return myColor(d) })
.attr("stroke-opacity", 0.4)
.attr("stroke-width", 1.5)
.selectAll("path")
.data(root.links())
.join("path")
.attr("d", d => `
M${d.target.y },${d.target.x}
C${d.source.y + root.dy / 2},${d.target.x}
${d.source.y + root.dy / 2},${d.source.x}
${d.source.y},${d.source.x}
`)
.each(function(d) { d.target.linkNode = this; })
.attr("stroke", d => d.target.color);

svg.append("g")
.selectAll("circle")
.data(root.descendants())
.join("circle")
.attr("cx", d => d.y)
.attr("cy", d => d.x)
//.attr("fill", "grey" )
.attr("fill", function(d){return myColor(d.depth) } )
//.attr("r", function(d) { return d.height * 2 + 5;});
.attr("r", "4")
// .each(function(d) { d.target.linkNode = this; })
// .attr("stroke", d => d.target.color)

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("x", d => d.y)
.attr("y", d => d.x)
.attr("dy", "0.31em")
.attr("dx", d => d.children ? -11 : 11)
.text(function(d) {return d.data.r_syn == null ? d.data.name : d.data.name+' | '+d.data.r_syn})
//.text(d => d.data.name)
//.attr("font-size", function(d) {
// return d.height * 2 + 10;
// })
.filter(d => d.children)
.attr("text-anchor", "end")
.clone(true).lower()
.attr("stroke", "white");


return svg.attr("viewBox", autoBox).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
function autoBox() {
document.body.appendChild(this);
const {x, y, width, height} = this.getBBox();
document.body.removeChild(this);
return [x, y, width, height];
}
Insert cell
data = FileAttachment("disciplinesCnacICiMab2.1.json").json()
Insert cell
// changement sur condition de sort
tree = data => {
const root = d3.hierarchy(data).sort((a, b) => a.data.name.toLowerCase().localeCompare (b.data.name.toLowerCase()));
root.dx = 9;
root.dy = width / (root.height + 3);
return d3.cluster().nodeSize([root.dx, root.dy])(root);
}
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