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.

60 lines
1.8 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 (options !== undefined) {
  14. options.nodes['fixed'] = optionsObj.fixed !== undefined ? options.fixed : true;
  15. options.nodes['parseColor'] = optionsObj.parseColor !== undefined ? options.parseColor : false;
  16. options.edges['inheritColor'] = optionsObj.inheritColor !== undefined ? options.inheritColor : false;
  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['value'] = gEdge.attributes !== undefined ? gEdge.attributes.Weight : undefined;
  28. // edge['width'] = edge['value'] !== undefined ? undefined : edgegEdge.size;
  29. if (gEdge.color && options.inheritColor === false) {
  30. edge['color'] = gEdge.color;
  31. }
  32. edges.push(edge);
  33. }
  34. for (var i = 0; i < gNodes.length; i++) {
  35. var node = {};
  36. var gNode = gNodes[i];
  37. node['id'] = gNode.id;
  38. node['attributes'] = gNode.attributes;
  39. node['title'] = gNode.title;
  40. node['x'] = gNode.x;
  41. node['y'] = gNode.y;
  42. node['label'] = gNode.label;
  43. if (options.nodes.parseColor === true) {
  44. node['color'] = gNode.color;
  45. }
  46. else {
  47. node['color'] = gNode.color !== undefined ? {background:gNode.color, border:gNode.color} : undefined;
  48. }
  49. node['size'] = gNode.size;
  50. node['fixed'] = options.nodes.fixed && gNode.x !== undefined && gNode.y !== undefined;
  51. nodes.push(node);
  52. }
  53. return {nodes:nodes, edges:edges};
  54. }
  55. exports.parseGephi = parseGephi;