Public
Edited
Jul 7, 2023
Insert cell
Insert cell
caseData = removeUselessEdges(largeGraphData)
Insert cell
viewof nodeCount = Inputs.range([0, caseData.nodes.length], {
label: "nodeCount",
step: 50,
value: 2000
})
Insert cell
viewof edgeCount = Inputs.range([0, caseData.edges.length], {
label: "edgeCount",
step: 50,
value: 2000
})
Insert cell
{
// 创建一个新的 dagre 图
const g = new graphlib.Graph();
// 设置图的默认排列方向为从左向右,以符合 dagre 布局的要求
g.setGraph({
rankdir: "LR",
align: "UL", // 左上角
nodesep: 4,
ranksep: 120
});
// 添加节点到图中
data.nodes.slice(0, nodeCount).forEach((node) => {
g.setNode(node.id, node);
});
data.edges.slice(0, edgeCount).forEach((edge) => {
g.setEdge(edge.source, edge.target, edge);
});
// 默认为每个边分配一个新的标签对象
g.setDefaultNodeLabel(() => ({}));
g.setDefaultEdgeLabel(() => ({}));
console.time("layout");
// 进行 dagre 布局,并计算节点的层级
dagre.layout(g);
console.timeEnd("layout");
return [g.nodes().map((d) => g.node(d)), g.nodes()];
}
Insert cell
removeUselessEdges = {
return (data) => {
const { nodes, edges } = data;
const idNode = new Map(nodes.map((d) => [d.id, d]));
const finalEdges = edges.filter(
(d) => idNode.has(d.source) && idNode.has(d.target)
);
return { nodes, edges: finalEdges };
};
}
Insert cell
Insert cell
largeGraphData = FileAttachment("large-graph-data.json").json()
Insert cell
data = graphdata626
Insert cell
graphdata626 = FileAttachment("graphdata-626.json").json()
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