123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586 |
- var zrUtil = require("zrender/lib/core/util");
- var RADIAN_EPSILON = 1e-4;
- function _trim(str) {
- return str.replace(/^\s+|\s+$/g, '');
- }
- function linearMap(val, domain, range, clamp) {
- var subDomain = domain[1] - domain[0];
- var subRange = range[1] - range[0];
- if (subDomain === 0) {
- return subRange === 0 ? range[0] : (range[0] + range[1]) / 2;
- }
-
-
-
-
- if (clamp) {
- if (subDomain > 0) {
- if (val <= domain[0]) {
- return range[0];
- } else if (val >= domain[1]) {
- return range[1];
- }
- } else {
- if (val >= domain[0]) {
- return range[0];
- } else if (val <= domain[1]) {
- return range[1];
- }
- }
- } else {
- if (val === domain[0]) {
- return range[0];
- }
- if (val === domain[1]) {
- return range[1];
- }
- }
- return (val - domain[0]) / subDomain * subRange + range[0];
- }
- function parsePercent(percent, all) {
- switch (percent) {
- case 'center':
- case 'middle':
- percent = '50%';
- break;
- case 'left':
- case 'top':
- percent = '0%';
- break;
- case 'right':
- case 'bottom':
- percent = '100%';
- break;
- }
- if (typeof percent === 'string') {
- if (_trim(percent).match(/%$/)) {
- return parseFloat(percent) / 100 * all;
- }
- return parseFloat(percent);
- }
- return percent == null ? NaN : +percent;
- }
- function round(x, precision, returnStr) {
- if (precision == null) {
- precision = 10;
- }
- precision = Math.min(Math.max(0, precision), 20);
- x = (+x).toFixed(precision);
- return returnStr ? x : +x;
- }
- function asc(arr) {
- arr.sort(function (a, b) {
- return a - b;
- });
- return arr;
- }
- function getPrecision(val) {
- val = +val;
- if (isNaN(val)) {
- return 0;
- }
-
-
-
- var e = 1;
- var count = 0;
- while (Math.round(val * e) / e !== val) {
- e *= 10;
- count++;
- }
- return count;
- }
- function getPrecisionSafe(val) {
- var str = val.toString();
- var eIndex = str.indexOf('e');
- if (eIndex > 0) {
- var precision = +str.slice(eIndex + 1);
- return precision < 0 ? -precision : 0;
- } else {
- var dotIndex = str.indexOf('.');
- return dotIndex < 0 ? 0 : str.length - 1 - dotIndex;
- }
- }
- function getPixelPrecision(dataExtent, pixelExtent) {
- var log = Math.log;
- var LN10 = Math.LN10;
- var dataQuantity = Math.floor(log(dataExtent[1] - dataExtent[0]) / LN10);
- var sizeQuantity = Math.round(log(Math.abs(pixelExtent[1] - pixelExtent[0])) / LN10);
- var precision = Math.min(Math.max(-dataQuantity + sizeQuantity, 0), 20);
- return !isFinite(precision) ? 20 : precision;
- }
- function getPercentWithPrecision(valueList, idx, precision) {
- if (!valueList[idx]) {
- return 0;
- }
- var sum = zrUtil.reduce(valueList, function (acc, val) {
- return acc + (isNaN(val) ? 0 : val);
- }, 0);
- if (sum === 0) {
- return 0;
- }
- var digits = Math.pow(10, precision);
- var votesPerQuota = zrUtil.map(valueList, function (val) {
- return (isNaN(val) ? 0 : val) / sum * digits * 100;
- });
- var targetSeats = digits * 100;
- var seats = zrUtil.map(votesPerQuota, function (votes) {
-
- return Math.floor(votes);
- });
- var currentSum = zrUtil.reduce(seats, function (acc, val) {
- return acc + val;
- }, 0);
- var remainder = zrUtil.map(votesPerQuota, function (votes, idx) {
- return votes - seats[idx];
- });
- while (currentSum < targetSeats) {
-
- var max = Number.NEGATIVE_INFINITY;
- var maxId = null;
- for (var i = 0, len = remainder.length; i < len; ++i) {
- if (remainder[i] > max) {
- max = remainder[i];
- maxId = i;
- }
- }
- ++seats[maxId];
- remainder[maxId] = 0;
- ++currentSum;
- }
- return seats[idx] / digits;
- }
- var MAX_SAFE_INTEGER = 9007199254740991;
- function remRadian(radian) {
- var pi2 = Math.PI * 2;
- return (radian % pi2 + pi2) % pi2;
- }
- function isRadianAroundZero(val) {
- return val > -RADIAN_EPSILON && val < RADIAN_EPSILON;
- }
- var TIME_REG = /^(?:(\d{4})(?:[-\/](\d{1,2})(?:[-\/](\d{1,2})(?:[T ](\d{1,2})(?::(\d\d)(?::(\d\d)(?:[.,](\d+))?)?)?(Z|[\+\-]\d\d:?\d\d)?)?)?)?)?$/;
- function parseDate(value) {
- if (value instanceof Date) {
- return value;
- } else if (typeof value === 'string') {
-
-
-
-
-
- var match = TIME_REG.exec(value);
- if (!match) {
-
- return new Date(NaN);
- }
- if (!match[8]) {
-
-
- return new Date(+match[1], +(match[2] || 1) - 1, +match[3] || 1, +match[4] || 0, +(match[5] || 0), +match[6] || 0, +match[7] || 0);
- }
-
-
-
-
-
-
- else {
- var hour = +match[4] || 0;
- if (match[8].toUpperCase() !== 'Z') {
- hour -= match[8].slice(0, 3);
- }
- return new Date(Date.UTC(+match[1], +(match[2] || 1) - 1, +match[3] || 1, hour, +(match[5] || 0), +match[6] || 0, +match[7] || 0));
- }
- } else if (value == null) {
- return new Date(NaN);
- }
- return new Date(Math.round(value));
- }
- function quantity(val) {
- return Math.pow(10, quantityExponent(val));
- }
- function quantityExponent(val) {
- if (val === 0) {
- return 0;
- }
- var exp = Math.floor(Math.log(val) / Math.LN10);
-
- if (val / Math.pow(10, exp) >= 10) {
- exp++;
- }
- return exp;
- }
- function nice(val, round) {
- var exponent = quantityExponent(val);
- var exp10 = Math.pow(10, exponent);
- var f = val / exp10;
- var nf;
- if (round) {
- if (f < 1.5) {
- nf = 1;
- } else if (f < 2.5) {
- nf = 2;
- } else if (f < 4) {
- nf = 3;
- } else if (f < 7) {
- nf = 5;
- } else {
- nf = 10;
- }
- } else {
- if (f < 1) {
- nf = 1;
- } else if (f < 2) {
- nf = 2;
- } else if (f < 3) {
- nf = 3;
- } else if (f < 5) {
- nf = 5;
- } else {
- nf = 10;
- }
- }
- val = nf * exp10;
-
- return exponent >= -20 ? +val.toFixed(exponent < 0 ? -exponent : 0) : val;
- }
- function quantile(ascArr, p) {
- var H = (ascArr.length - 1) * p + 1;
- var h = Math.floor(H);
- var v = +ascArr[h - 1];
- var e = H - h;
- return e ? v + e * (ascArr[h] - v) : v;
- }
- function reformIntervals(list) {
- list.sort(function (a, b) {
- return littleThan(a, b, 0) ? -1 : 1;
- });
- var curr = -Infinity;
- var currClose = 1;
- for (var i = 0; i < list.length;) {
- var interval = list[i].interval;
- var close = list[i].close;
- for (var lg = 0; lg < 2; lg++) {
- if (interval[lg] <= curr) {
- interval[lg] = curr;
- close[lg] = !lg ? 1 - currClose : 1;
- }
- curr = interval[lg];
- currClose = close[lg];
- }
- if (interval[0] === interval[1] && close[0] * close[1] !== 1) {
- list.splice(i, 1);
- } else {
- i++;
- }
- }
- return list;
- function littleThan(a, b, lg) {
- return a.interval[lg] < b.interval[lg] || a.interval[lg] === b.interval[lg] && (a.close[lg] - b.close[lg] === (!lg ? 1 : -1) || !lg && littleThan(a, b, 1));
- }
- }
- function isNumeric(v) {
- return v - parseFloat(v) >= 0;
- }
- exports.linearMap = linearMap;
- exports.parsePercent = parsePercent;
- exports.round = round;
- exports.asc = asc;
- exports.getPrecision = getPrecision;
- exports.getPrecisionSafe = getPrecisionSafe;
- exports.getPixelPrecision = getPixelPrecision;
- exports.getPercentWithPrecision = getPercentWithPrecision;
- exports.MAX_SAFE_INTEGER = MAX_SAFE_INTEGER;
- exports.remRadian = remRadian;
- exports.isRadianAroundZero = isRadianAroundZero;
- exports.parseDate = parseDate;
- exports.quantity = quantity;
- exports.quantityExponent = quantityExponent;
- exports.nice = nice;
- exports.quantile = quantile;
- exports.reformIntervals = reformIntervals;
- exports.isNumeric = isNumeric;
|