Published
Edited
Jun 11, 2020
3 forks
2 stars
Insert cell
md`# Force directed graph
This graph is generated using d3 spring layout package and customized for first 50 nodes from the facebook snap dataset provided in the assignement. The node weights are the # of links of the node and the size varies by the weight. The color of each node is also assigned my the weight of the nodes using d3 scaleOrdinal.
`
Insert cell
chart = {
const links = l.map(d => Object.create(d));
const nodes = n.nodes.map(d => Object.create(d));
const nn = nodes.map(n => {
const red = d3.randomInt(1, 256)();
const green = d3.randomInt(1, 256)();
const blue = d3.randomInt(1, 256)();
const alpha = Math.random();
return {
color: d3.rgb(red, green, blue, alpha)
};
});
var div = d3.select("body").append("div")
.attr("class", "tooltip")
.style("opacity", 0);
const gethtml = (id, links) => {
return `<div>node: ${id}</div> <div>links: ${links}</div>`;
}
const simulation = d3.forceSimulation(nodes)
.force("link", d3.forceLink(links).id(d => d.id))
.force("charge", d3.forceManyBody())
.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")
.attr("stroke-opacity", 0.6)
.selectAll("line")
.data(links)
.join("line")
.attr("stroke-width", d => Math.sqrt(d.value));

const node = svg.append("g")
.attr("stroke", "#fff")
.attr("stroke-width", 1.5)
.selectAll("circle")
.data(nodes)
.join("circle")
.attr("r", d => Math.sqrt(d.weight)+1)
.attr("fill", d => color(d))
.on('mouseover', function (d, i) {
d3.select(this).transition()
.duration('50')
.attr('opacity', '.85');
div.transition()
.duration(50)
.style("opacity", 1);
let num = 50;
div.html(gethtml(d.id,d.weight))
.style("left", (d3.event.pageX + 10) + "px")
.style("top", (d3.event.pageY - 15) + "px");
})
.on('mouseout', function (d, i) {
d3.select(this).transition()
.duration('50')
.attr('opacity', '1');
div.transition()
.duration('50')
.style("opacity", 0);
})
.call(drag(simulation));

// 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
l=FileAttachment("links@3.json").json()
Insert cell
n=FileAttachment("network@6.json").json()
Insert cell
style = html`<style>

div.tooltip {
position: absolute;
text-align: center;
padding: .5rem;
background: #FFFFFF;
color: #313639;
border: 1px solid #313639;
border-radius: 8px;
pointer-events: none;
font-size: 1.3rem;
}

</style>`
Insert cell
height=600
Insert cell
color = {
const scale = d3.scaleOrdinal(d3.schemeCategory10);
return d => scale(Math.sqrt(d.weight));
}
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", 'd3-random')
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