dataTool.js 14 KB

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