Published
Edited
Jan 21, 2019
1 star
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
chart = {
let transform = d3.zoomIdentity;
const links = data.links.filter(l => l.fitness > minFitness).map(d => Object.create(d));
const nodes = data.nodes.map(d => Object.create(d));

const simulation = d3.forceSimulation(nodes)
.force("link", d3.forceLink(links).id(d => d.id)
.distance((d) => 30 + 30 * (1 - d.fitness))
.strength(0.1)
)
.force("charge", d3.forceManyBody()
.strength(-100)
)
.force("x", d3.forceX())
.force("y", d3.forceY());

const svg = d3.select(DOM.svg(width, height))
const link = svg.append("g")
.attr("stroke", "#999")
.attr("stroke-opacity", 0.3)
.selectAll("line")
.data(links)
.enter().append("line")
.attr("stroke-width", d => 0.5 + 1 * d.fitness);
const zoomRect = svg.append("rect")
.attr("width", width)
.attr("height", height)
.style("fill", "none")
.style("pointer-events", "all")
const node = svg.append("g")
.attr("stroke", "#fff")
.attr("stroke-width", 1.5)
.selectAll("circle")
.data(nodes)
.enter().append("circle")
.attr("r", 5)
.attr("fill", "black");
const zoom = d3.zoom()
.scaleExtent([1/2, 64])
.on("zoom", zoomed);
zoomRect.call(zoom)
.call(zoom.translateTo, 0, 0);
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();
function zoomed() {
transform = d3.event.transform;
node.attr("transform", d3.event.transform);
link.attr("transform", d3.event.transform);
}
}
Insert cell
import {data} from '@borowski-9ld/invariants-graph'
Insert cell
height = 600
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