Public
Edited
Aug 3, 2023
Insert cell
Insert cell
chart = {
// Specify the dimensions of the chart.
const width = 800;
const height = width;
const labelDelta = 5;

// Specify the color scale.
const color = d3.scaleOrdinal(d3.schemeCategory10);

// The force simulation mutates links and nodes, so create a copy
// so that re-evaluating this cell produces the same result.
const links = data.links.map(d => ({...d}));
const nodes = data.nodes.map(d => ({...d}));

const distances = links.map(l => l.distance);
const distDomain = [
d3.min(distances),
d3.max(distances)
];
const strokeMap = d3.scaleLinear()
.domain(distDomain)
.range([10, 0.2]);
const distanceMap = d3.scaleLinear()
.domain(distDomain)
// .range([0, 1])
// Create a simulation with several forces.
const simulation = d3.forceSimulation(nodes)
// .force("link", d3.forceLink(links).id(d => d.id).strength(0.5).distance(d => ((d.distance * 5) ** 4) * 2.5))
.force("link", d3.forceLink(links).id(d => d.id).distance(d => (distanceMap(d.distance) + 1) * width / 4))
// .force("link", d3.forceLink(links).id(d => d.id).strength(d => 10/d.distance))
// .force("link", d3.forceLink(links).id(d => d.id).strength(d => .5/d.distance))
.force("charge", d3.forceManyBody().strength(-30))
.force("center", d3.forceCenter(width / 2, height / 2))
.on("tick", ticked);

simulation.alphaDecay(0.001); // Set alphaDecay to 0 for infinite movement

const opacityLow = 0.2;
const opacityHigh = 1;

// Create the SVG container.
const svg = d3.create("svg")
.attr("width", width)
.attr("height", height)
.attr("viewBox", [0, 0, width, height])
.attr("style", "max-width: 100%; height: auto;");

// Add a line for each link, and a circle for each node.
const link = svg.append("g")
.attr("stroke", "#f00")
.selectAll()
// Uncomment this block if you want to see the connections
.data(links)
.join("line")
.attr("stroke-opacity", () => opacityLow)
.attr("stroke-width", d => strokeMap(d.distance) ** 1 / 2);

const node = svg.append("g")
.attr("stroke", "#fff")
.attr("stroke-width", 1.5)
.selectAll()
.data(nodes)
.join("circle")
.attr("r", 5)
.attr("fill", d => color(d.group))
// .join("text")
// .text(d => d.id)
// ;

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

// Add a layer of labels.
const label = svg.append("g")
.attr("font-family", "sans-serif")
.attr("font-size", 16)
.selectAll("text")
.data(nodes)
.join("text")
.attr("dy", "0.35em")
// .attr("x", d => x(d.mpg) + 7)
// .attr("y", d => y(d.hp))
.text(d => d.id);
// 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)
.attr('opacity', d => dragSubject === d.target || dragSubject === d.source ? opacityHigh : opacityLow);

node
.attr("cx", d => d.x)
.attr("cy", d => d.y);

label
.attr("x", d => d.x + labelDelta)
.attr("y", d => d.y - labelDelta);

//console.log("Simulation alpha:", simulation.alpha());
}

let dragSubject = null;
// Reheat the simulation when drag starts, and fix the subject position.
function dragstarted(event) {
if (!event.active) simulation.alphaTarget(0.3).restart();
event.subject.fx = event.subject.x;
event.subject.fy = event.subject.y;
dragSubject = event.subject;
}

// 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);
event.subject.fx = null;
event.subject.fy = null;
dragSubject = null;
}

// 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("output@25.json").json()
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