Public
Edited
Feb 9, 2023
Insert cell
Insert cell
Insert cell
forceGraph = {
const initData = {
nodes: [ {id: 0 } ],
links: []
};

const Graph = ForceGraph3D()(domEl)
.enableNodeDrag(false)
.onNodeHover(node => domEl.style.cursor = node ? 'pointer' : null)
.onNodeClick(removeNode)
.graphData(initData);

setInterval(() => {
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)) }]
});
}, 1000);

return Graph;

//

function removeNode(node) {
let { nodes, links } = Graph.graphData();
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 });
}
}
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