Published
Edited
Jun 21, 2021
2 stars
Insert cell
md`# Force Layout Demo`
Insert cell
viewof updateButton = html`<button id="showNumbers">Show Labels</button>`
Insert cell
{
const svgBackgroundColor = "#152c33",
svg = d3.create("svg")
.attr("width", width)
.attr("height", height)
.style("background-color", svgBackgroundColor),
nodes = graph.nodes,
links = graph.links,
clusters = graph.clusters;
const container = svg.append("g");

const simulation = d3.forceSimulation(nodes)

.force("link", d3.forceLink(links).id(d => d.id))
.force("charge", d3.forceManyBody().strength(20))
.force('collide', d3.forceCollide(d => 22))
.force('center', d3.forceCenter()
.x(width / 2)
.y(height / 2))
.on("tick", ticked);
const link = container.append("g")
.attr("fill", "none")
.attr("stroke-width", 1.5)
.selectAll("line")
.data(links)
.join("line")
.attr("stroke", d => clusterColors[clusters.indexOf(parseInt(d.cluster, 10))]);


const node = container.append("g")
.attr("fill", "gray")
.selectAll("g")
.data(nodes)
.join("g");
const circles = node.append("circle")
.attr("stroke", d => clusterColors[clusters.indexOf(parseInt(d.cluster, 10))])
.attr("stroke-width", 1.5)
.attr("r", d => d.radius)
.attr("radius", d => d.radius)
.attr('fill', d => clusterColors[clusters.indexOf(parseInt(d.cluster, 10))])
const text = node.append("text")
.text(d => d.name)
.attr("fill", svgBackgroundColor)
.style("opacity", 0)
.attr("text-anchor", "middle")
.attr("dy", 5)
.attr("font-size", d => isNaN(d.name) == true ? "1.1em" : "0.8em")
.attr("font-family", "helvetica")
.attr("font-weight", "bold")
function ticked() {
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);

circles
.attr("cx", d => Math.max(d.radius, Math.min(width - d.radius, d.x)))
.attr("cy", d => Math.max(d.radius, Math.min(height - d.radius, d.y)));

text
.attr("x", d => Math.max(d.radius, Math.min(width - d.radius, d.x)))
.attr("y", d => Math.max(d.radius, Math.min(height - d.radius, d.y)));
}
function hideNumbers(){
text.style("opacity", 0);
let button = d3.select("button#hideNumbers");
button.attr("id", "showNumbers");
button.on("click", showNumbers);
button.html("Show Labels");
}

function showNumbers(){
text.style("opacity", 1);
let button = d3.select("button#showNumbers");
button.attr("id", "hideNumbers");
button.on("click", hideNumbers);
button.html("Hide Labels");
}


d3.select("button#showNumbers").on("click", showNumbers);
yield svg.node();
}
Insert cell
graph = {
const nodes = [],
links = [],
letters = "ABCDEFG";

let clusters = new Set(),
linkId = 0,
cc = 0,
count = 0;
/* creating the nodes and links data */
for(let char in letters){
const letterName = letters[char];
var nodeLetter = {
id: cc,
name: letterName,
radius: radius,
x: Math.random() * width,
y: Math.random() * height,
cluster: cc,
radius: radius * 1.5
}
clusters.add(cc);
nodes.push(nodeLetter);
cc++;
let numChildren = Math.floor(Math.random() * 6) + 2;
for(let i=1; i < numChildren; i++){
count++
var nodeNumber = {
id: cc,
name: count,
radius: radius,
x: Math.random() * width,
y: Math.random() * height,
cluster: nodeLetter['id'],
radius: radius
};
nodes.push(nodeNumber)
let link = {'source': nodeLetter['id'], 'target': cc, 'id': linkId, 'cluster': nodeLetter['id']}
links.push(link);
linkId++
cc++;
}
}
return {'nodes': nodes, 'links': links, 'clusters': Array.from(clusters)};
}
Insert cell
radius = 8
Insert cell
count = 0
Insert cell
cc = 0
Insert cell
width = 500
Insert cell
height = 500
Insert cell
padding = 5
Insert cell
clusterColors = ["#f72585","#b5179e","#7209b7","#560bad","#480ca8","#3a0ca3","#3f37c9","#4361ee","#4895ef","#4cc9f0"]
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