Public
Edited
Jun 21, 2021
1 fork
9 stars
Insert cell
md`# Force Layout Clusters`
Insert cell
md`Cluster code from this notebook: https://observablehq.com/@pjsier/force-layout-cells`
Insert cell
Insert cell
{
const svgBackgroundColor = "#152c33",
svg = d3.create("svg")
.attr("width", width)
.attr("height", height)
.style("background-color", svgBackgroundColor),
line = d3.line().curve(d3.curveBasisClosed),
nodes = graph.nodes,
links = graph.links,
clusters = graph.clusters;

let clusterMap = {};
nodes.forEach(n => {
if(!clusterMap[n.cluster] || (n.radius > clusterMap[n.cluster].radius)){
clusterMap[n.cluster] = n;
}
})
const container = svg.append("g");

const hullG = svg.append('g')
.attr('class', 'hulls');

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))
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")
let hullPoints = function (data) {
let pointArr = [];
const padding = radius;
data.each(d => {
const pad = d.radius + padding;
pointArr = pointArr.concat([
[d.x - pad, d.y - pad],
[d.x - pad, d.y + pad],
[d.x + pad, d.y - pad],
[d.x + pad, d.y + pad]
]);
});
return pointArr;
}
const hulls = hullG
.selectAll('path')
.data(Object.keys(clusterMap).map(c => {
return {
cluster: c,
nodes: node.filter(d => d.cluster == c)
};
}), d => d.cluster)
.join('path')
.attr('d', d => line(d3.polygonHull(hullPoints(d.nodes))))
.attr('fill', d => clusterColors[clusters.indexOf(parseInt(d.cluster, 10))])
.attr('opacity', 0.4);
simulation
.on("tick", ticked);
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)));

hulls
.attr('d', d => line(d3.polygonHull(hullPoints(d.nodes))));
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