Published
Edited
Sep 10, 2020
1 fork
3 stars
Insert cell
md`# Tree`
Insert cell
chart = {
const nodes = root.descendants();
addColors();
const svg = d3.create("svg")
.attr("viewBox", [-nodeSize / 2, -nodeSize * 3 / 2, width, (nodes.length + 1) * nodeSize])
.style("background-color", "#132031")
.style("overflow", "visible");
// ребра дерева
const link = svg.append("g")
.attr("fill", "none")
.attr("stroke", "#374d5e")
.selectAll("path")
.data(root.links())
.join("path")
.attr("d", d => `
M${d.source.depth * nodeSize},${d.source.index * nodeSize}
V${d.target.index * nodeSize}
h${nodeSize}
`);
const node = svg.append("g")
.selectAll("g")
.data(nodes)
.join("g")
.attr("transform", d => `translate(0,${d.index * nodeSize})`);
node.append("circle")
.attr("cx", d => d.depth * nodeSize)
.attr("r", radius + 1)
.attr("fill", d => d.data.color);
node.append("circle")
.attr("fill",(d, i) => `url(#avatar_${i})`)
.attr("cx", d => d.depth * nodeSize)
.attr("r", radius);
node.append("svg:defs").append("svg:pattern")
.attr("id", (d, i) => `avatar_${i}`)
.attr("width", radius * 2)
.attr("height", radius * 2)
.attr("patternUnits", "objectBoundingBox")
.append("svg:image")
.attr("xlink:href", d => d.data.url)
.attr("width", radius * 2)
.attr("height", radius * 2)
.attr("preserveAspectRatio", "xMidYMid slice")
.attr("x", 0)
.attr("y", 0);
node.append("text")
.attr("dy", "0")
.attr("y", radius / 2 - 1)
.attr("x", d => d.depth * nodeSize + radius + 5)
.attr("font-weight", 600)
.attr("fill", "#ddd")
.attr("font-family", "sans-serif")
.attr("font-size", 4)
.text(d => d.data.name);
return svg.node();
}
Insert cell
radius = 6
Insert cell
nodeSize = 17
Insert cell
height = 300
Insert cell
width = 300
Insert cell
dataColors = ["#f96b6b", "#dd934d", "#878787", "#00b050", "#00b0f0", "#8d42c6", "#0059c4", "#d31e79"];
Insert cell
function addColors() {
data.color = "#fff";
let i = 0;
for (let child of data.children) {
child.color = dataColors[i];
for (let subChild of child.children) {
subChild.color = dataColors[i];
}
i++;
}
}
Insert cell
root = { let i = 0; return d3.hierarchy(data).eachBefore(d => d.index = i++); }
Insert cell
data = FileAttachment("data-5-2-2-1@1.json").json()
Insert cell
d3 = require("d3@5")
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more