Published
Edited
Oct 22, 2019
2 stars
Insert cell
Insert cell
Insert cell
Insert cell
{
const buttons = d3.select(viewof buttons);
buttons.select('input[name=add]').on('click', addNode)
buttons.select('input[name=remove]').on('click', removeNode)
function removeNode() {
let { nodes, links } = Graph.graphData();
if (nodes.length == 1) return;
const node = nodes[nodes.length - 1]; // Remove this one

links = links.filter(l => l.source !== node && l.target !== node); // Remove links attached to node
nodes.splice(node.id, 1); // Remove node
nodes.forEach((n, idx) => { n.id = idx; }); // Reset node ids to array index
Graph.graphData({ nodes, links });
}
function addNode() {
const { nodes, links } = Graph.graphData();
const id = nodes.length;

Graph.graphData({
nodes: [...nodes, { id }],
links: [...links, { source: id, target: Math.round(Math.random() * (id-1)) }]
});
}
return Graph.graphData().nodes
}
Insert cell
Graph = {
const n = 3;
const data = {
nodes: [...Array(n).keys()].map(i => ({ id: i })),
links: [...Array(n).keys()]
.filter(id => id)
.map(id => ({
source: id,
target: Math.round(Math.random() * (id-1))
}))
};
const Graph = ForceGraph3D()(container)
.graphData(data)
.width(width)
.height(300)
.nodeRelSize(10)
.nodeLabel(d => 'This is node #' + d.id)
.linkDirectionalParticles(2)
.linkWidth(2)
.linkDirectionalParticleWidth(5)
.nodeOpacity(1)
.onNodeHover(node => container.style.cursor = node ? 'pointer' : null)
.onNodeClick(node => {

});
// This determines the distance between nodes
const linkForce = Graph
.d3Force('link')
.distance(link => 100);
return Graph
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
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