Graph database Analysis of the Steam Network
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.

112 lines
3.3 KiB

  1. ;(function(undefined) {
  2. 'use strict';
  3. if (typeof sigma === 'undefined')
  4. throw 'sigma is not declared';
  5. // Initialize package:
  6. sigma.utils.pkg('sigma.parsers');
  7. // Just a basic ID generator:
  8. var _id = 0;
  9. function edgeId() {
  10. return 'e' + (_id++);
  11. }
  12. /**
  13. * If the first arguments is a valid URL, this function loads a GEXF file and
  14. * creates a new sigma instance or updates the graph of a given instance. It
  15. * is possible to give a callback that will be executed at the end of the
  16. * process. And if the first argument is a DOM element, it will skip the
  17. * loading step and parse the given XML tree to fill the graph.
  18. *
  19. * @param {string|DOMElement} target The URL of the GEXF file or a valid
  20. * GEXF tree.
  21. * @param {object|sigma} sig A sigma configuration object or a
  22. * sigma instance.
  23. * @param {?function} callback Eventually a callback to execute
  24. * after having parsed the file. It will
  25. * be called with the related sigma
  26. * instance as parameter.
  27. */
  28. sigma.parsers.gexf = function(target, sig, callback) {
  29. var i,
  30. l,
  31. arr,
  32. obj;
  33. function parse(graph) {
  34. // Adapt the graph:
  35. arr = graph.nodes;
  36. for (i = 0, l = arr.length; i < l; i++) {
  37. obj = arr[i];
  38. obj.id = obj.id;
  39. if (obj.viz && typeof obj.viz === 'object') {
  40. if (obj.viz.position && typeof obj.viz.position === 'object') {
  41. obj.x = obj.viz.position.x;
  42. obj.y = -obj.viz.position.y; // Needed otherwise it's up side down
  43. }
  44. obj.size = obj.viz.size;
  45. obj.color = obj.viz.color;
  46. }
  47. }
  48. arr = graph.edges;
  49. for (i = 0, l = arr.length; i < l; i++) {
  50. obj = arr[i];
  51. obj.id = typeof obj.id === 'string' ? obj.id : edgeId();
  52. obj.source = '' + obj.source;
  53. obj.target = '' + obj.target;
  54. if (obj.viz && typeof obj.viz === 'object') {
  55. obj.color = obj.viz.color;
  56. obj.size = obj.viz.thickness;
  57. }
  58. // Weight over viz.thickness?
  59. obj.size = obj.weight;
  60. // Changing type to be direction so it won't mess with sigma's naming
  61. obj.direction = obj.type;
  62. delete obj.type;
  63. }
  64. // Update the instance's graph:
  65. if (sig instanceof sigma) {
  66. sig.graph.clear();
  67. arr = graph.nodes;
  68. for (i = 0, l = arr.length; i < l; i++)
  69. sig.graph.addNode(arr[i]);
  70. arr = graph.edges;
  71. for (i = 0, l = arr.length; i < l; i++)
  72. sig.graph.addEdge(arr[i]);
  73. // ...or instantiate sigma if needed:
  74. } else if (typeof sig === 'object') {
  75. sig.graph = graph;
  76. sig = new sigma(sig);
  77. // ...or it's finally the callback:
  78. } else if (typeof sig === 'function') {
  79. callback = sig;
  80. sig = null;
  81. }
  82. // Call the callback if specified:
  83. if (callback) {
  84. callback(sig || graph);
  85. return;
  86. } else
  87. return graph;
  88. }
  89. if (typeof target === 'string')
  90. gexf.fetch(target, parse);
  91. else if (typeof target === 'object')
  92. return parse(gexf.parse(target));
  93. };
  94. }).call(this);