Public
Edited
Sep 2, 2022
1 fork
5 stars
Insert cell
Insert cell
Insert cell
Insert cell
chart = {
const simulation = d3.forceSimulation()
.force("charge", d3.forceManyBody())
.force("link", d3.forceLink().id(d => d.id))
.force("x", d3.forceX())
.force("y", d3.forceY())
.on("tick", ticked);

const svg = d3.create("svg")
.attr("viewBox", [-width / 2, -height / 2, width +30, height -10 ]); // parameters: min-x, min-y, width and height

let link = svg.append("g")
.attr("stroke", "#999")
.attr("stroke-opacity", 0.6)
.selectAll("line");

let node = svg.append("g")
.attr("stroke", "#fff")
.attr("stroke-width", 1.5)
.selectAll("circle");

let text = svg.append("g")
.selectAll("text");

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

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);

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

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

return Object.assign(svg.node(), {
update({nodes, links}) {

// Make a shallow copy to protect against mutation, while
// recycling old nodes to preserve position and velocity.
const old = new Map(node.data().map(d => [d.id, d]));
nodes = nodes.map(d => Object.assign(old.get(d.id) || {}, d));
links = links.map(d => Object.assign({}, d));

node = node
.data(nodes, d => d.id)
.join(enter => enter.append("circle")
.attr("r", 5)
.attr("fill", function(d) { return colorScale(d.bipartite)}) // with dharpa dataset: can use gender, yields 2 colors or name, yields 10 colors
.call(drag(simulation))
.call(node => node.append("title").text(d => d.id)));

text = text
.data(nodes)
.join("text") // "join" here is important, because otherwise the labels keep piling up
// .enter()
// .append('text')
.attr('class', 'graph-node-labels')
// .attr('label-id', d => d.label)
.style("text-anchor", "middle")
.style("pointer-events", "none")
.attr('font-family', 'sans-serif')
.attr('font-size', '5px')
.attr('dx', 0)
.attr('dy', +2)
.text((d,i) => {
return d.sides || d.year}) // "||" is a logical OR operator in JS. Options for label display: d.title || d.sides || d.year

link = link
.data(links, d => [d.source, d.target])
.join("line");

simulation.nodes(nodes);
simulation.force("link").links(links);
simulation.alpha(1).restart().tick();
ticked(); // render now!
}
});
}
Insert cell
update = {
const nodes = data.nodes.filter(d => contains(d, time));
const links = data.links.filter(d => contains(d, time));
chart.update({nodes, links});
}
Insert cell
contains = ({start_date, end_date}, time) => start_date <= time && time < end_date
Insert cell
Insert cell
Jsondata = FileAttachment("treatiesBi@4.json")
Insert cell
data = (await Jsondata).json()
Insert cell
md`# Functions & Definitions`
Insert cell
times = [...new Set(Array.from(data.links, d => d.start_date).sort((a, b) => a - b))] // [...new Set()] filters unique values; added .sort((a, b) => a - b) for sorting Array and putting 1689 in line.
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
colorScale = d3.scaleOrdinal(d3.schemeSet3)
Insert cell
height = 250
Insert cell
width = 400
Insert cell
md`# Libraries`
Insert cell
d3 = require("d3@6")
Insert cell
import { Scrubber } from "@mbostock/scrubber"
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