Public
Edited
Oct 26, 2023
1 fork
Insert cell
Insert cell
chart = {
const width = 1344;
const height = 840;
const color = d3.scaleOrdinal(d3.schemeCategory10);
const links = data.links.map(d => ({...d}));
const nodes = data.nodes.map(d => ({...d}));
const simulation = d3.forceSimulation(nodes)
.force("link", d3.forceLink(links).id(d => d.id))
.force("charge", d3.forceManyBody().strength(d => d ? -200 : -200 / d.value))
.force("center", d3.forceCenter(width / 2, height / 2))
.on("tick", ticked);
const svg = d3.create("svg")
.attr("width", width)
.attr("height", height)
.attr("viewBox", [0, 0, width, height])
.attr("style", "max-width: 100%;");
const colorScale = d3.scaleLinear()
.domain([0, d3.max(links, d => d.value)])
.range(["lightgray", "black"]);
const min_weight = 1;
const filteredLinks = links.filter(d => d.value >= min_weight);
const link = svg.append("g")
.attr("stroke", "#999")
.attr("stroke-opacity", 0.6)
.selectAll()
.data(filteredLinks)
.join("line")
.attr("x1", d => d.source.x)
.attr("y1", d => d.source.y)
.attr("x2", d => d.target.x)
.attr("y2", d => d.target.y)
.attr("stroke", "black")
.attr("stroke-width", 5);
const node = svg.append("g")
.selectAll()
.data(nodes)
.join("g")
.call(d3.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended));

node.append("circle")
.attr("r", d => Math.sqrt(10 * d.size))
.attr("fill", d => color(d.group));

node.append("title")
.text(d => d.id);

node.append("text")
.text(d => d.id)
.attr("x", 8)
.attr("y", -8);

// Add a drag behavior.
node.call(d3.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended));

// Set the position attributes of links and nodes each time the simulation ticks.
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);

node
.attr("transform", d => `translate(${d.x},${d.y})`);
}

// ...

// Reheat the simulation when drag starts, and fix the subject position.
function dragstarted(event) {
if (!event.active) simulation.alphaTarget(0.3).restart();

// Check if the dragged node is connected to any other nodes
const isConnected = links.some(link => {
return link.source === event.subject || link.target === event.subject;
});

if (isConnected) {
// If the node is connected, fix its position
event.subject.fx = event.subject.x;
event.subject.fy = event.subject.y;
} else {
// If the node is disconnected, disable the forces affecting other nodes
simulation.force("center", null); // Remove centering force
simulation.force("charge", null); // Remove charge force
}
}

// Update the subject (dragged node) position during drag.
function dragged(event) {
event.subject.fx = event.x;
event.subject.fy = event.y;
}

// Restore the target alpha so the simulation cools after dragging ends.
// Unfix the subject position now that it’s no longer being dragged.
function dragended(event) {
if (!event.active) simulation.alphaTarget(0);

// Check if the dragged node is connected to any other nodes
const isConnected = links.some(link => {
return link.source === event.subject || link.target === event.subject;
});

if (isConnected) {
// If the node is connected, release it
event.subject.fx = null;
event.subject.fy = null;
} else {
// If the node is disconnected, re-add the forces that were removed
simulation.force("center", d3.forceCenter(width / 2, height / 2)); // Restore centering force
simulation.force("charge", d3.forceManyBody().strength(d => d ? -200 : -200 / d.value)); // Restore charge force
}
}

// ...


// When this cell is re-run, stop the previous simulation. (This doesn’t
// really matter since the target alpha is zero and the simulation will
// stop naturally, but it’s a good practice.)
invalidation.then(() => simulation.stop());

return svg.node();
}
Insert cell
data = FileAttachment("similarity@9.json").json()
Insert cell
Select a data source…
Type Table, then Shift-Enter. Ctrl-space for more options.

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