You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

773 lines
19 KiB

  1. (function (global, factory) {
  2. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('echarts')) :
  3. typeof define === 'function' && define.amd ? define(['exports', 'echarts'], factory) :
  4. (factory((global.dataTool = {}),global.echarts));
  5. }(this, (function (exports,echarts) { 'use strict';
  6. /**
  7. * @module zrender/core/util
  8. */
  9. // 用于处理merge时无法遍历Date等对象的问题
  10. var arrayProto = Array.prototype;
  11. var nativeMap = arrayProto.map;
  12. /**
  13. * Those data types can be cloned:
  14. * Plain object, Array, TypedArray, number, string, null, undefined.
  15. * Those data types will be assgined using the orginal data:
  16. * BUILTIN_OBJECT
  17. * Instance of user defined class will be cloned to a plain object, without
  18. * properties in prototype.
  19. * Other data types is not supported (not sure what will happen).
  20. *
  21. * Caution: do not support clone Date, for performance consideration.
  22. * (There might be a large number of date in `series.data`).
  23. * So date should not be modified in and out of echarts.
  24. *
  25. * @param {*} source
  26. * @return {*} new
  27. */
  28. /**
  29. * @memberOf module:zrender/core/util
  30. * @param {*} target
  31. * @param {*} source
  32. * @param {boolean} [overwrite=false]
  33. */
  34. /**
  35. * @param {Array} targetAndSources The first item is target, and the rests are source.
  36. * @param {boolean} [overwrite=false]
  37. * @return {*} target
  38. */
  39. /**
  40. * @param {*} target
  41. * @param {*} source
  42. * @memberOf module:zrender/core/util
  43. */
  44. /**
  45. * @param {*} target
  46. * @param {*} source
  47. * @param {boolean} [overlay=false]
  48. * @memberOf module:zrender/core/util
  49. */
  50. /**
  51. * 查询数组中元素的index
  52. * @memberOf module:zrender/core/util
  53. */
  54. /**
  55. * 构造类继承关系
  56. *
  57. * @memberOf module:zrender/core/util
  58. * @param {Function} clazz 源类
  59. * @param {Function} baseClazz 基类
  60. */
  61. /**
  62. * @memberOf module:zrender/core/util
  63. * @param {Object|Function} target
  64. * @param {Object|Function} sorce
  65. * @param {boolean} overlay
  66. */
  67. /**
  68. * Consider typed array.
  69. * @param {Array|TypedArray} data
  70. */
  71. /**
  72. * 数组或对象遍历
  73. * @memberOf module:zrender/core/util
  74. * @param {Object|Array} obj
  75. * @param {Function} cb
  76. * @param {*} [context]
  77. */
  78. /**
  79. * 数组映射
  80. * @memberOf module:zrender/core/util
  81. * @param {Array} obj
  82. * @param {Function} cb
  83. * @param {*} [context]
  84. * @return {Array}
  85. */
  86. function map(obj, cb, context) {
  87. if (!(obj && cb)) {
  88. return;
  89. }
  90. if (obj.map && obj.map === nativeMap) {
  91. return obj.map(cb, context);
  92. }
  93. else {
  94. var result = [];
  95. for (var i = 0, len = obj.length; i < len; i++) {
  96. result.push(cb.call(context, obj[i], i, obj));
  97. }
  98. return result;
  99. }
  100. }
  101. /**
  102. * @memberOf module:zrender/core/util
  103. * @param {Array} obj
  104. * @param {Function} cb
  105. * @param {Object} [memo]
  106. * @param {*} [context]
  107. * @return {Array}
  108. */
  109. /**
  110. * 数组过滤
  111. * @memberOf module:zrender/core/util
  112. * @param {Array} obj
  113. * @param {Function} cb
  114. * @param {*} [context]
  115. * @return {Array}
  116. */
  117. /**
  118. * 数组项查找
  119. * @memberOf module:zrender/core/util
  120. * @param {Array} obj
  121. * @param {Function} cb
  122. * @param {*} [context]
  123. * @return {*}
  124. */
  125. /**
  126. * @memberOf module:zrender/core/util
  127. * @param {Function} func
  128. * @param {*} context
  129. * @return {Function}
  130. */
  131. /**
  132. * @memberOf module:zrender/core/util
  133. * @param {Function} func
  134. * @return {Function}
  135. */
  136. /**
  137. * @memberOf module:zrender/core/util
  138. * @param {*} value
  139. * @return {boolean}
  140. */
  141. /**
  142. * @memberOf module:zrender/core/util
  143. * @param {*} value
  144. * @return {boolean}
  145. */
  146. /**
  147. * @memberOf module:zrender/core/util
  148. * @param {*} value
  149. * @return {boolean}
  150. */
  151. /**
  152. * @memberOf module:zrender/core/util
  153. * @param {*} value
  154. * @return {boolean}
  155. */
  156. /**
  157. * @memberOf module:zrender/core/util
  158. * @param {*} value
  159. * @return {boolean}
  160. */
  161. /**
  162. * @memberOf module:zrender/core/util
  163. * @param {*} value
  164. * @return {boolean}
  165. */
  166. /**
  167. * @memberOf module:zrender/core/util
  168. * @param {*} value
  169. * @return {boolean}
  170. */
  171. /**
  172. * Whether is exactly NaN. Notice isNaN('a') returns true.
  173. * @param {*} value
  174. * @return {boolean}
  175. */
  176. /**
  177. * If value1 is not null, then return value1, otherwise judget rest of values.
  178. * Low performance.
  179. * @memberOf module:zrender/core/util
  180. * @return {*} Final value
  181. */
  182. /**
  183. * @memberOf module:zrender/core/util
  184. * @param {Array} arr
  185. * @param {number} startIndex
  186. * @param {number} endIndex
  187. * @return {Array}
  188. */
  189. /**
  190. * Normalize css liked array configuration
  191. * e.g.
  192. * 3 => [3, 3, 3, 3]
  193. * [4, 2] => [4, 2, 4, 2]
  194. * [4, 3, 2] => [4, 3, 2, 3]
  195. * @param {number|Array.<number>} val
  196. * @return {Array.<number>}
  197. */
  198. /**
  199. * @memberOf module:zrender/core/util
  200. * @param {boolean} condition
  201. * @param {string} message
  202. */
  203. /**
  204. * @memberOf module:zrender/core/util
  205. * @param {string} str string to be trimed
  206. * @return {string} trimed string
  207. */
  208. /**
  209. * Set an object as primitive to be ignored traversing children in clone or merge
  210. */
  211. // GEXF File Parser
  212. // http://gexf.net/1.2draft/gexf-12draft-primer.pdf
  213. function parse(xml) {
  214. var doc;
  215. if (typeof xml === 'string') {
  216. var parser = new DOMParser();
  217. doc = parser.parseFromString(xml, 'text/xml');
  218. }
  219. else {
  220. doc = xml;
  221. }
  222. if (!doc || doc.getElementsByTagName('parsererror').length) {
  223. return null;
  224. }
  225. var gexfRoot = getChildByTagName(doc, 'gexf');
  226. if (!gexfRoot) {
  227. return null;
  228. }
  229. var graphRoot = getChildByTagName(gexfRoot, 'graph');
  230. var attributes = parseAttributes(getChildByTagName(graphRoot, 'attributes'));
  231. var attributesMap = {};
  232. for (var i = 0; i < attributes.length; i++) {
  233. attributesMap[attributes[i].id] = attributes[i];
  234. }
  235. return {
  236. nodes: parseNodes(getChildByTagName(graphRoot, 'nodes'), attributesMap),
  237. links: parseEdges(getChildByTagName(graphRoot, 'edges'))
  238. };
  239. }
  240. function parseAttributes(parent) {
  241. return parent ? map(getChildrenByTagName(parent, 'attribute'), function (attribDom) {
  242. return {
  243. id: getAttr(attribDom, 'id'),
  244. title: getAttr(attribDom, 'title'),
  245. type: getAttr(attribDom, 'type')
  246. };
  247. }) : [];
  248. }
  249. function parseNodes(parent, attributesMap) {
  250. return parent ? map(getChildrenByTagName(parent, 'node'), function (nodeDom) {
  251. var id = getAttr(nodeDom, 'id');
  252. var label = getAttr(nodeDom, 'label');
  253. var node = {
  254. id: id,
  255. name: label,
  256. itemStyle: {
  257. normal: {}
  258. }
  259. };
  260. var vizSizeDom = getChildByTagName(nodeDom, 'viz:size');
  261. var vizPosDom = getChildByTagName(nodeDom, 'viz:position');
  262. var vizColorDom = getChildByTagName(nodeDom, 'viz:color');
  263. // var vizShapeDom = getChildByTagName(nodeDom, 'viz:shape');
  264. var attvaluesDom = getChildByTagName(nodeDom, 'attvalues');
  265. if (vizSizeDom) {
  266. node.symbolSize = parseFloat(getAttr(vizSizeDom, 'value'));
  267. }
  268. if (vizPosDom) {
  269. node.x = parseFloat(getAttr(vizPosDom, 'x'));
  270. node.y = parseFloat(getAttr(vizPosDom, 'y'));
  271. // z
  272. }
  273. if (vizColorDom) {
  274. node.itemStyle.normal.color = 'rgb(' +[
  275. getAttr(vizColorDom, 'r') | 0,
  276. getAttr(vizColorDom, 'g') | 0,
  277. getAttr(vizColorDom, 'b') | 0
  278. ].join(',') + ')';
  279. }
  280. // if (vizShapeDom) {
  281. // node.shape = getAttr(vizShapeDom, 'shape');
  282. // }
  283. if (attvaluesDom) {
  284. var attvalueDomList = getChildrenByTagName(attvaluesDom, 'attvalue');
  285. node.attributes = {};
  286. for (var j = 0; j < attvalueDomList.length; j++) {
  287. var attvalueDom = attvalueDomList[j];
  288. var attId = getAttr(attvalueDom, 'for');
  289. var attValue = getAttr(attvalueDom, 'value');
  290. var attribute = attributesMap[attId];
  291. if (attribute) {
  292. switch (attribute.type) {
  293. case 'integer':
  294. case 'long':
  295. attValue = parseInt(attValue, 10);
  296. break;
  297. case 'float':
  298. case 'double':
  299. attValue = parseFloat(attValue);
  300. break;
  301. case 'boolean':
  302. attValue = attValue.toLowerCase() == 'true';
  303. break;
  304. default:
  305. }
  306. node.attributes[attId] = attValue;
  307. }
  308. }
  309. }
  310. return node;
  311. }) : [];
  312. }
  313. function parseEdges(parent) {
  314. return parent ? map(getChildrenByTagName(parent, 'edge'), function (edgeDom) {
  315. var id = getAttr(edgeDom, 'id');
  316. var label = getAttr(edgeDom, 'label');
  317. var sourceId = getAttr(edgeDom, 'source');
  318. var targetId = getAttr(edgeDom, 'target');
  319. var edge = {
  320. id: id,
  321. name: label,
  322. source: sourceId,
  323. target: targetId,
  324. lineStyle: {
  325. normal: {}
  326. }
  327. };
  328. var lineStyle = edge.lineStyle.normal;
  329. var vizThicknessDom = getChildByTagName(edgeDom, 'viz:thickness');
  330. var vizColorDom = getChildByTagName(edgeDom, 'viz:color');
  331. // var vizShapeDom = getChildByTagName(edgeDom, 'viz:shape');
  332. if (vizThicknessDom) {
  333. lineStyle.width = parseFloat(vizThicknessDom.getAttribute('value'));
  334. }
  335. if (vizColorDom) {
  336. lineStyle.color = 'rgb(' + [
  337. getAttr(vizColorDom, 'r') | 0,
  338. getAttr(vizColorDom, 'g') | 0,
  339. getAttr(vizColorDom, 'b') | 0
  340. ].join(',') + ')';
  341. }
  342. // if (vizShapeDom) {
  343. // edge.shape = vizShapeDom.getAttribute('shape');
  344. // }
  345. return edge;
  346. }) : [];
  347. }
  348. function getAttr(el, attrName) {
  349. return el.getAttribute(attrName);
  350. }
  351. function getChildByTagName (parent, tagName) {
  352. var node = parent.firstChild;
  353. while (node) {
  354. if (
  355. node.nodeType != 1 ||
  356. node.nodeName.toLowerCase() != tagName.toLowerCase()
  357. ) {
  358. node = node.nextSibling;
  359. } else {
  360. return node;
  361. }
  362. }
  363. return null;
  364. }
  365. function getChildrenByTagName (parent, tagName) {
  366. var node = parent.firstChild;
  367. var children = [];
  368. while (node) {
  369. if (node.nodeName.toLowerCase() == tagName.toLowerCase()) {
  370. children.push(node);
  371. }
  372. node = node.nextSibling;
  373. }
  374. return children;
  375. }
  376. var gexf = (Object.freeze || Object)({
  377. parse: parse
  378. });
  379. /**
  380. * Copyright (c) 2010-2015, Michael Bostock
  381. * All rights reserved.
  382. *
  383. * Redistribution and use in source and binary forms, with or without
  384. * modification, are permitted provided that the following conditions are met:
  385. *
  386. * * Redistributions of source code must retain the above copyright notice, this
  387. * list of conditions and the following disclaimer.
  388. *
  389. * * Redistributions in binary form must reproduce the above copyright notice,
  390. * this list of conditions and the following disclaimer in the documentation
  391. * and/or other materials provided with the distribution.
  392. *
  393. * * The name Michael Bostock may not be used to endorse or promote products
  394. * derived from this software without specific prior written permission.
  395. *
  396. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  397. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  398. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  399. * DISCLAIMED. IN NO EVENT SHALL MICHAEL BOSTOCK BE LIABLE FOR ANY DIRECT,
  400. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  401. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  402. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  403. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  404. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  405. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  406. */
  407. /**
  408. * @see <https://github.com/mbostock/d3/blob/master/src/arrays/quantile.js>
  409. * @see <http://en.wikipedia.org/wiki/Quantile>
  410. * @param {Array.<number>} ascArr
  411. */
  412. var quantile = function(ascArr, p) {
  413. var H = (ascArr.length - 1) * p + 1,
  414. h = Math.floor(H),
  415. v = +ascArr[h - 1],
  416. e = H - h;
  417. return e ? v + e * (ascArr[h] - v) : v;
  418. };
  419. /**
  420. * Linear mapping a value from domain to range
  421. * @memberOf module:echarts/util/number
  422. * @param {(number|Array.<number>)} val
  423. * @param {Array.<number>} domain Domain extent domain[0] can be bigger than domain[1]
  424. * @param {Array.<number>} range Range extent range[0] can be bigger than range[1]
  425. * @param {boolean} clamp
  426. * @return {(number|Array.<number>}
  427. */
  428. /**
  429. * Convert a percent string to absolute number.
  430. * Returns NaN if percent is not a valid string or number
  431. * @memberOf module:echarts/util/number
  432. * @param {string|number} percent
  433. * @param {number} all
  434. * @return {number}
  435. */
  436. /**
  437. * (1) Fix rounding error of float numbers.
  438. * (2) Support return string to avoid scientific notation like '3.5e-7'.
  439. *
  440. * @param {number} x
  441. * @param {number} [precision]
  442. * @param {boolean} [returnStr]
  443. * @return {number|string}
  444. */
  445. function asc(arr) {
  446. arr.sort(function (a, b) {
  447. return a - b;
  448. });
  449. return arr;
  450. }
  451. /**
  452. * Get precision
  453. * @param {number} val
  454. */
  455. /**
  456. * @param {string|number} val
  457. * @return {number}
  458. */
  459. /**
  460. * Minimal dicernible data precisioin according to a single pixel.
  461. *
  462. * @param {Array.<number>} dataExtent
  463. * @param {Array.<number>} pixelExtent
  464. * @return {number} precision
  465. */
  466. /**
  467. * Get a data of given precision, assuring the sum of percentages
  468. * in valueList is 1.
  469. * The largest remainer method is used.
  470. * https://en.wikipedia.org/wiki/Largest_remainder_method
  471. *
  472. * @param {Array.<number>} valueList a list of all data
  473. * @param {number} idx index of the data to be processed in valueList
  474. * @param {number} precision integer number showing digits of precision
  475. * @return {number} percent ranging from 0 to 100
  476. */
  477. // Number.MAX_SAFE_INTEGER, ie do not support.
  478. /**
  479. * To 0 - 2 * PI, considering negative radian.
  480. * @param {number} radian
  481. * @return {number}
  482. */
  483. /**
  484. * @param {type} radian
  485. * @return {boolean}
  486. */
  487. /**
  488. * @param {string|Date|number} value These values can be accepted:
  489. * + An instance of Date, represent a time in its own time zone.
  490. * + Or string in a subset of ISO 8601, only including:
  491. * + only year, month, date: '2012-03', '2012-03-01', '2012-03-01 05', '2012-03-01 05:06',
  492. * + separated with T or space: '2012-03-01T12:22:33.123', '2012-03-01 12:22:33.123',
  493. * + time zone: '2012-03-01T12:22:33Z', '2012-03-01T12:22:33+8000', '2012-03-01T12:22:33-05:00',
  494. * all of which will be treated as local time if time zone is not specified
  495. * (see <https://momentjs.com/>).
  496. * + Or other string format, including (all of which will be treated as loacal time):
  497. * '2012', '2012-3-1', '2012/3/1', '2012/03/01',
  498. * '2009/6/12 2:00', '2009/6/12 2:05:08', '2009/6/12 2:05:08.123'
  499. * + a timestamp, which represent a time in UTC.
  500. * @return {Date} date
  501. */
  502. /**
  503. * Quantity of a number. e.g. 0.1, 1, 10, 100
  504. *
  505. * @param {number} val
  506. * @return {number}
  507. */
  508. /**
  509. * find a “nice” number approximately equal to x. Round the number if round = true,
  510. * take ceiling if round = false. The primary observation is that the “nicest”
  511. * numbers in decimal are 1, 2, and 5, and all power-of-ten multiples of these numbers.
  512. *
  513. * See "Nice Numbers for Graph Labels" of Graphic Gems.
  514. *
  515. * @param {number} val Non-negative value.
  516. * @param {boolean} round
  517. * @return {number}
  518. */
  519. /**
  520. * Order intervals asc, and split them when overlap.
  521. * expect(numberUtil.reformIntervals([
  522. * {interval: [18, 62], close: [1, 1]},
  523. * {interval: [-Infinity, -70], close: [0, 0]},
  524. * {interval: [-70, -26], close: [1, 1]},
  525. * {interval: [-26, 18], close: [1, 1]},
  526. * {interval: [62, 150], close: [1, 1]},
  527. * {interval: [106, 150], close: [1, 1]},
  528. * {interval: [150, Infinity], close: [0, 0]}
  529. * ])).toEqual([
  530. * {interval: [-Infinity, -70], close: [0, 0]},
  531. * {interval: [-70, -26], close: [1, 1]},
  532. * {interval: [-26, 18], close: [0, 1]},
  533. * {interval: [18, 62], close: [0, 1]},
  534. * {interval: [62, 150], close: [0, 1]},
  535. * {interval: [150, Infinity], close: [0, 0]}
  536. * ]);
  537. * @param {Array.<Object>} list, where `close` mean open or close
  538. * of the interval, and Infinity can be used.
  539. * @return {Array.<Object>} The origin list, which has been reformed.
  540. */
  541. /**
  542. * parseFloat NaNs numeric-cast false positives (null|true|false|"")
  543. * ...but misinterprets leading-number strings, particularly hex literals ("0x...")
  544. * subtraction forces infinities to NaN
  545. *
  546. * @param {*} v
  547. * @return {boolean}
  548. */
  549. /**
  550. * See:
  551. * <https://en.wikipedia.org/wiki/Box_plot#cite_note-frigge_hoaglin_iglewicz-2>
  552. * <http://stat.ethz.ch/R-manual/R-devel/library/grDevices/html/boxplot.stats.html>
  553. *
  554. * Helper method for preparing data.
  555. *
  556. * @param {Array.<number>} rawData like
  557. * [
  558. * [12,232,443], (raw data set for the first box)
  559. * [3843,5545,1232], (raw datat set for the second box)
  560. * ...
  561. * ]
  562. * @param {Object} [opt]
  563. *
  564. * @param {(number|string)} [opt.boundIQR=1.5] Data less than min bound is outlier.
  565. * default 1.5, means Q1 - 1.5 * (Q3 - Q1).
  566. * If 'none'/0 passed, min bound will not be used.
  567. * @param {(number|string)} [opt.layout='horizontal']
  568. * Box plot layout, can be 'horizontal' or 'vertical'
  569. * @return {Object} {
  570. * boxData: Array.<Array.<number>>
  571. * outliers: Array.<Array.<number>>
  572. * axisData: Array.<string>
  573. * }
  574. */
  575. var prepareBoxplotData = function (rawData, opt) {
  576. opt = opt || [];
  577. var boxData = [];
  578. var outliers = [];
  579. var axisData = [];
  580. var boundIQR = opt.boundIQR;
  581. var useExtreme = boundIQR === 'none' || boundIQR === 0;
  582. for (var i = 0; i < rawData.length; i++) {
  583. axisData.push(i + '');
  584. var ascList = asc(rawData[i].slice());
  585. var Q1 = quantile(ascList, 0.25);
  586. var Q2 = quantile(ascList, 0.5);
  587. var Q3 = quantile(ascList, 0.75);
  588. var min = ascList[0];
  589. var max = ascList[ascList.length - 1];
  590. var bound = (boundIQR == null ? 1.5 : boundIQR) * (Q3 - Q1);
  591. var low = useExtreme
  592. ? min
  593. : Math.max(min, Q1 - bound);
  594. var high = useExtreme
  595. ? max
  596. : Math.min(max, Q3 + bound);
  597. boxData.push([low, Q1, Q2, Q3, high]);
  598. for (var j = 0; j < ascList.length; j++) {
  599. var dataItem = ascList[j];
  600. if (dataItem < low || dataItem > high) {
  601. var outlier = [i, dataItem];
  602. opt.layout === 'vertical' && outlier.reverse();
  603. outliers.push(outlier);
  604. }
  605. }
  606. }
  607. return {
  608. boxData: boxData,
  609. outliers: outliers,
  610. axisData: axisData
  611. };
  612. };
  613. var version = '1.0.0';
  614. // For backward compatibility, where the namespace `dataTool` will
  615. // be mounted on `echarts` is the extension `dataTool` is imported.
  616. // But the old version of echarts do not have `dataTool` namespace,
  617. // so check it before mounting.
  618. if (echarts.dataTool) {
  619. echarts.dataTool.version = version;
  620. echarts.dataTool.gexf = gexf;
  621. echarts.dataTool.prepareBoxplotData = prepareBoxplotData;
  622. }
  623. exports.version = version;
  624. exports.gexf = gexf;
  625. exports.prepareBoxplotData = prepareBoxplotData;
  626. })));
  627. //# sourceMappingURL=dataTool.js.map