Public
Edited
Feb 3, 2023
Insert cell
Insert cell
Insert cell
nodes = [
{ id: "1", data: { x: 100, y: 100 } },
{ id: "2", data: { x: 150, y: 100 } }
]
Insert cell
edges = [{ id: "Edge", source: "1", target: "2", data: {} }]
Insert cell
Insert cell
graph = new GraphLib.Graph({ nodes, edges })
Insert cell
Insert cell
Insert cell
drawGraph = {
return async (graph) => {
const canvas = createCanvas();
invalidation.then(() => canvas.destroy());

await canvas.ready;
// Draw lines
graph.getAllEdges().forEach((edge) => {
drawEdge(canvas, edge);
});
// Draw nodes
graph.getAllNodes().forEach((node) => {
drawNode(canvas, node);
});

return canvas.getConfig().container;
};
}
Insert cell
Insert cell
Insert cell
Insert cell
addNode = (id) => {
// Add a new node on random position
graph.addNode({
id,
data: { x: _.random(10, 390), y: _.random(10, 390) }
});
// Connect it with a random node.
const allNodes = graph.getAllNodes();
const targetNode = allNodes[_.random(allNodes.length - 2)];
if (targetNode) {
graph.addEdge({
id,
source: id,
target: targetNode.id,
data: {}
});
}
}
Insert cell
removeNode = () => {
const allNodes = graph.getAllNodes();
const targetNode = allNodes[_.random(allNodes.length - 1)];
if (targetNode) {
graph.removeNode(targetNode.id);
}
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
drawGraphOnDemand = {
return async (graph) => {
const canvas = createCanvas();
invalidation.then(() => {
canvas.destroy();
graph.onChanged = () => {};
});
await canvas.ready;
// Draw existing lines.
graph.getAllEdges().forEach((edge) => {
drawEdge(canvas, edge);
});
// Draw existing nodes.
graph.getAllNodes().forEach((node) => {
drawNode(canvas, node);
});
// Update when changed.
graph.onChanged = (event) => {
event.changes.forEach((change) => {
if (change.type === "NodeAdded") {
const node = change.value;
drawNode(canvas, node);
} else if (change.type === "NodeRemoved") {
const node = change.value;
canvas.document.getElementById(node.id).remove();
} else if (change.type === "EdgeAdded") {
const edge = change.value;
drawEdge(canvas, edge);
} else if (change.type === "EdgeRemoved") {
const edge = change.value;
canvas.document.getElementById(edge.id).remove();
}
});
};
return canvas.getConfig().container;
};
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
addThreeNodes = (id) => {
graph.batch(() => {
addNode(id + "-1");
addNode(id + "-2");
addNode(id + "-3");
});
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more