From 50b87f6d43b1ad5584b62ecac9b0f765104de4f5 Mon Sep 17 00:00:00 2001 From: Kevin Nadro Date: Wed, 22 Jun 2016 22:29:08 -0700 Subject: [PATCH] Allowing for custom node positions on graphs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Can add custom (x,y) positions for all the nodes in a graph! This code doesn’t not work or effect the graphs that are “trees” i.e using setTreeData. --- js/module/tracer/directed_graph.js | 14 +++++++++++--- js/module/tracer/tracer.js | 9 +++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/js/module/tracer/directed_graph.js b/js/module/tracer/directed_graph.js index 702dce2..434f60b 100644 --- a/js/module/tracer/directed_graph.js +++ b/js/module/tracer/directed_graph.js @@ -14,6 +14,8 @@ class DirectedGraphTracer extends Tracer { constructor(name) { super(name); + this.nodePositions = []; + if (this.isNew) initView(this); } @@ -43,6 +45,13 @@ class DirectedGraphTracer extends Tracer { return this; } + _setNodePositions(obj){ + for(var i = 0; i < obj.length; i++){ + this.nodePositions.push(obj[i]); + } + super.dirtyData(); + } + processStep(step, options) { switch (step.type) { case 'setTreeData': @@ -116,7 +125,6 @@ class DirectedGraphTracer extends Tracer { setData(G, undirected) { if (super.setData.apply(this, arguments)) return true; - this.graph.clear(); const nodes = []; const edges = []; @@ -127,8 +135,8 @@ class DirectedGraphTracer extends Tracer { nodes.push({ id: this.n(i), label: '' + i, - x: .5 + Math.sin(currentAngle) / 2, - y: .5 + Math.cos(currentAngle) / 2, + x: (this.nodePositions[i]) ? this.nodePositions[i].x : .5 + Math.sin(currentAngle) / 2, + y: (this.nodePositions[i]) ? this.nodePositions[i].y : .5 + Math.cos(currentAngle) / 2, size: 1, color: this.color.default, weight: 0 diff --git a/js/module/tracer/tracer.js b/js/module/tracer/tracer.js index 642c714..7040f40 100644 --- a/js/module/tracer/tracer.js +++ b/js/module/tracer/tracer.js @@ -77,12 +77,21 @@ class Tracer { $name.text(name || this.defaultName); } + dirtyData(){ + this.isNew = true; + } + + cleanData(){ + this.isNew = false; + } + setData() { const data = toJSON(arguments); if (!this.isNew && this.lastData === data) { return true; } this.lastData = this.capsule.lastData = data; + this.cleanData(); return false; } -- GitLab