Public
Edited
Nov 27, 2022
1 star
Insert cell
Insert cell
Insert cell
data = d3.csv(
"https://raw.githubusercontent.com/bumbeishvili/sample-data/main/data-oracle.csv"
)
Insert cell
Insert cell
createHierarchicalData = d3.stratify()
Insert cell
hierarchicalData = createHierarchicalData(data)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
layoutFunc = d3.tree()
Insert cell
hierarchicalDataWithPositions = layoutFunc(hierarchicalData.copy())
Insert cell
descendants = hierarchicalDataWithPositions.descendants()
Insert cell
Insert cell
updatedCoords = descendants.map((d) => {
Object.assign(d, {
_newX: d.x * width,
_newY: d.y * height
});
return d;
})
Insert cell
Insert cell
{
const svg = d3
.create("svg")
.attr("width", width)
.attr("height", height)
.style("overflow", "visible");

const g = svg.append("g");

g.selectAll("path.links")
.data(updatedCoords.filter((d) => d.parent))
.join("path")
.attr("d", (d) => link(d))
.attr("stroke", "black");

const nodes = g
.selectAll("g.node")
.data(updatedCoords)
.join("g")
.attr("transform", (d) => `translate(${d._newX},${d._newY})`);

nodes.append("circle").attr("r", 3);
nodes
.append("text")
.text((d) => d.data.name)
.attr("font-size", 10)
.attr("transform", "translate(3.5,-5) rotate(-90)")
.attr("stroke", "white")
.attr("stroke-width", 5)
.clone(true)
.attr("stroke", "none");

return svg.node();
}
Insert cell
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