gexf.js 5.7 KB

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