dataTool.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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');
  154. // let vizShapeDom = getChildByTagName(nodeDom, 'viz:shape');
  155. var attvaluesDom = getChildByTagName(nodeDom, 'attvalues');
  156. if (vizSizeDom) {
  157. node.symbolSize = parseFloat(getAttr(vizSizeDom, 'value'));
  158. }
  159. if (vizPosDom) {
  160. node.x = parseFloat(getAttr(vizPosDom, 'x'));
  161. node.y = parseFloat(getAttr(vizPosDom, 'y'));
  162. // z
  163. }
  164. if (vizColorDom) {
  165. node.itemStyle.normal.color = 'rgb(' + [getAttr(vizColorDom, 'r') | 0, getAttr(vizColorDom, 'g') | 0, getAttr(vizColorDom, 'b') | 0].join(',') + ')';
  166. }
  167. // if (vizShapeDom) {
  168. // node.shape = getAttr(vizShapeDom, 'shape');
  169. // }
  170. if (attvaluesDom) {
  171. var attvalueDomList = getChildrenByTagName(attvaluesDom, 'attvalue');
  172. node.attributes = {};
  173. for (var j = 0; j < attvalueDomList.length; j++) {
  174. var attvalueDom = attvalueDomList[j];
  175. var attId = getAttr(attvalueDom, 'for');
  176. var attValue = getAttr(attvalueDom, 'value');
  177. var attribute = attributesMap[attId];
  178. if (attribute) {
  179. switch (attribute.type) {
  180. case 'integer':
  181. case 'long':
  182. attValue = parseInt(attValue, 10);
  183. break;
  184. case 'float':
  185. case 'double':
  186. attValue = parseFloat(attValue);
  187. break;
  188. case 'boolean':
  189. attValue = attValue.toLowerCase() === 'true';
  190. break;
  191. }
  192. node.attributes[attId] = attValue;
  193. }
  194. }
  195. }
  196. return node;
  197. }) : [];
  198. }
  199. function parseEdges(parent) {
  200. return parent ? map(getChildrenByTagName(parent, 'edge'), function (edgeDom) {
  201. var id = getAttr(edgeDom, 'id');
  202. var label = getAttr(edgeDom, 'label');
  203. var sourceId = getAttr(edgeDom, 'source');
  204. var targetId = getAttr(edgeDom, 'target');
  205. var edge = {
  206. id: id,
  207. name: label,
  208. source: sourceId,
  209. target: targetId,
  210. lineStyle: {
  211. normal: {}
  212. }
  213. };
  214. var lineStyle = edge.lineStyle.normal;
  215. var vizThicknessDom = getChildByTagName(edgeDom, 'viz:thickness');
  216. var vizColorDom = getChildByTagName(edgeDom, 'viz:color');
  217. // let vizShapeDom = getChildByTagName(edgeDom, 'viz:shape');
  218. if (vizThicknessDom) {
  219. lineStyle.width = parseFloat(vizThicknessDom.getAttribute('value'));
  220. }
  221. if (vizColorDom) {
  222. lineStyle.color = 'rgb(' + [getAttr(vizColorDom, 'r') | 0, getAttr(vizColorDom, 'g') | 0, getAttr(vizColorDom, 'b') | 0].join(',') + ')';
  223. }
  224. // if (vizShapeDom) {
  225. // edge.shape = vizShapeDom.getAttribute('shape');
  226. // }
  227. return edge;
  228. }) : [];
  229. }
  230. function getAttr(el, attrName) {
  231. return el.getAttribute(attrName);
  232. }
  233. function getChildByTagName(parent, tagName) {
  234. var node = parent.firstChild;
  235. while (node) {
  236. if (node.nodeType !== 1 || node.nodeName.toLowerCase() !== tagName.toLowerCase()) {
  237. node = node.nextSibling;
  238. } else {
  239. return node;
  240. }
  241. }
  242. return null;
  243. }
  244. function getChildrenByTagName(parent, tagName) {
  245. var node = parent.firstChild;
  246. var children = [];
  247. while (node) {
  248. if (node.nodeName.toLowerCase() === tagName.toLowerCase()) {
  249. children.push(node);
  250. }
  251. node = node.nextSibling;
  252. }
  253. return children;
  254. }
  255. var gexf = /*#__PURE__*/Object.freeze({
  256. __proto__: null,
  257. parse: parse
  258. });
  259. /*
  260. * Licensed to the Apache Software Foundation (ASF) under one
  261. * or more contributor license agreements. See the NOTICE file
  262. * distributed with this work for additional information
  263. * regarding copyright ownership. The ASF licenses this file
  264. * to you under the Apache License, Version 2.0 (the
  265. * "License"); you may not use this file except in compliance
  266. * with the License. You may obtain a copy of the License at
  267. *
  268. * http://www.apache.org/licenses/LICENSE-2.0
  269. *
  270. * Unless required by applicable law or agreed to in writing,
  271. * software distributed under the License is distributed on an
  272. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  273. * KIND, either express or implied. See the License for the
  274. * specific language governing permissions and limitations
  275. * under the License.
  276. */
  277. /**
  278. * AUTO-GENERATED FILE. DO NOT MODIFY.
  279. */
  280. /*
  281. * Licensed to the Apache Software Foundation (ASF) under one
  282. * or more contributor license agreements. See the NOTICE file
  283. * distributed with this work for additional information
  284. * regarding copyright ownership. The ASF licenses this file
  285. * to you under the Apache License, Version 2.0 (the
  286. * "License"); you may not use this file except in compliance
  287. * with the License. You may obtain a copy of the License at
  288. *
  289. * http://www.apache.org/licenses/LICENSE-2.0
  290. *
  291. * Unless required by applicable law or agreed to in writing,
  292. * software distributed under the License is distributed on an
  293. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  294. * KIND, either express or implied. See the License for the
  295. * specific language governing permissions and limitations
  296. * under the License.
  297. */
  298. function asc(arr) {
  299. arr.sort(function (a, b) {
  300. return a - b;
  301. });
  302. return arr;
  303. }
  304. function quantile(ascArr, p) {
  305. var H = (ascArr.length - 1) * p + 1;
  306. var h = Math.floor(H);
  307. var v = +ascArr[h - 1];
  308. var e = H - h;
  309. return e ? v + e * (ascArr[h] - v) : v;
  310. }
  311. /**
  312. * See:
  313. * <https://en.wikipedia.org/wiki/Box_plot#cite_note-frigge_hoaglin_iglewicz-2>
  314. * <http://stat.ethz.ch/R-manual/R-devel/library/grDevices/html/boxplot.stats.html>
  315. *
  316. * Helper method for preparing data.
  317. *
  318. * @param {Array.<number>} rawData like
  319. * [
  320. * [12,232,443], (raw data set for the first box)
  321. * [3843,5545,1232], (raw data set for the second box)
  322. * ...
  323. * ]
  324. * @param {Object} [opt]
  325. *
  326. * @param {(number|string)} [opt.boundIQR=1.5] Data less than min bound is outlier.
  327. * default 1.5, means Q1 - 1.5 * (Q3 - Q1).
  328. * If 'none'/0 passed, min bound will not be used.
  329. * @param {(number|string)} [opt.layout='horizontal']
  330. * Box plot layout, can be 'horizontal' or 'vertical'
  331. * @return {Object} {
  332. * boxData: Array.<Array.<number>>
  333. * outliers: Array.<Array.<number>>
  334. * axisData: Array.<string>
  335. * }
  336. */
  337. function prepareBoxplotData (rawData, opt) {
  338. opt = opt || {};
  339. var boxData = [];
  340. var outliers = [];
  341. var axisData = [];
  342. var boundIQR = opt.boundIQR;
  343. var useExtreme = boundIQR === 'none' || boundIQR === 0;
  344. for (var i = 0; i < rawData.length; i++) {
  345. axisData.push(i + '');
  346. var ascList = asc(rawData[i].slice());
  347. var Q1 = quantile(ascList, 0.25);
  348. var Q2 = quantile(ascList, 0.5);
  349. var Q3 = quantile(ascList, 0.75);
  350. var min = ascList[0];
  351. var max = ascList[ascList.length - 1];
  352. var bound = (boundIQR == null ? 1.5 : boundIQR) * (Q3 - Q1);
  353. var low = useExtreme ? min : Math.max(min, Q1 - bound);
  354. var high = useExtreme ? max : Math.min(max, Q3 + bound);
  355. boxData.push([low, Q1, Q2, Q3, high]);
  356. for (var j = 0; j < ascList.length; j++) {
  357. var dataItem = ascList[j];
  358. if (dataItem < low || dataItem > high) {
  359. var outlier = [i, dataItem];
  360. opt.layout === 'vertical' && outlier.reverse();
  361. outliers.push(outlier);
  362. }
  363. }
  364. }
  365. return {
  366. boxData: boxData,
  367. outliers: outliers,
  368. axisData: axisData
  369. };
  370. }
  371. // import { boxplotTransform } from './boxplotTransform.js';
  372. var version = '1.0.0';
  373. // export {boxplotTransform};
  374. // For backward compatibility, where the namespace `dataTool` will
  375. // be mounted on `echarts` is the extension `dataTool` is imported.
  376. // But the old version of echarts do not have `dataTool` namespace,
  377. // so check it before mounting.
  378. if (echarts.dataTool) {
  379. echarts.dataTool.version = version;
  380. echarts.dataTool.gexf = gexf;
  381. echarts.dataTool.prepareBoxplotData = prepareBoxplotData;
  382. // echarts.dataTool.boxplotTransform = boxplotTransform;
  383. }
  384. exports.gexf = gexf;
  385. exports.prepareBoxplotData = prepareBoxplotData;
  386. exports.version = version;
  387. Object.defineProperty(exports, '__esModule', { value: true });
  388. })));
  389. //# sourceMappingURL=dataTool.js.map