Published
Edited
Oct 4, 2022
Insert cell
Insert cell
chart = {
const root = d3.hierarchy(data);
const links = root.links();
const nodes = root.descendants();
const cArray=["#F55D3E", "#76BED0", "#F7CB15"]
const simulation = d3.forceSimulation(nodes)
.force('center', d3.forceCenter(-30,0))
.force("link", d3.forceLink(links).id(d => d.id).distance(d=> d.source.height*70).strength(0.6))
.force("charge", d3.forceManyBody().strength(d=> -400*(d.height+1)))
.force("x", d3.forceX())
.force("y", d3.forceY(200))
// .force("charge", 10000000)
// .force("fixed",d => {console.log (d); return d.height==3 ? true: false});

const svg = d3.create("svg")
.attr("viewBox", [-width / 2, -height / 2, width, height])
.style("font", "16px sans-serif")
// .style("background-color", "rgb(25, 25, 25)");
.style("background-color", "#fff");

const link = svg.append("g")
.attr("stroke", "#878E88")
.attr("stroke-opacity", 0.6)
.selectAll("line")
.data(links)
.join("line")
// .selectAll("g")
// .data(nodes)
// .join("g")
.attr("stroke-width", d =>d.source.height*d.source.height);

const node = svg.append("g")
.attr("fill", "currentColor")
.attr("stroke-linecap", "round")
.attr("stroke-linejoin", "round")
.selectAll("g")
.data(nodes)
.join("g")
.call(drag(simulation));
node.append("circle")
.attr("fill", d => cArray[d.height])
.attr("r", d=> (d.height+1)*5)
.attr("stroke", "grey")
.attr("stroke-width", 1)

node.append("text")
.attr("text-align","center")
.attr("x", 15)
// .attr("y", ".41em")
.attr("fill", d=>"#444")
.text(d => d.data.name)
// .text(d => d.children)
.clone(true).lower()
.attr("fill", "none")
.attr("stroke", "white")
.attr("stroke-width", 3);

simulation.on("tick", () => {
// nodes[0].x = -250;
nodes[0].vx= -(nodes[0].x+250);
// d3.timer(printTime1);
nodes[0].vy = -(nodes[0].y);
link
.attr("x1", d => d.source.x)
.attr("y1", d => d.source.y)
.attr("x2", d => d.target.x)
.attr("y2", d => d.target.y);

node
.attr("transform", d => `translate(${d.x},${d.y})`)
.attr("cx", d => d.x)
.attr("cy", d => d.y);
});
// function printTime1(elapsed){
// // console.log(nodes[0].x)
// nodes[0].x=-250+20*Math.sin(elapsed/1000);
// nodes[0].y= 20*Math.cos(elapsed/1000)
// // d3.hierarchy(data).descendants()
// // d3.select("#demo1").text(Math.round(elapsed) + "ms")
// }
invalidation.then(() => simulation.stop());

return svg.node();
}
Insert cell
data = FileAttachment("TreeOfThesis@6.json").json()
Insert cell
height = 600
Insert cell
drag = simulation => {
function dragstarted(event, d) {
if (!event.active) simulation.alphaTarget(0.3).restart();
d.fx = d.x;
d.fy = d.y;
}
function dragged(event, d) {
d.fx = event.x;
d.fy = event.y;
}
function dragended(event, d) {
if (!event.active) simulation.alphaTarget(0);
d.fx = null;
d.fy = null;
}
return d3.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended);
}
Insert cell
d3 = require("d3@6")
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