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.

71 lines
2.3 KiB

  1. /**
  2. *
  3. * @param {json} gephiJSON
  4. * @param {obj} optionsObj
  5. * @returns {{nodes: Array, edges: Array}}
  6. */
  7. function parseGephi(gephiJSON, optionsObj) {
  8. var edges = [];
  9. var nodes = [];
  10. var options = {
  11. edges: {
  12. inheritColor: false
  13. },
  14. nodes: {
  15. fixed: false,
  16. parseColor: false
  17. }
  18. };
  19. if (optionsObj !== undefined) {
  20. if (optionsObj.fixed !== undefined) {options.nodes.fixed = optionsObj.fixed}
  21. if (optionsObj.parseColor !== undefined) {options.nodes.parseColor = optionsObj.parseColor}
  22. if (optionsObj.inheritColor !== undefined) {options.edges.inheritColor = optionsObj.inheritColor}
  23. }
  24. var gEdges = gephiJSON.edges;
  25. var gNodes = gephiJSON.nodes;
  26. for (var i = 0; i < gEdges.length; i++) {
  27. var edge = {};
  28. var gEdge = gEdges[i];
  29. edge['id'] = gEdge.id;
  30. edge['from'] = gEdge.source;
  31. edge['to'] = gEdge.target;
  32. edge['attributes'] = gEdge.attributes;
  33. edge['label'] = gEdge.label;
  34. edge['title'] = gEdge.attributes !== undefined ? gEdge.attributes.title : undefined;
  35. if (gEdge['type'] === 'Directed') {
  36. edge['arrows'] = 'to';
  37. }
  38. // edge['value'] = gEdge.attributes !== undefined ? gEdge.attributes.Weight : undefined;
  39. // edge['width'] = edge['value'] !== undefined ? undefined : edgegEdge.size;
  40. if (gEdge.color && options.inheritColor === false) {
  41. edge['color'] = gEdge.color;
  42. }
  43. edges.push(edge);
  44. }
  45. for (var j = 0; j < gNodes.length; j++) {
  46. var node = {};
  47. var gNode = gNodes[j];
  48. node['id'] = gNode.id;
  49. node['attributes'] = gNode.attributes;
  50. node['x'] = gNode.x;
  51. node['y'] = gNode.y;
  52. node['label'] = gNode.label;
  53. node['title'] = gNode.attributes !== undefined ? gNode.attributes.title : gNode.title;
  54. if (options.nodes.parseColor === true) {
  55. node['color'] = gNode.color;
  56. }
  57. else {
  58. node['color'] = gNode.color !== undefined ? {background:gNode.color, border:gNode.color, highlight: {background:gNode.color, border:gNode.color}, hover:{background:gNode.color, border:gNode.color}} : undefined;
  59. }
  60. node['size'] = gNode.size;
  61. node['fixed'] = options.nodes.fixed && gNode.x !== undefined && gNode.y !== undefined;
  62. nodes.push(node);
  63. }
  64. return {nodes:nodes, edges:edges};
  65. }
  66. exports.parseGephi = parseGephi;