vis.js is a dynamic, browser-based visualization library
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

441 lines
12 KiB

  1. function selectCompany(event) {
  2. var nodeId = event.value;
  3. network.selectNodes([nodeId]);
  4. highlightConnections({nodes:[nodeId]})
  5. network.focusOnNode(nodeId,{animation:false, scale:0.2})
  6. }
  7. function selectYear(year) {
  8. selectedYear = year
  9. console.log(year)
  10. populateYearDiv();
  11. recursiveClearDOM(document.getElementById("projectsDropdown"));
  12. populateProjectsDropdown();
  13. recursiveClearDOM(document.getElementById("companyDropdown"));
  14. }
  15. function selectProject(event) {
  16. if (event.value != "none") {
  17. selectedProject = event.value;
  18. }
  19. if (selectedType != "") {
  20. if (network) {
  21. network.destroy()
  22. network = null;
  23. }
  24. loadJSON('./data/combined/' + selectedYear + "_" + selectedProject + ".json", drawAll);
  25. document.getElementById("FILENAME").innerHTML = selectedYear + "_" + selectedProject + ".json";
  26. }
  27. }
  28. function selectType(type) {
  29. if (selectedType != type) {
  30. selectedType = type;
  31. totalValue = 0;
  32. populateTypeDiv();
  33. var allNodes = nodes.get();
  34. if (allNodes.length > 0) {
  35. if (selectedType == "connections") {
  36. for (var i = 0; i < allNodes.length; i++) {
  37. var node = allNodes[i];
  38. node.title = node.id + ": " + node.euData[selectedType];
  39. node.value = Math.max(1,Math.pow(Number(node.euData[selectedType]),1.05));
  40. totalValue += node.value;
  41. }
  42. }
  43. else {
  44. for (var i = 0; i < allNodes.length; i++) {
  45. var node = allNodes[i];
  46. node.title = node.id + ": " + Math.round(Number(node.euData[selectedType])) + " Euro";
  47. node.value = Math.max(Math.round(0.0001 * Number(node.euData[selectedType])),1);
  48. totalValue += node.value;
  49. }
  50. }
  51. nodes.update(allNodes);
  52. }
  53. }
  54. }
  55. function recursiveClearDOM(DOMobject) {
  56. while (DOMobject.hasChildNodes() == true) {
  57. recursiveClearDOM(DOMobject.firstChild);
  58. DOMobject.removeChild(DOMobject.firstChild);
  59. }
  60. }
  61. function viewAllNeighbours() {
  62. network.zoomExtent({nodes:connectedNodes, duration:0})
  63. }
  64. function doSteps(amount) {
  65. network.setFreezeSimulation(false);
  66. network.setOptions({stabilizationIterations:amount})
  67. network._stabilize()
  68. network.setFreezeSimulation(true);
  69. }
  70. var network;
  71. var nodes;
  72. var edges;
  73. var edgeOpacity = 0.15;
  74. var totalValue = 0;
  75. function drawAll(dataJSON, file) {
  76. // create an array with nodes
  77. nodes = new vis.DataSet(dataJSON.nodes);
  78. // create an array with edges
  79. edges = new vis.DataSet(dataJSON.edges);
  80. var totalMass = 0;
  81. totalValue = 0;
  82. for (var i = 0; i < dataJSON.nodes.length; i++) {
  83. totalMass += dataJSON.nodes[i].mass;
  84. totalValue += dataJSON.nodes[i].value;
  85. }
  86. var gravityConstant = -20000;
  87. if (totalMass < 2000) {
  88. gravityConstant = -2000;
  89. }
  90. var edgeNodeRatio = Math.max(1,dataJSON.edges.length) / Math.max(1,dataJSON.nodes.length);
  91. var nodeEdgeRatio = Math.max(1,dataJSON.nodes.length) / Math.max(1,dataJSON.edges.length);
  92. var centralGravity = Math.min(5,Math.max(0.1,edgeNodeRatio));
  93. edgeOpacity = Math.min(1.0,Math.max(0.15,nodeEdgeRatio*2));
  94. var container = document.getElementById('mynetwork');
  95. var data = {
  96. nodes: nodes,
  97. edges: edges
  98. };
  99. var amountOfNodes = dataJSON.nodes.length;
  100. var options = {
  101. stabilize: false,
  102. stabilizationIterations: 15000,
  103. smoothCurves: {
  104. enabled: true,
  105. dynamic: false
  106. },
  107. configurePhysics: false,
  108. physics: {barnesHut: {gravitationalConstant: gravityConstant, centralGravity: centralGravity, springLength: 100, springConstant: 0.001}},
  109. //physics: {barnesHut: {gravitationalConstant: 0, centralGravity: 0.0, springConstant: 0}},
  110. nodes: {
  111. shape: 'dot',
  112. radiusMax: amountOfNodes * 0.5,
  113. fontColor: '#ffffff',
  114. fontDrawThreshold: 8,
  115. scaleFontWithValue: true,
  116. fontSizeMin: 14,
  117. fontSizeMax: amountOfNodes * 0.25,
  118. fontSizeMaxVisible: 20,
  119. fontStrokeWidth: 1, // px
  120. fontStrokeColor: '#222222'
  121. },
  122. edges: {
  123. opacity: edgeOpacity
  124. },
  125. hideEdgesOnDrag: true,
  126. maxVelocity: 100,
  127. tooltip: {
  128. delay: 200,
  129. fontSize: 12,
  130. color: {
  131. background: "#fff"
  132. }
  133. }
  134. };
  135. network = new vis.Network(container, data, options);
  136. network.setFreezeSimulation(true);
  137. network.on("click",highlightConnections);
  138. populateCompanyDropdown();
  139. window.onresize = function () {network.redraw();};
  140. }
  141. // marked is used so we don't do heavy stuff on each click
  142. var marked = false;
  143. var connectedNodes = [];
  144. function highlightConnections(selectedItems) {
  145. if (selectedItems.nodes[0] == 'none') {
  146. return;
  147. }
  148. var nodeId;
  149. var requireUpdate = false;
  150. // we get all data from the dataset once to avoid updating multiple times.
  151. if (selectedItems.nodes.length == 0 && marked === true) {
  152. connectedNodes = [];
  153. requireUpdate = true;
  154. var allNodes = nodes.get({returnType:"Object"});
  155. // restore on unselect
  156. for (nodeId in allNodes) {
  157. allNodes[nodeId].color = undefined;
  158. if (allNodes[nodeId].oldLabel !== undefined) {
  159. allNodes[nodeId].label = allNodes[nodeId].oldLabel;
  160. allNodes[nodeId].oldLabel = undefined;
  161. }
  162. }
  163. marked = false;
  164. network.setOptions({nodes:{fontSizeMin:14},edges:{opacity:edgeOpacity}})
  165. }
  166. else if (selectedItems.nodes.length > 0) {
  167. var allNodes = nodes.get({returnType:"Object"});
  168. marked = true;
  169. requireUpdate = true;
  170. var mainNode = selectedItems.nodes[0]; // this is an ID
  171. connectedNodes = network.getConnectedNodes(mainNode);
  172. connectedNodes.push(mainNode);
  173. for (nodeId in allNodes) {
  174. allNodes[nodeId].color = 'rgba(150,150,150,0.1)';
  175. if (allNodes[nodeId].oldLabel === undefined) {
  176. allNodes[nodeId].oldLabel = allNodes[nodeId].label;
  177. allNodes[nodeId].label = "";
  178. }
  179. }
  180. for (var i = 0; i < connectedNodes.length; i++) {
  181. allNodes[connectedNodes[i]].color = undefined;
  182. if (allNodes[connectedNodes[i]].oldLabel !== undefined) {
  183. allNodes[connectedNodes[i]].label = allNodes[connectedNodes[i]].oldLabel;
  184. allNodes[connectedNodes[i]].oldLabel = undefined;
  185. }
  186. }
  187. // we want to set the fontSizeMin just so that the node we're looking at has a good fontsize at the zoomLevel
  188. network.setOptions({nodes:{fontSizeMin:150},edges:{opacity:0.025}})
  189. }
  190. if (requireUpdate === true) {
  191. var updateArray = [];
  192. for (nodeId in allNodes) {
  193. updateArray.push(allNodes[nodeId]);
  194. }
  195. nodes.update(updateArray);
  196. }
  197. }
  198. function loadJSON(path, success, error) {
  199. selectedFile = path;
  200. var xhr = new XMLHttpRequest();
  201. xhr.onreadystatechange = function() {
  202. if (xhr.readyState === 4) {
  203. if (xhr.status === 200) {
  204. success(JSON.parse(xhr.responseText), path);
  205. }
  206. else {
  207. error();
  208. }
  209. }
  210. };
  211. xhr.open("GET", path, true);
  212. xhr.send();
  213. }
  214. function populateCompanyDropdown() {
  215. var nodesAr = nodes.get();
  216. var nodeIds = [];
  217. for (var i = 0; i < nodesAr.length;i++) {
  218. nodeIds.push(nodesAr[i].id);
  219. }
  220. nodeIds.sort()
  221. var dropdown = document.getElementById("companyDropdown");
  222. var option = document.createElement('option');
  223. option.text = '-- pick a company to focus on.';
  224. option.value = 'none';
  225. dropdown.add(option);
  226. for (i = 0; i < nodeIds.length; i++) {
  227. option = document.createElement('option');
  228. option.text = option.value = nodeIds[i];
  229. dropdown.add(option);
  230. }
  231. }
  232. function populateTypeDiv() {
  233. var container = document.getElementById("typeContainer");
  234. recursiveClearDOM(container);
  235. var types = ['funding', 'connections'];
  236. var typeLabels = ['size by funding', 'size by connections'];
  237. for (i = 0; i < types.length; i++) {
  238. var div = document.createElement('div');
  239. div.className = 'type';
  240. if (types[i] == selectedType) {
  241. div.className += ' selected'
  242. }
  243. div.innerHTML = typeLabels[i];
  244. div.onclick = selectType.bind(this,types[i])
  245. container.appendChild(div);
  246. }
  247. }
  248. function populateYearDiv() {
  249. var container = document.getElementById("yearContainer");
  250. recursiveClearDOM(container);
  251. for (i = 0; i < years.length; i++) {
  252. var div = document.createElement('div');
  253. div.className = 'year';
  254. if (years[i] == selectedYear) {
  255. div.className += ' selected'
  256. }
  257. div.innerHTML = years[i];
  258. div.onclick = selectYear.bind(this,years[i])
  259. container.appendChild(div);
  260. }
  261. }
  262. function populateProjectsDropdown() {
  263. var dropdown = document.getElementById("projectsDropdown");
  264. var option = document.createElement('option');
  265. option.text = '-- pick project to see the connections';
  266. option.value = 'none';
  267. dropdown.add(option);
  268. for (i = 0; i < yearlyProjects[selectedYear].length; i++) {
  269. option = document.createElement('option');
  270. option.text = option.value = yearlyProjects[selectedYear][i];
  271. dropdown.add(option);
  272. }
  273. }
  274. function download() {
  275. var file = selectedFile.replace("./data/combined/","");
  276. var nodesAr = nodes.get();
  277. var edgesAr = edges.get();
  278. var obj = {nodes: nodesAr, edges: edgesAr};
  279. var json = JSON.stringify(obj);
  280. var blob = new Blob([json], {type: "text/plain;charset=utf-8"});
  281. saveAs(blob, file);
  282. }
  283. var filesList = [
  284. //'2010_FP7-ENERGY.json',
  285. //'2010_FP7-ENVIRONMENT.json',
  286. //'2010_FP7-EURATOM-FISSION.json',
  287. //'2010_FP7-HEALTH.json',
  288. //'2010_FP7-ICT.json',
  289. //'2010_FP7-IDEAS-ERC.json',
  290. //'2010_FP7-INCO.json',
  291. //'2010_FP7-INFRASTRUCTURES.json',
  292. //'2010_FP7-JTI.json',
  293. //'2010_FP7-KBBE.json',
  294. //'2010_FP7-NMP.json',
  295. //'2010_FP7-PEOPLE.json',
  296. //'2010_FP7-REGIONS.json',
  297. //'2010_FP7-REGPOT.json',
  298. //'2010_FP7-SIS.json',
  299. //'2010_FP7-SME.json',
  300. //'2010_FP7-SPACE.json',
  301. //'2010_FP7-SSH.json',
  302. //'2010_FP7-TRANSPORT.json',
  303. //'2010_Other.json',
  304. //'2011_FP7-COH.json',
  305. //'2011_FP7-ENERGY.json',
  306. //'2011_FP7-ENVIRONMENT.json',
  307. //'2011_FP7-EURATOM-FISSION.json',
  308. //'2011_FP7-GA.json',
  309. //'2011_FP7-HEALTH.json',
  310. //'2011_FP7-ICT,FP7-JTI.json',
  311. //'2011_FP7-ICT.json',
  312. //'2011_FP7-IDEAS-ERC.json',
  313. //'2011_FP7-INCO.json',
  314. //'2011_FP7-INFRASTRUCTURES.json',
  315. //'2011_FP7-JTI.json',
  316. //'2011_FP7-KBBE.json',
  317. //'2011_FP7-NMP,FP7-INFRASTRUCTURES.json',
  318. //'2011_FP7-NMP,FP7-TRANSPORT.json',
  319. //'2011_FP7-NMP.json',
  320. //'2011_FP7-PEOPLE.json',
  321. //'2011_FP7-REGIONS.json',
  322. //'2011_FP7-REGPOT.json',
  323. //'2011_FP7-SECURITY.json',
  324. //'2011_FP7-SIS.json',
  325. //'2011_FP7-SME.json',
  326. //'2011_FP7-SPACE.json',
  327. //'2011_FP7-SSH.json',
  328. //'2011_FP7-TRANSPORT.json',
  329. //'2012_CIP.json',
  330. //'2012_FP7-ENERGY.json',
  331. //'2012_FP7-ENVIRONMENT.json',
  332. //'2012_FP7-EURATOM-FISSION.json',
  333. //'2012_FP7-HEALTH.json',
  334. //'2012_FP7-ICT.json',
  335. //'2012_FP7-IDEAS-ERC.json',
  336. //'2012_FP7-INCO.json',
  337. //'2012_FP7-INFRASTRUCTURES.json',
  338. //'2012_FP7-JTI.json',
  339. //'2012_FP7-KBBE.json',
  340. //'2012_FP7-NMP.json',
  341. //'2012_FP7-PEOPLE.json',
  342. //'2012_FP7-REGIONS.json',
  343. //'2012_FP7-REGPOT.json',
  344. //'2012_FP7-SECURITY.json',
  345. //'2012_FP7-SIS.json',
  346. //'2012_FP7-SME.json',
  347. //'2012_FP7-SPACE.json',
  348. //'2012_FP7-SSH.json',
  349. //'2012_FP7-TRANSPORT.json',
  350. //'2012_Other.json',
  351. //'2013_CIP.json',
  352. //'2013_FP7-COH.json',
  353. //'2013_FP7-ENERGY.json',
  354. //'2013_FP7-ENVIRONMENT.json',
  355. '2013_FP7-EURATOM-FISSION.json',
  356. //'2013_FP7-HEALTH.json',
  357. '2013_FP7-ICT.json',
  358. //'2013_FP7-IDEAS-ERC.json',
  359. //'2013_FP7-INCO.json',
  360. //'2013_FP7-INFRASTRUCTURES,FP7-SME.json',
  361. //'2013_FP7-INFRASTRUCTURES.json',
  362. //'2013_FP7-JTI.json',
  363. //'2013_FP7-KBBE.json',
  364. //'2013_FP7-NMP.json',
  365. //'2013_FP7-PEOPLE.json',
  366. //'2013_FP7-REGIONS.json',
  367. //'2013_FP7-REGPOT.json',
  368. //'2013_FP7-SECURITY.json',
  369. //'2013_FP7-SIS.json',
  370. //'2013_FP7-SME.json',
  371. //'2013_FP7-SPACE.json',
  372. //'2013_FP7-SSH.json',
  373. //'2013_FP7-TRANSPORT.json',
  374. ]
  375. filesList.sort();
  376. var fileIndex = -1;
  377. function next() {
  378. if (network) {
  379. network.destroy()
  380. network = null;
  381. }
  382. fileIndex++;
  383. document.getElementById("FILENAME").innerHTML = filesList[fileIndex];
  384. loadJSON('./data/combined/' + filesList[fileIndex], drawAll)
  385. }
  386. var years = [];
  387. var yearlyProjects = {};
  388. var selectedFile = "";
  389. for (var i = 0; i < filesList.length; i++) {
  390. var filename = filesList[i];
  391. var filenameArray = filename.split("_");
  392. var year = filenameArray[0];
  393. var project = filenameArray[1].replace(".json", "");
  394. if (years.indexOf(year) == -1) {
  395. years.push(year);
  396. yearlyProjects[year] = [];
  397. }
  398. yearlyProjects[year].push(project);
  399. }
  400. var selectedYear = years[years.length-1];
  401. var selectedType = "connections";
  402. var selectedProject = "";
  403. populateYearDiv();
  404. populateTypeDiv();
  405. populateProjectsDropdown();
  406. //next()