gexf.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. var zrUtil = require("zrender/lib/core/util");
  2. // GEXF File Parser
  3. // http://gexf.net/1.2draft/gexf-12draft-primer.pdf
  4. function parse(xml) {
  5. var doc;
  6. if (typeof xml === 'string') {
  7. var parser = new DOMParser();
  8. doc = parser.parseFromString(xml, 'text/xml');
  9. } else {
  10. doc = xml;
  11. }
  12. if (!doc || doc.getElementsByTagName('parsererror').length) {
  13. return null;
  14. }
  15. var gexfRoot = getChildByTagName(doc, 'gexf');
  16. if (!gexfRoot) {
  17. return null;
  18. }
  19. var graphRoot = getChildByTagName(gexfRoot, 'graph');
  20. var attributes = parseAttributes(getChildByTagName(graphRoot, 'attributes'));
  21. var attributesMap = {};
  22. for (var i = 0; i < attributes.length; i++) {
  23. attributesMap[attributes[i].id] = attributes[i];
  24. }
  25. return {
  26. nodes: parseNodes(getChildByTagName(graphRoot, 'nodes'), attributesMap),
  27. links: parseEdges(getChildByTagName(graphRoot, 'edges'))
  28. };
  29. }
  30. function parseAttributes(parent) {
  31. return parent ? zrUtil.map(getChildrenByTagName(parent, 'attribute'), function (attribDom) {
  32. return {
  33. id: getAttr(attribDom, 'id'),
  34. title: getAttr(attribDom, 'title'),
  35. type: getAttr(attribDom, 'type')
  36. };
  37. }) : [];
  38. }
  39. function parseNodes(parent, attributesMap) {
  40. return parent ? zrUtil.map(getChildrenByTagName(parent, 'node'), function (nodeDom) {
  41. var id = getAttr(nodeDom, 'id');
  42. var label = getAttr(nodeDom, 'label');
  43. var node = {
  44. id: id,
  45. name: label,
  46. itemStyle: {
  47. normal: {}
  48. }
  49. };
  50. var vizSizeDom = getChildByTagName(nodeDom, 'viz:size');
  51. var vizPosDom = getChildByTagName(nodeDom, 'viz:position');
  52. var vizColorDom = getChildByTagName(nodeDom, 'viz:color'); // var vizShapeDom = getChildByTagName(nodeDom, 'viz:shape');
  53. var attvaluesDom = getChildByTagName(nodeDom, 'attvalues');
  54. if (vizSizeDom) {
  55. node.symbolSize = parseFloat(getAttr(vizSizeDom, 'value'));
  56. }
  57. if (vizPosDom) {
  58. node.x = parseFloat(getAttr(vizPosDom, 'x'));
  59. node.y = parseFloat(getAttr(vizPosDom, 'y')); // z
  60. }
  61. if (vizColorDom) {
  62. node.itemStyle.normal.color = 'rgb(' + [getAttr(vizColorDom, 'r') | 0, getAttr(vizColorDom, 'g') | 0, getAttr(vizColorDom, 'b') | 0].join(',') + ')';
  63. } // if (vizShapeDom) {
  64. // node.shape = getAttr(vizShapeDom, 'shape');
  65. // }
  66. if (attvaluesDom) {
  67. var attvalueDomList = getChildrenByTagName(attvaluesDom, 'attvalue');
  68. node.attributes = {};
  69. for (var j = 0; j < attvalueDomList.length; j++) {
  70. var attvalueDom = attvalueDomList[j];
  71. var attId = getAttr(attvalueDom, 'for');
  72. var attValue = getAttr(attvalueDom, 'value');
  73. var attribute = attributesMap[attId];
  74. if (attribute) {
  75. switch (attribute.type) {
  76. case 'integer':
  77. case 'long':
  78. attValue = parseInt(attValue, 10);
  79. break;
  80. case 'float':
  81. case 'double':
  82. attValue = parseFloat(attValue);
  83. break;
  84. case 'boolean':
  85. attValue = attValue.toLowerCase() == 'true';
  86. break;
  87. default:
  88. }
  89. node.attributes[attId] = attValue;
  90. }
  91. }
  92. }
  93. return node;
  94. }) : [];
  95. }
  96. function parseEdges(parent) {
  97. return parent ? zrUtil.map(getChildrenByTagName(parent, 'edge'), function (edgeDom) {
  98. var id = getAttr(edgeDom, 'id');
  99. var label = getAttr(edgeDom, 'label');
  100. var sourceId = getAttr(edgeDom, 'source');
  101. var targetId = getAttr(edgeDom, 'target');
  102. var edge = {
  103. id: id,
  104. name: label,
  105. source: sourceId,
  106. target: targetId,
  107. lineStyle: {
  108. normal: {}
  109. }
  110. };
  111. var lineStyle = edge.lineStyle.normal;
  112. var vizThicknessDom = getChildByTagName(edgeDom, 'viz:thickness');
  113. var vizColorDom = getChildByTagName(edgeDom, 'viz:color'); // var vizShapeDom = getChildByTagName(edgeDom, 'viz:shape');
  114. if (vizThicknessDom) {
  115. lineStyle.width = parseFloat(vizThicknessDom.getAttribute('value'));
  116. }
  117. if (vizColorDom) {
  118. lineStyle.color = 'rgb(' + [getAttr(vizColorDom, 'r') | 0, getAttr(vizColorDom, 'g') | 0, getAttr(vizColorDom, 'b') | 0].join(',') + ')';
  119. } // if (vizShapeDom) {
  120. // edge.shape = vizShapeDom.getAttribute('shape');
  121. // }
  122. return edge;
  123. }) : [];
  124. }
  125. function getAttr(el, attrName) {
  126. return el.getAttribute(attrName);
  127. }
  128. function getChildByTagName(parent, tagName) {
  129. var node = parent.firstChild;
  130. while (node) {
  131. if (node.nodeType != 1 || node.nodeName.toLowerCase() != tagName.toLowerCase()) {
  132. node = node.nextSibling;
  133. } else {
  134. return node;
  135. }
  136. }
  137. return null;
  138. }
  139. function getChildrenByTagName(parent, tagName) {
  140. var node = parent.firstChild;
  141. var children = [];
  142. while (node) {
  143. if (node.nodeName.toLowerCase() == tagName.toLowerCase()) {
  144. children.push(node);
  145. }
  146. node = node.nextSibling;
  147. }
  148. return children;
  149. }
  150. exports.parse = parse;