blob: 66d7cbd5a3230e216f701b08547be609ce30cbfa [file] [log] [blame]
Javier López-Contreras052503f2018-12-26 12:34:42 +01001// s is the sigma graph
2// graf is the JSON graph
3var s, graf;
4
5// query dario JSON for the graph information
6function xhr(method, url, params, callback) {
avm99963027b5b02018-12-28 02:31:46 +01007 var http = new XMLHttpRequest();
8 if (method == "POST") {
9 http.open(method, url, true);
10 } else {
11 if (params != "") {
12 http.open(method, url+"?"+params, true);
13 } else {
14 http.open(method, url, true);
15 }
16 }
17 http.onload = function() {
18 if(this.status != 200) {
19 console.warn("Attention, status code "+this.status+" when loading via xhr url "+url);
20 }
21 callback(this.responseText, this.status);
22 };
23 if (method == "POST") {
24 http.setRequestHeader("Content-type","application/x-www-form-urlencoded");
25 http.send(params);
26 } else {
27 http.send();
28 }
Javier López-Contreras052503f2018-12-26 12:34:42 +010029}
30
31
32function initGraf() {
avm99963027b5b02018-12-28 02:31:46 +010033 // create new methods for sigma library
34 updateSigma();
Javier López-Contreras052503f2018-12-26 12:34:42 +010035
avm99963027b5b02018-12-28 02:31:46 +010036 // create graf, s is the sigma graf
37 s = new sigma({
38 renderers: [{
39 container: "graf",
40 type: "webgl"
41 }],
42 settings: {
43 defaultEdgeColor: "#fff",
44 edgeColor: "default",
45 defaultLabelColor: "#fff",
46 autoRescale: false,
47 zoomMax: 30,
48 // enableEdgeHovering: true,
49 font: "Roboto",
50 labelThreshold: 5
51 }
52 });
Javier López-Contreras052503f2018-12-26 12:34:42 +010053
54
avm99963027b5b02018-12-28 02:31:46 +010055 // query for JSON for graph data
56 xhr("GET", "api.php", "action=getgraf", function(responseText, status) {
57 // graf is the JSON data
58 graf = JSON.parse(responseText);
Javier López-Contreras052503f2018-12-26 12:34:42 +010059
avm99963027b5b02018-12-28 02:31:46 +010060 // does graf.nodes have a size attribute?
61 var rectBorrar = [[0,0], [0,0], [0,0], [0,0]];
62 for (var i in graf.nodes) {
63 if (graf.nodes[i].name == "Erase") rectBorrar[0] = [ graf.nodes[i].x , graf.nodes[i].y ];
64 if (graf.nodes[i].name == "Borrar") rectBorrar[1] = [ graf.nodes[i].x , graf.nodes[i].y ];
65 if (graf.nodes[i].name == "Esborrar") rectBorrar[2] = [ graf.nodes[i].x , graf.nodes[i].y ];
66 if (graf.nodes[i].name == "Delete") rectBorrar[3] = [ graf.nodes[i].x , graf.nodes[i].y ];
67 }
68
69 var sizegraf = 0;
70 for (var i in graf.nodes) {
avm999639f5952a2018-12-31 01:36:38 +010071 if (isInRect(graf.nodes[i].x, graf.nodes[i].y, rectBorrar)) continue;
avm99963027b5b02018-12-28 02:31:46 +010072 sizegraf++;
73 }
74 var nnode = 0;
75 for (var i in graf.nodes) {
76 var ncolor = null;
77
78 if(graf.nodes[i].sex =="F") ncolor = "#d61c08";
79 else if(graf.nodes[i].sex == "M") ncolor = "#0159aa";
80 else ncolor = "#0ca80a";
81
82 // post-processing for year corrections
83 if(1970 < graf.nodes[i].year && graf.nodes[i].year < 2004) graf.nodes[i].year += 18;
84
avm999639f5952a2018-12-31 01:36:38 +010085 var newX = 5000*Math.cos( 2*Math.PI*nnode/sizegraf ) + 725;
86 var newY = 5000*Math.sin( 2*Math.PI*nnode/sizegraf ) + 800;
avm99963027b5b02018-12-28 02:31:46 +010087
88 if (isInRect(graf.nodes[i].x, graf.nodes[i].y, rectBorrar) ) continue;
89
90 s.graph.addNode({
91 // we add color, originalColor, size, originalX..Y, circleX..Y atributes
92 id: graf.nodes[i].id,
93 year: graf.nodes[i].year,
94 sex: graf.nodes[i].sex,
95 label: graf.nodes[i].name,
96 x: graf.nodes[i].x,
97 y: graf.nodes[i].y,
98 circleX: newX,
99 circleY: newY,
100 originalX: graf.nodes[i].x,
101 originalY: graf.nodes[i].y,
102 size: 10,
103 color: ncolor,
104 originalColor: ncolor
105 });
106 nnode++;
107
108 }
109
110 for (var i in graf.edges) {
111 if (isInRect(graf.nodes[graf.edges[i].a].x, graf.nodes[graf.edges[i].a].y, rectBorrar)) continue;
112 if (isInRect(graf.nodes[graf.edges[i].b].x, graf.nodes[graf.edges[i].b].y, rectBorrar)) continue;
113
114 s.graph.addEdge({
115 id: i,
116 source: graf.edges[i].a,
117 target: graf.edges[i].b,
118 size: Math.min(4, Math.max((7/(2*Math.pow(20, 2)))*Math.pow(graf.edges[i].votes, 2) + 1/2, 0.5))
119 });
120
121 }
122
123 s.bind('clickNode', function(e) {
124 var nodeId = e.data.node.id,
125 toKeep = s.graph.neighbors(nodeId);
126 // toKeep[nodeId] = e.data.node;
127
128
129 s.graph.nodes().forEach(function(n) {
130 if (toKeep[n.id] || n.id == nodeId) {
131 n.color = n.originalColor;
132 } else {
133 n.color = '#333';
134 }
135 });
136
137 s.graph.edges().forEach(function(e) {
138 if ((e.source == nodeId || e.target == nodeId) && (toKeep[e.source] || toKeep[e.target])) {
139 e.color = '#fff';
140 } else {
141 e.color = '#333';
142 }
143 });
144
145 if (circleMode) {
146 s.graph.nodes().forEach(function (n) {
147 n.x = n.circleX;
148 n.y = n.circleY;
149 n.size = 10;
150 });
151
152 e.data.node.x = 0;
153 e.data.node.y = 0;
154 e.data.node.size = 30;
155 }
156
157 s.refresh();
158
159 dialog.show(nodeId, toKeep);
160 });
161
162 initDialog();
163 initCamera();
164 initSearchBar();
165
166 s.refresh();
avm9996323805b52018-12-31 00:32:50 +0100167 autocomplete(document.querySelector("#search-input"), graf.nodes, "search", rectBorrar);
avm99963027b5b02018-12-28 02:31:46 +0100168 });
Javier López-Contreras052503f2018-12-26 12:34:42 +0100169}
170
171function updateSigma() {
avm99963027b5b02018-12-28 02:31:46 +0100172 // returns set of neighouts
173 sigma.classes.graph.addMethod("neighbors", function(nodeId) {
174 var k,
175 neighbors = {},
176 index = this.allNeighborsIndex[nodeId] || [];
Javier López-Contreras052503f2018-12-26 12:34:42 +0100177
avm99963027b5b02018-12-28 02:31:46 +0100178 for (k in index) {
179 neighbors[k] = this.nodesIndex[k];
180 }
Javier López-Contreras052503f2018-12-26 12:34:42 +0100181
avm99963027b5b02018-12-28 02:31:46 +0100182 return neighbors;
183 });
Javier López-Contreras052503f2018-12-26 12:34:42 +0100184
avm99963027b5b02018-12-28 02:31:46 +0100185 // returns number of neighbours from a set of years
186 sigma.classes.graph.addMethod("numNeighborsFromYears", function(nodeId, showYearsCopy) {
187 var k,
188 neighbors = 0,
189 index = this.allNeighborsIndex[nodeId] || [];
190
191 for (k in index) {
192 if(this.nodesIndex){
193 if (showYearsCopy.has("" + this.nodesIndex[k].year)) neighbors++;
194 else if (this.nodesIndex[k].year == 0) neighbors++;
195 }
196 }
197
198 return neighbors;
199 });
200}