Skip to content

Commit 6c05885

Browse files
authored
Merge pull request algorithm-visualizer#193 from nadr0/setting-graph-nodes
Allowing for custom node positions on graphs
2 parents 10fe085 + a46f701 commit 6c05885

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

js/module/tracer/directed_graph.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ class DirectedGraphTracer extends Tracer {
1414
constructor(name) {
1515
super(name);
1616

17+
this.nodePositions = [];
18+
1719
if (this.isNew) initView(this);
1820
}
1921

@@ -43,6 +45,13 @@ class DirectedGraphTracer extends Tracer {
4345
return this;
4446
}
4547

48+
_setNodePositions(obj){
49+
for(var i = 0; i < obj.length; i++){
50+
this.nodePositions.push(obj[i]);
51+
}
52+
super.dirtyData();
53+
}
54+
4655
processStep(step, options) {
4756
switch (step.type) {
4857
case 'setTreeData':
@@ -116,7 +125,6 @@ class DirectedGraphTracer extends Tracer {
116125

117126
setData(G, undirected) {
118127
if (super.setData.apply(this, arguments)) return true;
119-
120128
this.graph.clear();
121129
const nodes = [];
122130
const edges = [];
@@ -127,8 +135,8 @@ class DirectedGraphTracer extends Tracer {
127135
nodes.push({
128136
id: this.n(i),
129137
label: '' + i,
130-
x: .5 + Math.sin(currentAngle) / 2,
131-
y: .5 + Math.cos(currentAngle) / 2,
138+
x: (this.nodePositions[i]) ? this.nodePositions[i].x : .5 + Math.sin(currentAngle) / 2,
139+
y: (this.nodePositions[i]) ? this.nodePositions[i].y : .5 + Math.cos(currentAngle) / 2,
132140
size: 1,
133141
color: this.color.default,
134142
weight: 0

js/module/tracer/tracer.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,21 @@ class Tracer {
7777
$name.text(name || this.defaultName);
7878
}
7979

80+
dirtyData(){
81+
this.isNew = true;
82+
}
83+
84+
cleanData(){
85+
this.isNew = false;
86+
}
87+
8088
setData() {
8189
const data = toJSON(arguments);
8290
if (!this.isNew && this.lastData === data) {
8391
return true;
8492
}
8593
this.lastData = this.capsule.lastData = data;
94+
this.cleanData();
8695
return false;
8796
}
8897

0 commit comments

Comments
 (0)