dataTool.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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. (function (global, factory) {
  20. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('echarts')) :
  21. typeof define === 'function' && define.amd ? define(['exports', 'echarts'], factory) :
  22. (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.dataTool = {}, global.echarts));
  23. }(this, (function (exports, echarts) { 'use strict';
  24. var arrayProto = Array.prototype;
  25. var nativeSlice = arrayProto.slice;
  26. var nativeMap = arrayProto.map;
  27. var ctorFunction = function () { }.constructor;
  28. var protoFunction = ctorFunction ? ctorFunction.prototype : null;
  29. function map(arr, cb, context) {
  30. if (!arr) {
  31. return [];
  32. }
  33. if (!cb) {
  34. return slice(arr);
  35. }
  36. if (arr.map && arr.map === nativeMap) {
  37. return arr.map(cb, context);
  38. }
  39. else {
  40. var result = [];
  41. for (var i = 0, len = arr.length; i < len; i++) {
  42. result.push(cb.call(context, arr[i], i, arr));
  43. }
  44. return result;
  45. }
  46. }
  47. function bindPolyfill(func, context) {
  48. var args = [];
  49. for (var _i = 2; _i < arguments.length; _i++) {
  50. args[_i - 2] = arguments[_i];
  51. }
  52. return function () {
  53. return func.apply(context, args.concat(nativeSlice.call(arguments)));
  54. };
  55. }
  56. var bind = (protoFunction && isFunction(protoFunction.bind))
  57. ? protoFunction.call.bind(protoFunction.bind)
  58. : bindPolyfill;
  59. function isFunction(value) {
  60. return typeof value === 'function';
  61. }
  62. function slice(arr) {
  63. var args = [];
  64. for (var _i = 1; _i < arguments.length; _i++) {
  65. args[_i - 1] = arguments[_i];
  66. }
  67. return nativeSlice.apply(arr, args);
  68. }
  69. function parse(xml) {
  70. var doc;
  71. if (typeof xml === 'string') {
  72. var parser = new DOMParser();
  73. doc = parser.parseFromString(xml, 'text/xml');
  74. } else {
  75. doc = xml;
  76. }
  77. if (!doc || doc.getElementsByTagName('parsererror').length) {
  78. return null;
  79. }
  80. var gexfRoot = getChildByTagName(doc, 'gexf');
  81. if (!gexfRoot) {
  82. return null;
  83. }
  84. var graphRoot = getChildByTagName(gexfRoot, 'graph');
  85. var attributes = parseAttributes(getChildByTagName(graphRoot, 'attributes'));
  86. var attributesMap = {};
  87. for (var i = 0; i < attributes.length; i++) {
  88. attributesMap[attributes[i].id] = attributes[i];
  89. }
  90. return {
  91. nodes: parseNodes(getChildByTagName(graphRoot, 'nodes'), attributesMap),
  92. links: parseEdges(getChildByTagName(graphRoot, 'edges'))
  93. };
  94. }
  95. function parseAttributes(parent) {
  96. return parent ? map(getChildrenByTagName(parent, 'attribute'), function (attribDom) {
  97. return {
  98. id: getAttr(attribDom, 'id'),
  99. title: getAttr(attribDom, 'title'),
  100. type: getAttr(attribDom, 'type')
  101. };
  102. }) : [];
  103. }
  104. function parseNodes(parent, attributesMap) {
  105. return parent ? map(getChildrenByTagName(parent, 'node'), function (nodeDom) {
  106. var id = getAttr(nodeDom, 'id');
  107. var label = getAttr(nodeDom, 'label');
  108. var node = {
  109. id: id,
  110. name: label,
  111. itemStyle: {
  112. normal: {}
  113. }
  114. };
  115. var vizSizeDom = getChildByTagName(nodeDom, 'viz:size');
  116. var vizPosDom = getChildByTagName(nodeDom, 'viz:position');
  117. var vizColorDom = getChildByTagName(nodeDom, 'viz:color'); // let vizShapeDom = getChildByTagName(nodeDom, 'viz:shape');
  118. var attvaluesDom = getChildByTagName(nodeDom, 'attvalues');
  119. if (vizSizeDom) {
  120. node.symbolSize = parseFloat(getAttr(vizSizeDom, 'value'));
  121. }
  122. if (vizPosDom) {
  123. node.x = parseFloat(getAttr(vizPosDom, 'x'));
  124. node.y = parseFloat(getAttr(vizPosDom, 'y')); // z
  125. }
  126. if (vizColorDom) {
  127. node.itemStyle.normal.color = 'rgb(' + [getAttr(vizColorDom, 'r') | 0, getAttr(vizColorDom, 'g') | 0, getAttr(vizColorDom, 'b') | 0].join(',') + ')';
  128. } // if (vizShapeDom) {
  129. // node.shape = getAttr(vizShapeDom, 'shape');
  130. // }
  131. if (attvaluesDom) {
  132. var attvalueDomList = getChildrenByTagName(attvaluesDom, 'attvalue');
  133. node.attributes = {};
  134. for (var j = 0; j < attvalueDomList.length; j++) {
  135. var attvalueDom = attvalueDomList[j];
  136. var attId = getAttr(attvalueDom, 'for');
  137. var attValue = getAttr(attvalueDom, 'value');
  138. var attribute = attributesMap[attId];
  139. if (attribute) {
  140. switch (attribute.type) {
  141. case 'integer':
  142. case 'long':
  143. attValue = parseInt(attValue, 10);
  144. break;
  145. case 'float':
  146. case 'double':
  147. attValue = parseFloat(attValue);
  148. break;
  149. case 'boolean':
  150. attValue = attValue.toLowerCase() === 'true';
  151. break;
  152. }
  153. node.attributes[attId] = attValue;
  154. }
  155. }
  156. }
  157. return node;
  158. }) : [];
  159. }
  160. function parseEdges(parent) {
  161. return parent ? map(getChildrenByTagName(parent, 'edge'), function (edgeDom) {
  162. var id = getAttr(edgeDom, 'id');
  163. var label = getAttr(edgeDom, 'label');
  164. var sourceId = getAttr(edgeDom, 'source');
  165. var targetId = getAttr(edgeDom, 'target');
  166. var edge = {
  167. id: id,
  168. name: label,
  169. source: sourceId,
  170. target: targetId,
  171. lineStyle: {
  172. normal: {}
  173. }
  174. };
  175. var lineStyle = edge.lineStyle.normal;
  176. var vizThicknessDom = getChildByTagName(edgeDom, 'viz:thickness');
  177. var vizColorDom = getChildByTagName(edgeDom, 'viz:color'); // let vizShapeDom = getChildByTagName(edgeDom, 'viz:shape');
  178. if (vizThicknessDom) {
  179. lineStyle.width = parseFloat(vizThicknessDom.getAttribute('value'));
  180. }
  181. if (vizColorDom) {
  182. lineStyle.color = 'rgb(' + [getAttr(vizColorDom, 'r') | 0, getAttr(vizColorDom, 'g') | 0, getAttr(vizColorDom, 'b') | 0].join(',') + ')';
  183. } // if (vizShapeDom) {
  184. // edge.shape = vizShapeDom.getAttribute('shape');
  185. // }
  186. return edge;
  187. }) : [];
  188. }
  189. function getAttr(el, attrName) {
  190. return el.getAttribute(attrName);
  191. }
  192. function getChildByTagName(parent, tagName) {
  193. var node = parent.firstChild;
  194. while (node) {
  195. if (node.nodeType !== 1 || node.nodeName.toLowerCase() !== tagName.toLowerCase()) {
  196. node = node.nextSibling;
  197. } else {
  198. return node;
  199. }
  200. }
  201. return null;
  202. }
  203. function getChildrenByTagName(parent, tagName) {
  204. var node = parent.firstChild;
  205. var children = [];
  206. while (node) {
  207. if (node.nodeName.toLowerCase() === tagName.toLowerCase()) {
  208. children.push(node);
  209. }
  210. node = node.nextSibling;
  211. }
  212. return children;
  213. }
  214. var gexf = /*#__PURE__*/Object.freeze({
  215. __proto__: null,
  216. parse: parse
  217. });
  218. /*
  219. * Licensed to the Apache Software Foundation (ASF) under one
  220. * or more contributor license agreements. See the NOTICE file
  221. * distributed with this work for additional information
  222. * regarding copyright ownership. The ASF licenses this file
  223. * to you under the Apache License, Version 2.0 (the
  224. * "License"); you may not use this file except in compliance
  225. * with the License. You may obtain a copy of the License at
  226. *
  227. * http://www.apache.org/licenses/LICENSE-2.0
  228. *
  229. * Unless required by applicable law or agreed to in writing,
  230. * software distributed under the License is distributed on an
  231. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  232. * KIND, either express or implied. See the License for the
  233. * specific language governing permissions and limitations
  234. * under the License.
  235. */
  236. /**
  237. * AUTO-GENERATED FILE. DO NOT MODIFY.
  238. */
  239. /*
  240. * Licensed to the Apache Software Foundation (ASF) under one
  241. * or more contributor license agreements. See the NOTICE file
  242. * distributed with this work for additional information
  243. * regarding copyright ownership. The ASF licenses this file
  244. * to you under the Apache License, Version 2.0 (the
  245. * "License"); you may not use this file except in compliance
  246. * with the License. You may obtain a copy of the License at
  247. *
  248. * http://www.apache.org/licenses/LICENSE-2.0
  249. *
  250. * Unless required by applicable law or agreed to in writing,
  251. * software distributed under the License is distributed on an
  252. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  253. * KIND, either express or implied. See the License for the
  254. * specific language governing permissions and limitations
  255. * under the License.
  256. */
  257. function asc(arr) {
  258. arr.sort(function (a, b) {
  259. return a - b;
  260. });
  261. return arr;
  262. }
  263. function quantile(ascArr, p) {
  264. var H = (ascArr.length - 1) * p + 1;
  265. var h = Math.floor(H);
  266. var v = +ascArr[h - 1];
  267. var e = H - h;
  268. return e ? v + e * (ascArr[h] - v) : v;
  269. }
  270. /**
  271. * See:
  272. * <https://en.wikipedia.org/wiki/Box_plot#cite_note-frigge_hoaglin_iglewicz-2>
  273. * <http://stat.ethz.ch/R-manual/R-devel/library/grDevices/html/boxplot.stats.html>
  274. *
  275. * Helper method for preparing data.
  276. *
  277. * @param {Array.<number>} rawData like
  278. * [
  279. * [12,232,443], (raw data set for the first box)
  280. * [3843,5545,1232], (raw data set for the second box)
  281. * ...
  282. * ]
  283. * @param {Object} [opt]
  284. *
  285. * @param {(number|string)} [opt.boundIQR=1.5] Data less than min bound is outlier.
  286. * default 1.5, means Q1 - 1.5 * (Q3 - Q1).
  287. * If 'none'/0 passed, min bound will not be used.
  288. * @param {(number|string)} [opt.layout='horizontal']
  289. * Box plot layout, can be 'horizontal' or 'vertical'
  290. * @return {Object} {
  291. * boxData: Array.<Array.<number>>
  292. * outliers: Array.<Array.<number>>
  293. * axisData: Array.<string>
  294. * }
  295. */
  296. function prepareBoxplotData (rawData, opt) {
  297. opt = opt || {};
  298. var boxData = [];
  299. var outliers = [];
  300. var axisData = [];
  301. var boundIQR = opt.boundIQR;
  302. var useExtreme = boundIQR === 'none' || boundIQR === 0;
  303. for (var i = 0; i < rawData.length; i++) {
  304. axisData.push(i + '');
  305. var ascList = asc(rawData[i].slice());
  306. var Q1 = quantile(ascList, 0.25);
  307. var Q2 = quantile(ascList, 0.5);
  308. var Q3 = quantile(ascList, 0.75);
  309. var min = ascList[0];
  310. var max = ascList[ascList.length - 1];
  311. var bound = (boundIQR == null ? 1.5 : boundIQR) * (Q3 - Q1);
  312. var low = useExtreme ? min : Math.max(min, Q1 - bound);
  313. var high = useExtreme ? max : Math.min(max, Q3 + bound);
  314. boxData.push([low, Q1, Q2, Q3, high]);
  315. for (var j = 0; j < ascList.length; j++) {
  316. var dataItem = ascList[j];
  317. if (dataItem < low || dataItem > high) {
  318. var outlier = [i, dataItem];
  319. opt.layout === 'vertical' && outlier.reverse();
  320. outliers.push(outlier);
  321. }
  322. }
  323. }
  324. return {
  325. boxData: boxData,
  326. outliers: outliers,
  327. axisData: axisData
  328. };
  329. }
  330. var version = '1.0.0';
  331. // For backward compatibility, where the namespace `dataTool` will
  332. // be mounted on `echarts` is the extension `dataTool` is imported.
  333. // But the old version of echarts do not have `dataTool` namespace,
  334. // so check it before mounting.
  335. if (echarts.dataTool) {
  336. echarts.dataTool.version = version;
  337. echarts.dataTool.gexf = gexf;
  338. echarts.dataTool.prepareBoxplotData = prepareBoxplotData; // echarts.dataTool.boxplotTransform = boxplotTransform;
  339. }
  340. exports.gexf = gexf;
  341. exports.prepareBoxplotData = prepareBoxplotData;
  342. exports.version = version;
  343. Object.defineProperty(exports, '__esModule', { value: true });
  344. })));
  345. //# sourceMappingURL=dataTool.js.map