Published
Edited
May 7, 2020
Insert cell
chart = {
const min_node_popularity = 3
const min_link_value = 3
const nodes = data.items.nodes.filter(node => node.popularity > min_node_popularity ).map(d => Object.create(d));
const links = data.items.links.filter(link => link.value > min_link_value ).map(d => Object.create(d));


const simulation = d3.forceSimulation(nodes)
.force("link", d3.forceLink(links).id(d => d.id))
.force("charge", d3.forceManyBody().strength(d => -50*Math.sqrt(d.popularity)))
.force("center", d3.forceCenter(width / 2, height / 2));

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

const link = svg.append("g")
.attr("stroke", "#999")
.selectAll("line")
.data(links)
.join("line")
.attr("stroke-opacity", d => 0.2)
.attr("stroke-width", d => d.value*0.1);

const node = svg.append("g")
.attr("class", "nodes")
.attr("stroke", "#fff")
.attr("stroke-width", 0.1)
.selectAll("circle")
.data(nodes)
.join("circle")
.attr("r", d => Math.sqrt(d.popularity)*2)
.attr("fill", color)
.call(drag(simulation));
node.append("title")
.text(d => d.id+": "+d.popularity+" mentions");
const text = svg.selectAll("nodes")
.data(nodes)
.join("text")
.attr("x", (d, i) => i * 15)
.attr("y", 17)
.attr("dy", "0.2em")
.attr("font-family", "Verdana")
.attr("font-size", d => Math.pow(d.popularity,1/6)*5)
.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);
text
.attr("x", d => d.x) //position of the lower left point of the text
.attr("y", d => d.y); //position of the lower left point of the text
});

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

return svg.node();
}
Insert cell
data = fetch("https://raw.githubusercontent.com/avk0/skills_graph/master/data/all/proc-tags_Python.json")
.then(res => res.json())
Insert cell
height = 900
Insert cell
color = {
const scale = d3.scaleOrdinal([4, 3, 2, 1, 0],[`orange`, `lightgreen`, `cyan`, `khaki`, `lightgrey`]);
// d3.schemeSet2
return (d) => scale(parseInt(Math.sqrt(d.popularity)/Math.sqrt(1000)*5));
}
Insert cell
drag = simulation => {
function dragstarted(d) {
if (!d3.event.active) simulation.alphaTarget(0.3).restart();
d.fx = d.x;
d.fy = d.y;
}
function dragged(d) {
d.fx = d3.event.x;
d.fy = d3.event.y;
}
function dragended(d) {
if (!d3.event.active) simulation.alphaTarget(0);
d.fx = null;
d.fy = null;
}
return d3.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended);
}
Insert cell
d3 = require("d3@5")
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