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