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.

66 lines
2.1 KiB

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