blob: aee3eaff0074a57c355afe2eff5503921c9261a1 [file] [log] [blame]
Ubuntu82b8a832013-02-06 22:00:11 +00001function gui(data_source){
2 var width = 960,
3 height = 500;
4 var color = d3.scale.category20();
5
6 var svg = d3.select("body").append("svg:svg")
7 .attr("width", width)
8 .attr("height", height);
9
10 var force = d3.layout.force()
11 .charge(-500)
12 .linkDistance(100)
13 .size([width, height]);
14
15 var path = svg.selectAll("path");
16 var circle = svg.selectAll("circle");
17 var text = svg.selectAll("g");
18
19
20 d3.json(data_source,init);
21
22 function init(json){
23 nodes = force.nodes();
24 links = force.links();
25
26 json.nodes.forEach(function(item) {
27 nodes.push(item);
28 });
29 json.links.forEach(function(item) {
30 links.push(item);
31 });
32 draw(nodes, links);
33 }
34
35 function update(json) {
36 Array.prototype.diff2 = function(arr) {
37 return this.filter(function(i) {
38 for (var j = 0; j < arr.length ; j++) {
39 if (arr[j].source === i.source.index &&
40 arr[j].target === i.target.index)
41 return false;
42 }
43 return true;
44 });
45 };
46
47 Array.prototype.diff = function(arr) {
48 return this.filter(function(i) {
49 for (var j = 0; j < arr.length ; j++) {
50 if (arr[j].source.index === i.source &&
51 arr[j].target.index === i.target)
52 return false;
53 }
54 return true;
55 });
56 };
57
58 Array.prototype.node_diff = function(arr) {
59 return this.filter(function(i) {
60 for (var j = 0; j < arr.length ; j++) {
61 if (arr[j].name === i.name)
62 return false;
63 }
64 return true;
65 });
66 };
67
68
69// links.sort(function(a,b) {
70// if (a.source > b.source) {return 1;}
71// else if (a.source < b.source) {return -1;}
72// else {
73// if (a.target > b.target) {return 1;}
74// if (a.target < b.target) {return -1;}
75// else {return 0;}
76// }
77// });
78// for (var i=0; i<links.length; i++) {
79// if (i != 0 &&
80// links[i].source == links[i-1].source &&
81// links[i].target == links[i-1].target) {
82// links[i].linknum = links[i-1].linknum + 1;
83// }
84// else {links[i].linknum = 1;};
85// };
86
87
88
89 function cdiff(topo) {
90 var changed = false;
91 var l_adds = topo.links.diff(links);
92 var l_rems = links.diff2(topo.links);
93
94 var n_adds = topo.nodes.node_diff(nodes);
95 var n_rems = nodes.node_diff(topo.nodes);
96
97 for (var i = 0; i < l_rems.length ; i++) {
98 for (var j = 0; j < links.length; j++) {
99 if (links[j].source.index == l_rems[i].source.index &&
100 links[j].target.index == l_rems[i].target.index) {
101 links.splice(j,1);
102 changed = true;
103 break;
104 }
105 }
106 }
107 for (var i = 0; i < l_adds.length; i++) {
108 links.push(l_adds[i]);
109 changed = true;
110 }
111 for (var i = 0; i < n_rems.length; i++) {
112 for (var j = 0; j < nodes.length; j++) {
113 if ( nodes[j].name == n_rems[i].name ){
114 nodes.splice(j,1);
115 changed = true;
116 break;
117 }
118 }
119 }
120 for (var i = 0; i < n_adds.length; i++) {
121 nodes.push(n_adds[i]);
122 changed = true;
123 }
124 return changed
125 }
126
127
128
129 var changed = cdiff(json);
130 for (var i = 0; i < json.nodes.length; i++) {
131 nodes[i].group = json.nodes[i].group
132 }
133
134 console.log(circle);
135
136 console.log("changed?");
137 console.log(changed);
138
139
140 if (changed){
141 path = svg.selectAll("path").data(links)
142 circle = svg.selectAll("circle").data(nodes);
143 text = svg.selectAll("text").data(nodes);
144
145 force.stop();
146
147 path.enter().append("svg:path")
148 .attr("class", function(d) { return "link"; })
149 .attr("marker-end", "url(#Triangle)");
150
151 circle.enter().append("svg:circle")
152 .attr("r", 6)
153 .call(force.drag);
154
155/* text.enter().append("svg:text")
156 .attr("x", 8)
157 .attr("y", ".31em")
158 .attr("class", "shadow")
159 .text(function(d) { return d.name.split(":")[7]; }); */
160
161 text.enter().append("svg:text")
162 .attr("x", 8)
163 .attr("y", ".31em")
164 .text(function(d) { return d.name.split(":")[7]; });
165
166 circle.append("title")
167 .text(function(d) { return d.name; });
168
169 path.exit().remove();
170 circle.exit().remove();
171 text.exit().remove();
172
173 force.on("tick", tick);
174 force.start();
175 }
176 }
177 function draw(nodes, links){
178 path = svg.append("svg:g").selectAll("path").data(links)
179 circle = svg.append("svg:g").selectAll("circle").data(nodes);
180 text = svg.append("svg:g").selectAll("text").data(nodes);
181
182 path.enter().append("svg:path")
183 .attr("class", function(d) { return "link"; })
184 .attr("marker-end", "url(#Triangle)");
185
186 circle.enter().append("svg:circle")
187 .attr("r", 8)
188 .call(force.drag);
189
190/* text.enter().append("svg:text")
191 .attr("x", 8)
192 .attr("y", ".31em")
193 .attr("class", "shadow")
194 .text(function(d) { return d.name.split(":")[7]; }); */
195
196 text.enter().append("svg:text")
197 .attr("x", 8)
198 .attr("y", ".31em")
199 .text(function(d) { return d.name.split(":")[7]; });
200
201 circle.append("title")
202 .text(function(d) { return d.name; });
203
Ubuntuaea2a682013-02-08 08:30:10 +0000204 circle.attr("fill", function(d) {
205 if (d.group == 1){return "red";}
206 else if (d.group == 2){return "blue";}
207 else if (d.group == 3){return "green";}
208 else{ return "gray"; }
209 });
Ubuntu82b8a832013-02-06 22:00:11 +0000210
211 force.on("tick", tick);
212 path.exit().remove();
213 circle.exit().remove();
214// text.exit().remove();
215
216 force.start();
217
218 setInterval(function() {
219 $.ajax({
220// url: 'http://onosnat.onlab.us:8080/topology',
221 url: data_source,
222 success: function(json) {
223 update(json)
224 },
225 dataType: "json"
226 });
227 }, 3000);
228 }
229 function tick() {
230 path.attr("d", function(d) {
231 var dx = d.target.x - d.source.x,
232 dy = d.target.y - d.source.y,
233 dr = 1/d.linknum; //linknum is defined above
234 dr = 300;
235 return "M" + d.source.x + "," + d.source.y + "A" + dr + "," + dr + " 0 0,1 " + d.target.x + "," + d.target.y;
236 });
237// circle.attr("cx", function(d) { return d.x; }).attr("cy", function(d) { return d.y; });
238 circle.attr("transform", function(d) {
239 return "translate(" + d.x + "," + d.y + ")";
240 })
Ubuntuaea2a682013-02-08 08:30:10 +0000241 circle.attr("fill", function(d) {
242 if (d.group == 1){return "red";}
243 else if (d.group == 2){return "blue";}
244 else if (d.group == 3){return "green";}
245 else{ return "gray"; }
246 });
Ubuntu82b8a832013-02-06 22:00:11 +0000247// text.attr("x", function(d) { return d.x; }).attr("y", function(d) { return d.y; });
248// text.attr("x", function(d) { return d.x; }).attr("y", function(d) { return d.y; });
249 text.attr("transform", function(d) {
250 return "translate(" + d.x + "," + d.y + ")";
251 });
252 }
253}