gexf.js 6.4 KB

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