Javier López-Contreras | 052503f | 2018-12-26 12:34:42 +0100 | [diff] [blame] | 1 | // s is the sigma graph
|
| 2 | // graf is the JSON graph
|
| 3 | var s, graf;
|
| 4 |
|
| 5 | // query dario JSON for the graph information
|
| 6 | function xhr(method, url, params, callback) {
|
avm99963 | 027b5b0 | 2018-12-28 02:31:46 +0100 | [diff] [blame] | 7 | 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() {
|
Adrià Vilanova Martínez | 1082a13 | 2021-07-08 16:54:10 +0200 | [diff] [blame^] | 18 | if (this.status != 200) {
|
avm99963 | 027b5b0 | 2018-12-28 02:31:46 +0100 | [diff] [blame] | 19 | console.warn("Attention, status code "+this.status+" when loading via xhr url "+url);
|
Adrià Vilanova Martínez | 1082a13 | 2021-07-08 16:54:10 +0200 | [diff] [blame^] | 20 |
|
| 21 | console.log(response);
|
| 22 | if (('error' in response) && ('msg' in response)) {
|
| 23 | alert(response.msg);
|
| 24 | }
|
avm99963 | 027b5b0 | 2018-12-28 02:31:46 +0100 | [diff] [blame] | 25 | }
|
| 26 | callback(this.responseText, this.status);
|
| 27 | };
|
| 28 | if (method == "POST") {
|
| 29 | http.setRequestHeader("Content-type","application/x-www-form-urlencoded");
|
| 30 | http.send(params);
|
| 31 | } else {
|
| 32 | http.send();
|
| 33 | }
|
Javier López-Contreras | 052503f | 2018-12-26 12:34:42 +0100 | [diff] [blame] | 34 | }
|
| 35 |
|
| 36 |
|
| 37 | function initGraf() {
|
avm99963 | 027b5b0 | 2018-12-28 02:31:46 +0100 | [diff] [blame] | 38 | // create new methods for sigma library
|
| 39 | updateSigma();
|
Javier López-Contreras | 052503f | 2018-12-26 12:34:42 +0100 | [diff] [blame] | 40 |
|
avm99963 | 027b5b0 | 2018-12-28 02:31:46 +0100 | [diff] [blame] | 41 | // create graf, s is the sigma graf
|
| 42 | s = new sigma({
|
| 43 | renderers: [{
|
| 44 | container: "graf",
|
| 45 | type: "webgl"
|
| 46 | }],
|
| 47 | settings: {
|
| 48 | defaultEdgeColor: "#fff",
|
| 49 | edgeColor: "default",
|
| 50 | defaultLabelColor: "#fff",
|
| 51 | autoRescale: false,
|
| 52 | zoomMax: 30,
|
| 53 | // enableEdgeHovering: true,
|
| 54 | font: "Roboto",
|
| 55 | labelThreshold: 5
|
| 56 | }
|
| 57 | });
|
Javier López-Contreras | 052503f | 2018-12-26 12:34:42 +0100 | [diff] [blame] | 58 |
|
| 59 |
|
avm99963 | 027b5b0 | 2018-12-28 02:31:46 +0100 | [diff] [blame] | 60 | // query for JSON for graph data
|
| 61 | xhr("GET", "api.php", "action=getgraf", function(responseText, status) {
|
| 62 | // graf is the JSON data
|
| 63 | graf = JSON.parse(responseText);
|
Javier López-Contreras | 052503f | 2018-12-26 12:34:42 +0100 | [diff] [blame] | 64 |
|
avm99963 | 027b5b0 | 2018-12-28 02:31:46 +0100 | [diff] [blame] | 65 | // does graf.nodes have a size attribute?
|
| 66 | var rectBorrar = [[0,0], [0,0], [0,0], [0,0]];
|
| 67 | for (var i in graf.nodes) {
|
| 68 | if (graf.nodes[i].name == "Erase") rectBorrar[0] = [ graf.nodes[i].x , graf.nodes[i].y ];
|
| 69 | if (graf.nodes[i].name == "Borrar") rectBorrar[1] = [ graf.nodes[i].x , graf.nodes[i].y ];
|
| 70 | if (graf.nodes[i].name == "Esborrar") rectBorrar[2] = [ graf.nodes[i].x , graf.nodes[i].y ];
|
| 71 | if (graf.nodes[i].name == "Delete") rectBorrar[3] = [ graf.nodes[i].x , graf.nodes[i].y ];
|
| 72 | }
|
| 73 |
|
| 74 | var sizegraf = 0;
|
| 75 | for (var i in graf.nodes) {
|
avm99963 | 9f5952a | 2018-12-31 01:36:38 +0100 | [diff] [blame] | 76 | if (isInRect(graf.nodes[i].x, graf.nodes[i].y, rectBorrar)) continue;
|
avm99963 | 027b5b0 | 2018-12-28 02:31:46 +0100 | [diff] [blame] | 77 | sizegraf++;
|
| 78 | }
|
| 79 | var nnode = 0;
|
| 80 | for (var i in graf.nodes) {
|
| 81 | var ncolor = null;
|
| 82 |
|
| 83 | if(graf.nodes[i].sex =="F") ncolor = "#d61c08";
|
| 84 | else if(graf.nodes[i].sex == "M") ncolor = "#0159aa";
|
| 85 | else ncolor = "#0ca80a";
|
| 86 |
|
| 87 | // post-processing for year corrections
|
| 88 | if(1970 < graf.nodes[i].year && graf.nodes[i].year < 2004) graf.nodes[i].year += 18;
|
| 89 |
|
avm99963 | 9f5952a | 2018-12-31 01:36:38 +0100 | [diff] [blame] | 90 | var newX = 5000*Math.cos( 2*Math.PI*nnode/sizegraf ) + 725;
|
| 91 | var newY = 5000*Math.sin( 2*Math.PI*nnode/sizegraf ) + 800;
|
avm99963 | 027b5b0 | 2018-12-28 02:31:46 +0100 | [diff] [blame] | 92 |
|
| 93 | if (isInRect(graf.nodes[i].x, graf.nodes[i].y, rectBorrar) ) continue;
|
| 94 |
|
| 95 | s.graph.addNode({
|
| 96 | // we add color, originalColor, size, originalX..Y, circleX..Y atributes
|
| 97 | id: graf.nodes[i].id,
|
| 98 | year: graf.nodes[i].year,
|
| 99 | sex: graf.nodes[i].sex,
|
| 100 | label: graf.nodes[i].name,
|
| 101 | x: graf.nodes[i].x,
|
| 102 | y: graf.nodes[i].y,
|
| 103 | circleX: newX,
|
| 104 | circleY: newY,
|
| 105 | originalX: graf.nodes[i].x,
|
| 106 | originalY: graf.nodes[i].y,
|
| 107 | size: 10,
|
| 108 | color: ncolor,
|
| 109 | originalColor: ncolor
|
| 110 | });
|
| 111 | nnode++;
|
| 112 |
|
| 113 | }
|
| 114 |
|
| 115 | for (var i in graf.edges) {
|
| 116 | if (isInRect(graf.nodes[graf.edges[i].a].x, graf.nodes[graf.edges[i].a].y, rectBorrar)) continue;
|
| 117 | if (isInRect(graf.nodes[graf.edges[i].b].x, graf.nodes[graf.edges[i].b].y, rectBorrar)) continue;
|
| 118 |
|
| 119 | s.graph.addEdge({
|
| 120 | id: i,
|
| 121 | source: graf.edges[i].a,
|
| 122 | target: graf.edges[i].b,
|
| 123 | size: Math.min(4, Math.max((7/(2*Math.pow(20, 2)))*Math.pow(graf.edges[i].votes, 2) + 1/2, 0.5))
|
| 124 | });
|
| 125 |
|
| 126 | }
|
| 127 |
|
| 128 | s.bind('clickNode', function(e) {
|
| 129 | var nodeId = e.data.node.id,
|
| 130 | toKeep = s.graph.neighbors(nodeId);
|
| 131 | // toKeep[nodeId] = e.data.node;
|
| 132 |
|
| 133 |
|
| 134 | s.graph.nodes().forEach(function(n) {
|
| 135 | if (toKeep[n.id] || n.id == nodeId) {
|
| 136 | n.color = n.originalColor;
|
| 137 | } else {
|
| 138 | n.color = '#333';
|
| 139 | }
|
| 140 | });
|
| 141 |
|
| 142 | s.graph.edges().forEach(function(e) {
|
| 143 | if ((e.source == nodeId || e.target == nodeId) && (toKeep[e.source] || toKeep[e.target])) {
|
| 144 | e.color = '#fff';
|
| 145 | } else {
|
| 146 | e.color = '#333';
|
| 147 | }
|
| 148 | });
|
| 149 |
|
| 150 | if (circleMode) {
|
| 151 | s.graph.nodes().forEach(function (n) {
|
| 152 | n.x = n.circleX;
|
| 153 | n.y = n.circleY;
|
| 154 | n.size = 10;
|
| 155 | });
|
| 156 |
|
| 157 | e.data.node.x = 0;
|
| 158 | e.data.node.y = 0;
|
| 159 | e.data.node.size = 30;
|
| 160 | }
|
| 161 |
|
| 162 | s.refresh();
|
| 163 |
|
| 164 | dialog.show(nodeId, toKeep);
|
| 165 | });
|
| 166 |
|
| 167 | initDialog();
|
| 168 | initCamera();
|
| 169 | initSearchBar();
|
| 170 |
|
| 171 | s.refresh();
|
avm99963 | 23805b5 | 2018-12-31 00:32:50 +0100 | [diff] [blame] | 172 | autocomplete(document.querySelector("#search-input"), graf.nodes, "search", rectBorrar);
|
avm99963 | 027b5b0 | 2018-12-28 02:31:46 +0100 | [diff] [blame] | 173 | });
|
Javier López-Contreras | 052503f | 2018-12-26 12:34:42 +0100 | [diff] [blame] | 174 | }
|
| 175 |
|
| 176 | function updateSigma() {
|
avm99963 | 027b5b0 | 2018-12-28 02:31:46 +0100 | [diff] [blame] | 177 | // returns set of neighouts
|
| 178 | sigma.classes.graph.addMethod("neighbors", function(nodeId) {
|
| 179 | var k,
|
| 180 | neighbors = {},
|
| 181 | index = this.allNeighborsIndex[nodeId] || [];
|
Javier López-Contreras | 052503f | 2018-12-26 12:34:42 +0100 | [diff] [blame] | 182 |
|
avm99963 | 027b5b0 | 2018-12-28 02:31:46 +0100 | [diff] [blame] | 183 | for (k in index) {
|
| 184 | neighbors[k] = this.nodesIndex[k];
|
| 185 | }
|
Javier López-Contreras | 052503f | 2018-12-26 12:34:42 +0100 | [diff] [blame] | 186 |
|
avm99963 | 027b5b0 | 2018-12-28 02:31:46 +0100 | [diff] [blame] | 187 | return neighbors;
|
| 188 | });
|
Javier López-Contreras | 052503f | 2018-12-26 12:34:42 +0100 | [diff] [blame] | 189 |
|
avm99963 | 027b5b0 | 2018-12-28 02:31:46 +0100 | [diff] [blame] | 190 | // returns number of neighbours from a set of years
|
| 191 | sigma.classes.graph.addMethod("numNeighborsFromYears", function(nodeId, showYearsCopy) {
|
| 192 | var k,
|
| 193 | neighbors = 0,
|
| 194 | index = this.allNeighborsIndex[nodeId] || [];
|
| 195 |
|
| 196 | for (k in index) {
|
| 197 | if(this.nodesIndex){
|
| 198 | if (showYearsCopy.has("" + this.nodesIndex[k].year)) neighbors++;
|
| 199 | else if (this.nodesIndex[k].year == 0) neighbors++;
|
| 200 | }
|
| 201 | }
|
| 202 |
|
| 203 | return neighbors;
|
| 204 | });
|
| 205 | }
|