blob: 7bee4c0ccb558d479ad0910fdac710a72a2efdc1 [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
204 circle.attr("fill", function(d) { if (d.group == 0){return "blue";}else{ return "gray"; }})
205
206 force.on("tick", tick);
207 path.exit().remove();
208 circle.exit().remove();
209// text.exit().remove();
210
211 force.start();
212
213 setInterval(function() {
214 $.ajax({
215// url: 'http://onosnat.onlab.us:8080/topology',
216 url: data_source,
217 success: function(json) {
218 update(json)
219 },
220 dataType: "json"
221 });
222 }, 3000);
223 }
224 function tick() {
225 path.attr("d", function(d) {
226 var dx = d.target.x - d.source.x,
227 dy = d.target.y - d.source.y,
228 dr = 1/d.linknum; //linknum is defined above
229 dr = 300;
230 return "M" + d.source.x + "," + d.source.y + "A" + dr + "," + dr + " 0 0,1 " + d.target.x + "," + d.target.y;
231 });
232// circle.attr("cx", function(d) { return d.x; }).attr("cy", function(d) { return d.y; });
233 circle.attr("transform", function(d) {
234 return "translate(" + d.x + "," + d.y + ")";
235 })
236 circle.attr("fill", function(d) { if (d.group == 0){return "blue";}else{ return "gray"; }});
237// text.attr("x", function(d) { return d.x; }).attr("y", function(d) { return d.y; });
238// text.attr("x", function(d) { return d.x; }).attr("y", function(d) { return d.y; });
239 text.attr("transform", function(d) {
240 return "translate(" + d.x + "," + d.y + ")";
241 });
242 }
243}