gexf.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. var zrUtil = require("zrender/lib/core/util");
  20. /*
  21. * Licensed to the Apache Software Foundation (ASF) under one
  22. * or more contributor license agreements. See the NOTICE file
  23. * distributed with this work for additional information
  24. * regarding copyright ownership. The ASF licenses this file
  25. * to you under the Apache License, Version 2.0 (the
  26. * "License"); you may not use this file except in compliance
  27. * with the License. You may obtain a copy of the License at
  28. *
  29. * http://www.apache.org/licenses/LICENSE-2.0
  30. *
  31. * Unless required by applicable law or agreed to in writing,
  32. * software distributed under the License is distributed on an
  33. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  34. * KIND, either express or implied. See the License for the
  35. * specific language governing permissions and limitations
  36. * under the License.
  37. */
  38. /**
  39. * This is a parse of GEXF.
  40. *
  41. * The spec of GEXF:
  42. * https://gephi.org/gexf/1.2draft/gexf-12draft-primer.pdf
  43. */
  44. function parse(xml) {
  45. var doc;
  46. if (typeof xml === 'string') {
  47. var parser = new DOMParser();
  48. doc = parser.parseFromString(xml, 'text/xml');
  49. } else {
  50. doc = xml;
  51. }
  52. if (!doc || doc.getElementsByTagName('parsererror').length) {
  53. return null;
  54. }
  55. var gexfRoot = getChildByTagName(doc, 'gexf');
  56. if (!gexfRoot) {
  57. return null;
  58. }
  59. var graphRoot = getChildByTagName(gexfRoot, 'graph');
  60. var attributes = parseAttributes(getChildByTagName(graphRoot, 'attributes'));
  61. var attributesMap = {};
  62. for (var i = 0; i < attributes.length; i++) {
  63. attributesMap[attributes[i].id] = attributes[i];
  64. }
  65. return {
  66. nodes: parseNodes(getChildByTagName(graphRoot, 'nodes'), attributesMap),
  67. links: parseEdges(getChildByTagName(graphRoot, 'edges'))
  68. };
  69. }
  70. function parseAttributes(parent) {
  71. return parent ? zrUtil.map(getChildrenByTagName(parent, 'attribute'), function (attribDom) {
  72. return {
  73. id: getAttr(attribDom, 'id'),
  74. title: getAttr(attribDom, 'title'),
  75. type: getAttr(attribDom, 'type')
  76. };
  77. }) : [];
  78. }
  79. function parseNodes(parent, attributesMap) {
  80. return parent ? zrUtil.map(getChildrenByTagName(parent, 'node'), function (nodeDom) {
  81. var id = getAttr(nodeDom, 'id');
  82. var label = getAttr(nodeDom, 'label');
  83. var node = {
  84. id: id,
  85. name: label,
  86. itemStyle: {
  87. normal: {}
  88. }
  89. };
  90. var vizSizeDom = getChildByTagName(nodeDom, 'viz:size');
  91. var vizPosDom = getChildByTagName(nodeDom, 'viz:position');
  92. var vizColorDom = getChildByTagName(nodeDom, 'viz:color'); // var vizShapeDom = getChildByTagName(nodeDom, 'viz:shape');
  93. var attvaluesDom = getChildByTagName(nodeDom, 'attvalues');
  94. if (vizSizeDom) {
  95. node.symbolSize = parseFloat(getAttr(vizSizeDom, 'value'));
  96. }
  97. if (vizPosDom) {
  98. node.x = parseFloat(getAttr(vizPosDom, 'x'));
  99. node.y = parseFloat(getAttr(vizPosDom, 'y')); // z
  100. }
  101. if (vizColorDom) {
  102. node.itemStyle.normal.color = 'rgb(' + [getAttr(vizColorDom, 'r') | 0, getAttr(vizColorDom, 'g') | 0, getAttr(vizColorDom, 'b') | 0].join(',') + ')';
  103. } // if (vizShapeDom) {
  104. // node.shape = getAttr(vizShapeDom, 'shape');
  105. // }
  106. if (attvaluesDom) {
  107. var attvalueDomList = getChildrenByTagName(attvaluesDom, 'attvalue');
  108. node.attributes = {};
  109. for (var j = 0; j < attvalueDomList.length; j++) {
  110. var attvalueDom = attvalueDomList[j];
  111. var attId = getAttr(attvalueDom, 'for');
  112. var attValue = getAttr(attvalueDom, 'value');
  113. var attribute = attributesMap[attId];
  114. if (attribute) {
  115. switch (attribute.type) {
  116. case 'integer':
  117. case 'long':
  118. attValue = parseInt(attValue, 10);
  119. break;
  120. case 'float':
  121. case 'double':
  122. attValue = parseFloat(attValue);
  123. break;
  124. case 'boolean':
  125. attValue = attValue.toLowerCase() === 'true';
  126. break;
  127. default:
  128. }
  129. node.attributes[attId] = attValue;
  130. }
  131. }
  132. }
  133. return node;
  134. }) : [];
  135. }
  136. function parseEdges(parent) {
  137. return parent ? zrUtil.map(getChildrenByTagName(parent, 'edge'), function (edgeDom) {
  138. var id = getAttr(edgeDom, 'id');
  139. var label = getAttr(edgeDom, 'label');
  140. var sourceId = getAttr(edgeDom, 'source');
  141. var targetId = getAttr(edgeDom, 'target');
  142. var edge = {
  143. id: id,
  144. name: label,
  145. source: sourceId,
  146. target: targetId,
  147. lineStyle: {
  148. normal: {}
  149. }
  150. };
  151. var lineStyle = edge.lineStyle.normal;
  152. var vizThicknessDom = getChildByTagName(edgeDom, 'viz:thickness');
  153. var vizColorDom = getChildByTagName(edgeDom, 'viz:color'); // var vizShapeDom = getChildByTagName(edgeDom, 'viz:shape');
  154. if (vizThicknessDom) {
  155. lineStyle.width = parseFloat(vizThicknessDom.getAttribute('value'));
  156. }
  157. if (vizColorDom) {
  158. lineStyle.color = 'rgb(' + [getAttr(vizColorDom, 'r') | 0, getAttr(vizColorDom, 'g') | 0, getAttr(vizColorDom, 'b') | 0].join(',') + ')';
  159. } // if (vizShapeDom) {
  160. // edge.shape = vizShapeDom.getAttribute('shape');
  161. // }
  162. return edge;
  163. }) : [];
  164. }
  165. function getAttr(el, attrName) {
  166. return el.getAttribute(attrName);
  167. }
  168. function getChildByTagName(parent, tagName) {
  169. var node = parent.firstChild;
  170. while (node) {
  171. if (node.nodeType !== 1 || node.nodeName.toLowerCase() !== tagName.toLowerCase()) {
  172. node = node.nextSibling;
  173. } else {
  174. return node;
  175. }
  176. }
  177. return null;
  178. }
  179. function getChildrenByTagName(parent, tagName) {
  180. var node = parent.firstChild;
  181. var children = [];
  182. while (node) {
  183. if (node.nodeName.toLowerCase() === tagName.toLowerCase()) {
  184. children.push(node);
  185. }
  186. node = node.nextSibling;
  187. }
  188. return children;
  189. }
  190. exports.parse = parse;