Public
Edited
Mar 27, 2023
1 fork
Insert cell
network = FileAttachment("final_json.JSON").json()
Insert cell
{
// const nodes = network.nodes.map(d => ({ ...d })),
// links = network.links.map(l => ({
// source: l.source.id,
// target: l.target.id,
// value: l.value
// }));

const nodes = network.nodes,
links = network.links;

const svg = d3
.create("svg")
.attr("id", "graph")
.attr("viewBox", [0, 0, width, height]);

const lines = svg
.selectAll("line")
.data(links)
.join("line")
.style("stroke", "#333")
.style("stroke-opacity", l => {
console.log(l, opacity(l.value));
return opacity(l.value);
});

// const circles = svg
// .selectAll("circle")
// .data(nodes)
// .join("circle")
// .attr("r", 3);

const text = svg
.selectAll("text")
.data(nodes)
.join("text")
.attr("fill", "#333")
.style("font-size", d => size(d.value) + "pt")
.style("fill", d => color(d.cluster))
.text(d => d.id);

const ticked = () => {
lines
.attr("x1", l => l.source.x)
.attr("y1", l => l.source.y)
.attr("x2", l => l.target.x)
.attr("y2", l => l.target.y);
// circles.attr("cx", d => d.x).attr("cy", d => d.y);

text.attr("x", d => d.x).attr("y", d => d.y);
};

const simulation = d3
.forceSimulation(nodes)
.force("charge", d3.forceManyBody().strength(-20))
.force("center", d3.forceCenter(width / 2, height / 2))
// .force("x", d3.forceX(width / 2))
.force("link", d3.forceLink(links).id(d => d.id))
.force("boundary", forceBoundary(3, 3, width, height))
.on("tick", ticked);

text.call(drag(simulation));

invalidation.then(() => simulation.stop());

return svg.node();
}
Insert cell
drag = simulation => {
function dragstarted(event) {
if (!event.active) simulation.alphaTarget(0.3).restart();
event.subject.fx = event.subject.x;
event.subject.fy = event.subject.y;
}

function dragged(event) {
event.subject.fx = event.x;
event.subject.fy = event.y;
}

function dragended(event) {
if (!event.active) simulation.alphaTarget(0);
event.subject.fx = null;
event.subject.fy = null;
}

return d3
.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended);
}
Insert cell
height = 400
Insert cell
color = d3.scaleOrdinal(d3.schemeCategory10)
Insert cell
opacity = d3
.scaleLinear()
.domain(d3.extent(network.links, d => d.value))
.range([0.1, 1])
Insert cell
size = d3
.scaleLinear()
.domain(d3.extent(network.nodes, d => d.value))
.range([4, 18])
Insert cell
opacity.domain()
Insert cell
getKey = (a, b) => (a >= b ? `${a}~${b}` : `${b}~${a}`)
Insert cell
findOrAdd = (dNodes, n) => {
if (!dNodes.has(n)) dNodes.set(n, { id: n, value: 0 });
return dNodes.get(n);
}
Insert cell
dim = 5000
Insert cell
netClustering = require("netclustering")
Insert cell
forceBoundary = require("d3-force-boundary")
Insert cell
d3 = require("d3@6")
Insert cell
import { navio } from "@john-guerra/navio"
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