From 4cc0b09d9bae57774e7825e86bcc403e13c47643 Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Wed, 3 Jul 2019 03:36:48 +0000 Subject: [PATCH] Layout checkout, fix TODO in Collection --- Collection.js | 16 +++++++--------- Layout.js | 38 ++++++++++++++++++++++++++++++++++++++ render.js | 4 +++- 3 files changed, 48 insertions(+), 10 deletions(-) create mode 100644 Layout.js diff --git a/Collection.js b/Collection.js index 540ab15..cf8856e 100644 --- a/Collection.js +++ b/Collection.js @@ -1,12 +1,12 @@ class Collection { constructor(nodes) { - this.nodes = nodes; + this.nodes = new Set(nodes); } setTension() { this.vec = [0, 0]; this.tension = 0; - for (let node of this.nodes) { + for (let node of this.nodes.values()) { node.setTension(); for (let i of [0, 1]) { this.vec[i] += node.vec[i]; @@ -16,11 +16,9 @@ class Collection { } offsetCollides(graph, offset) { - // TODO: make this.nodes always a set - let nodeSet = new Set(this.nodes); - for (let node of this.nodes) { + for (let node of this.nodes.values()) { let other = node.offsetCollides(graph, offset); - if (other && !nodeSet.has(other)) { + if (other && !this.nodes.has(other)) { return other; } } @@ -28,19 +26,19 @@ class Collection { } savePos() { - for (let node of this.nodes) { + for (let node of this.nodes.values()) { node.savePos(); } } restorePos(graph) { - for (let node of this.nodes) { + for (let node of this.nodes.values()) { node.restorePos(graph); } } moveBy(graph, offset) { - let nodes = new Set(this.nodes); + let nodes = new Set(this.nodes.values()); while (nodes.size) { for (let node of nodes) { if (node.offsetCollides(graph, offset)) { diff --git a/Layout.js b/Layout.js new file mode 100644 index 0000000..f8b17ae --- /dev/null +++ b/Layout.js @@ -0,0 +1,38 @@ +class Layout { + constructor(graph) { + this.nodesByPos = new Map(); + + this.graph_ = graph; + + this.setInitialPositions(); + } + + setInitialPositions() { + const SPACING = 4; + + let maxRankNodes = 0; + for (let nodes of this.graph_.nodesByPageRank.values()) { + maxRankNodes = Math.max(maxRankNodes, nodes.length); + } + + let ranks = Array.from(this.graph_.nodesByPageRank.keys()); + ranks.sort((a, b) => a - b); + for (let r = 0; r < ranks.length; ++r) { + let nodes = this.graph_.nodesByPageRank.get(ranks[r]); + for (let n = 0; n < nodes.length; ++n) { + let node = nodes[n]; + let pos = [ + r * SPACING, + Math.floor((nodes.length / 2) * SPACING) + (n * SPACING) + + (node.subgraph * SPACING * maxRankNodes), + ]; + node.pos = pos; + this.setNodePos(node, pos); + } + } + } + + setNodePos(node, pos) { + this.nodesByPos.set(pos.toString(), node); + } +} diff --git a/render.js b/render.js index 71d3b1c..9cb0a86 100644 --- a/render.js +++ b/render.js @@ -1,5 +1,7 @@ function onmessage(def) { - new Graph(def); + let graph = new Graph(def); + let layout = new Layout(graph); } +