gexf.js 6.5 KB

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