Published
Edited
Dec 1, 2020
1 star
Insert cell
Insert cell
Insert cell
Insert cell
chart = {
data.nodes.forEach(d => {
d.import_count = data.links.filter(l => l.__proto__.target == d.id).length;
});
const simulation = d3.forceSimulation(data.nodes)
.force("link", d3.forceLink(data.links).id(d => d.id).distance(50).strength(2))
.force("charge", d3.forceManyBody().strength(d => -10-d.import_count*10))
//.force("cluster", forceCluster(data.nodes))
.force("center", d3.forceCenter(width / 2, height / 2));

const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height])
.attr("xlink", "http://www.w3.org/1999/xlink");
const s = 10;
svg
.append('defs')
.append('marker')
.attr('id', 'arrow')
.attr('viewBox', [0, 0, 20, 20])
.attr('refX', 20)
.attr('refY', 10)
.attr('markerWidth', 10)
.attr('markerHeight', 10)
.attr('orient', 'auto-start-reverse')
.append('path')
.attr('d', d3.line()([[0, 0], [0, 20], [20, 10]]))
.attr('fill', 'currentColor')
.attr('stroke', 'none');
const big_g = svg.append("g");

const link = big_g.append("g")
.attr("stroke", "#999")
.attr("fill", "#999")
.attr("stroke-opacity", 0.6)
.attr('marker-end', 'url(#arrow)')
.selectAll("line")
.data(data.links)
.join("line");

const g = big_g.append("g")
.attr("stroke", "#fff")
.attr("stroke-width", 1.5)
const node = g
.selectAll("a")
.data(data.nodes)
.join("a")
.attr("xlink:href", d => `https://pygae.github.io/lean-ga-docs/${d.path.join('/')}.html`)
.attr("target", "_blank")
.append('circle')
.attr("r", d => Math.sqrt((d.import_count + 1) * 16))
.attr("fill", color)
.call(drag(simulation));
d3.zoom().on("zoom", (event) => big_g.attr("transform", event.transform))(svg);
node.append("title")
.text(d => d.id);

simulation.on("tick", () => {
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("cx", d => d.x)
.attr("cy", d => d.y);
});

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

return svg.node();
}
Insert cell
raw_data = FileAttachment("import_graph.json").json()
Insert cell
data = {
const mk_id = (x) => x[0] + ':' + x[1].join('.');
const links = raw_data.links.map(({source, target}) => Object.create({source: mk_id(source), target: mk_id(target)}));
const nodes = raw_data.nodes.map(d => Object.create({id: mk_id(d.id), project: d.id[0], path: d.id[1]}));
return {
links: links.filter(({source, target}) => source != 'core:init.default'&&target != 'core:init.default'),
nodes: nodes.filter((d) => d.id != 'core:init.default'),
}
}
Insert cell
height = 600
Insert cell
main_groups = [...new Set(data.nodes.map(n => n.path[0]))].sort()
Insert cell
main_group = (d) => main_groups.indexOf(d.path[0])
Insert cell
color_map = (x) => d3.interpolateRainbow(x / main_groups.length);
Insert cell
color = {
return d => {
return color_map(main_group(d));
}
}
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
function centroid(nodes) {
let x = 0;
let y = 0;
let z = 0;
for (const d of nodes) {
let k = d.r ** 2;
x += d.x * k;
y += d.y * k;
z += k;
}
return {x: x / z, y: y / z};
}
Insert cell
function forceCluster(nodes) {
const strength = 0.2;

function force(alpha) {
const centroids = d3.rollup(nodes, centroid, d => main_group(d));
console.log(centroids);
const l = alpha * strength;
for (const d of nodes) {
const {x: cx, y: cy} = centroids.get(main_group(d));
d.vx -= (d.x - cx) * l;
d.vy -= (d.y - cy) * l;
}
}

return force;
}
Insert cell
d3 = require("d3@6")
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