v1.0 with SW PWA enabled
This commit is contained in:
260
frontend/node_modules/recharts-scale/src/getNiceTickValues.js
generated
vendored
Normal file
260
frontend/node_modules/recharts-scale/src/getNiceTickValues.js
generated
vendored
Normal file
@ -0,0 +1,260 @@
|
||||
/**
|
||||
* @fileOverview calculate tick values of scale
|
||||
* @author xile611, arcthur
|
||||
* @date 2015-09-17
|
||||
*/
|
||||
import Decimal from 'decimal.js-light';
|
||||
import {
|
||||
compose, range, memoize, map, reverse,
|
||||
} from './util/utils';
|
||||
import Arithmetic from './util/arithmetic';
|
||||
|
||||
/**
|
||||
* Calculate a interval of a minimum value and a maximum value
|
||||
*
|
||||
* @param {Number} min The minimum value
|
||||
* @param {Number} max The maximum value
|
||||
* @return {Array} An interval
|
||||
*/
|
||||
function getValidInterval([min, max]) {
|
||||
let [validMin, validMax] = [min, max];
|
||||
|
||||
// exchange
|
||||
if (min > max) {
|
||||
[validMin, validMax] = [max, min];
|
||||
}
|
||||
|
||||
return [validMin, validMax];
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate the step which is easy to understand between ticks, like 10, 20, 25
|
||||
*
|
||||
* @param {Decimal} roughStep The rough step calculated by deviding the
|
||||
* difference by the tickCount
|
||||
* @param {Boolean} allowDecimals Allow the ticks to be decimals or not
|
||||
* @param {Integer} correctionFactor A correction factor
|
||||
* @return {Decimal} The step which is easy to understand between two ticks
|
||||
*/
|
||||
function getFormatStep(roughStep, allowDecimals, correctionFactor) {
|
||||
if (roughStep.lte(0)) { return new Decimal(0); }
|
||||
|
||||
const digitCount = Arithmetic.getDigitCount(roughStep.toNumber());
|
||||
// The ratio between the rough step and the smallest number which has a bigger
|
||||
// order of magnitudes than the rough step
|
||||
const digitCountValue = new Decimal(10).pow(digitCount);
|
||||
const stepRatio = roughStep.div(digitCountValue);
|
||||
// When an integer and a float multiplied, the accuracy of result may be wrong
|
||||
const stepRatioScale = digitCount !== 1 ? 0.05 : 0.1;
|
||||
const amendStepRatio = new Decimal(
|
||||
Math.ceil(stepRatio.div(stepRatioScale).toNumber()),
|
||||
).add(correctionFactor).mul(stepRatioScale);
|
||||
|
||||
const formatStep = amendStepRatio.mul(digitCountValue);
|
||||
|
||||
return allowDecimals ? formatStep : new Decimal(Math.ceil(formatStep));
|
||||
}
|
||||
|
||||
/**
|
||||
* calculate the ticks when the minimum value equals to the maximum value
|
||||
*
|
||||
* @param {Number} value The minimum valuue which is also the maximum value
|
||||
* @param {Integer} tickCount The count of ticks
|
||||
* @param {Boolean} allowDecimals Allow the ticks to be decimals or not
|
||||
* @return {Array} ticks
|
||||
*/
|
||||
function getTickOfSingleValue(value, tickCount, allowDecimals) {
|
||||
let step = 1;
|
||||
// calculate the middle value of ticks
|
||||
let middle = new Decimal(value);
|
||||
|
||||
if (!middle.isint() && allowDecimals) {
|
||||
const absVal = Math.abs(value);
|
||||
|
||||
if (absVal < 1) {
|
||||
// The step should be a float number when the difference is smaller than 1
|
||||
step = new Decimal(10).pow(Arithmetic.getDigitCount(value) - 1);
|
||||
|
||||
middle = new Decimal(Math.floor(middle.div(step).toNumber())).mul(step);
|
||||
} else if (absVal > 1) {
|
||||
// Return the maximum integer which is smaller than 'value' when 'value' is greater than 1
|
||||
middle = new Decimal(Math.floor(value));
|
||||
}
|
||||
} else if (value === 0) {
|
||||
middle = new Decimal(Math.floor((tickCount - 1) / 2));
|
||||
} else if (!allowDecimals) {
|
||||
middle = new Decimal(Math.floor(value));
|
||||
}
|
||||
|
||||
const middleIndex = Math.floor((tickCount - 1) / 2);
|
||||
|
||||
const fn = compose(
|
||||
map((n) => middle.add(new Decimal(n - middleIndex).mul(step)).toNumber()),
|
||||
range,
|
||||
);
|
||||
|
||||
return fn(0, tickCount);
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate the step
|
||||
*
|
||||
* @param {Number} min The minimum value of an interval
|
||||
* @param {Number} max The maximum value of an interval
|
||||
* @param {Integer} tickCount The count of ticks
|
||||
* @param {Boolean} allowDecimals Allow the ticks to be decimals or not
|
||||
* @param {Number} correctionFactor A correction factor
|
||||
* @return {Object} The step, minimum value of ticks, maximum value of ticks
|
||||
*/
|
||||
function calculateStep(min, max, tickCount, allowDecimals, correctionFactor = 0) {
|
||||
// dirty hack (for recharts' test)
|
||||
if (!Number.isFinite((max - min) / (tickCount - 1))) {
|
||||
return {
|
||||
step: new Decimal(0),
|
||||
tickMin: new Decimal(0),
|
||||
tickMax: new Decimal(0),
|
||||
};
|
||||
}
|
||||
|
||||
// The step which is easy to understand between two ticks
|
||||
const step = getFormatStep(
|
||||
new Decimal(max).sub(min).div(tickCount - 1),
|
||||
allowDecimals,
|
||||
correctionFactor,
|
||||
);
|
||||
|
||||
// A medial value of ticks
|
||||
let middle;
|
||||
|
||||
// When 0 is inside the interval, 0 should be a tick
|
||||
if (min <= 0 && max >= 0) {
|
||||
middle = new Decimal(0);
|
||||
} else {
|
||||
// calculate the middle value
|
||||
middle = new Decimal(min).add(max).div(2);
|
||||
// minus modulo value
|
||||
middle = middle.sub(new Decimal(middle).mod(step));
|
||||
}
|
||||
|
||||
let belowCount = Math.ceil(middle.sub(min).div(step).toNumber());
|
||||
let upCount = Math.ceil(new Decimal(max).sub(middle).div(step)
|
||||
.toNumber());
|
||||
const scaleCount = belowCount + upCount + 1;
|
||||
|
||||
if (scaleCount > tickCount) {
|
||||
// When more ticks need to cover the interval, step should be bigger.
|
||||
return calculateStep(min, max, tickCount, allowDecimals, correctionFactor + 1);
|
||||
} if (scaleCount < tickCount) {
|
||||
// When less ticks can cover the interval, we should add some additional ticks
|
||||
upCount = max > 0 ? upCount + (tickCount - scaleCount) : upCount;
|
||||
belowCount = max > 0 ? belowCount : belowCount + (tickCount - scaleCount);
|
||||
}
|
||||
|
||||
return {
|
||||
step,
|
||||
tickMin: middle.sub(new Decimal(belowCount).mul(step)),
|
||||
tickMax: middle.add(new Decimal(upCount).mul(step)),
|
||||
};
|
||||
}
|
||||
/**
|
||||
* Calculate the ticks of an interval, the count of ticks will be guraranteed
|
||||
*
|
||||
* @param {Number} min, max min: The minimum value, max: The maximum value
|
||||
* @param {Integer} tickCount The count of ticks
|
||||
* @param {Boolean} allowDecimals Allow the ticks to be decimals or not
|
||||
* @return {Array} ticks
|
||||
*/
|
||||
function getNiceTickValuesFn([min, max], tickCount = 6, allowDecimals = true) {
|
||||
// More than two ticks should be return
|
||||
const count = Math.max(tickCount, 2);
|
||||
const [cormin, cormax] = getValidInterval([min, max]);
|
||||
|
||||
if (cormin === -Infinity || cormax === Infinity) {
|
||||
const values = cormax === Infinity
|
||||
? [cormin, ...range(0, tickCount - 1).map(() => Infinity)]
|
||||
: [...range(0, tickCount - 1).map(() => -Infinity), cormax];
|
||||
|
||||
return min > max ? reverse(values) : values;
|
||||
}
|
||||
|
||||
if (cormin === cormax) {
|
||||
return getTickOfSingleValue(cormin, tickCount, allowDecimals);
|
||||
}
|
||||
|
||||
// Get the step between two ticks
|
||||
const { step, tickMin, tickMax } = calculateStep(cormin, cormax, count, allowDecimals);
|
||||
|
||||
const values = Arithmetic.rangeStep(tickMin, tickMax.add(new Decimal(0.1).mul(step)), step);
|
||||
|
||||
return min > max ? reverse(values) : values;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate the ticks of an interval, the count of ticks won't be guraranteed
|
||||
*
|
||||
* @param {Number} min, max min: The minimum value, max: The maximum value
|
||||
* @param {Integer} tickCount The count of ticks
|
||||
* @param {Boolean} allowDecimals Allow the ticks to be decimals or not
|
||||
* @return {Array} ticks
|
||||
*/
|
||||
function getTickValuesFn([min, max], tickCount = 6, allowDecimals = true) {
|
||||
// More than two ticks should be return
|
||||
const count = Math.max(tickCount, 2);
|
||||
const [cormin, cormax] = getValidInterval([min, max]);
|
||||
|
||||
if (cormin === -Infinity || cormax === Infinity) {
|
||||
return [min, max];
|
||||
}
|
||||
|
||||
if (cormin === cormax) {
|
||||
return getTickOfSingleValue(cormin, tickCount, allowDecimals);
|
||||
}
|
||||
|
||||
const step = getFormatStep(new Decimal(cormax).sub(cormin).div(count - 1), allowDecimals, 0);
|
||||
|
||||
const fn = compose(
|
||||
map((n) => new Decimal(cormin).add(new Decimal(n).mul(step)).toNumber()),
|
||||
range,
|
||||
);
|
||||
|
||||
const values = fn(0, count).filter((entry) => (entry >= cormin && entry <= cormax));
|
||||
|
||||
return min > max ? reverse(values) : values;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate the ticks of an interval, the count of ticks won't be guraranteed,
|
||||
* but the domain will be guaranteed
|
||||
*
|
||||
* @param {Number} min, max min: The minimum value, max: The maximum value
|
||||
* @param {Integer} tickCount The count of ticks
|
||||
* @param {Boolean} allowDecimals Allow the ticks to be decimals or not
|
||||
* @return {Array} ticks
|
||||
*/
|
||||
function getTickValuesFixedDomainFn([min, max], tickCount, allowDecimals = true) {
|
||||
// More than two ticks should be return
|
||||
const [cormin, cormax] = getValidInterval([min, max]);
|
||||
|
||||
if (cormin === -Infinity || cormax === Infinity) {
|
||||
return [min, max];
|
||||
}
|
||||
|
||||
if (cormin === cormax) { return [cormin]; }
|
||||
|
||||
const count = Math.max(tickCount, 2);
|
||||
const step = getFormatStep(new Decimal(cormax).sub(cormin).div(count - 1), allowDecimals, 0);
|
||||
const values = [
|
||||
...Arithmetic.rangeStep(
|
||||
new Decimal(cormin),
|
||||
new Decimal(cormax).sub(new Decimal(0.99).mul(step)),
|
||||
step,
|
||||
),
|
||||
cormax,
|
||||
];
|
||||
|
||||
return min > max ? reverse(values) : values;
|
||||
}
|
||||
|
||||
export const getNiceTickValues = memoize(getNiceTickValuesFn);
|
||||
export const getTickValues = memoize(getTickValuesFn);
|
||||
export const getTickValuesFixedDomain = memoize(getTickValuesFixedDomainFn);
|
||||
1
frontend/node_modules/recharts-scale/src/index.js
generated
vendored
Normal file
1
frontend/node_modules/recharts-scale/src/index.js
generated
vendored
Normal file
@ -0,0 +1 @@
|
||||
export { getTickValues, getNiceTickValues, getTickValuesFixedDomain } from './getNiceTickValues';
|
||||
108
frontend/node_modules/recharts-scale/src/util/arithmetic.js
generated
vendored
Normal file
108
frontend/node_modules/recharts-scale/src/util/arithmetic.js
generated
vendored
Normal file
@ -0,0 +1,108 @@
|
||||
/**
|
||||
* @fileOverview 一些公用的运算方法
|
||||
* @author xile611
|
||||
* @date 2015-09-17
|
||||
*/
|
||||
import Decimal from 'decimal.js-light';
|
||||
import { curry } from './utils';
|
||||
|
||||
/**
|
||||
* 获取数值的位数
|
||||
* 其中绝对值属于区间[0.1, 1), 得到的值为0
|
||||
* 绝对值属于区间[0.01, 0.1),得到的位数为 -1
|
||||
* 绝对值属于区间[0.001, 0.01),得到的位数为 -2
|
||||
*
|
||||
* @param {Number} value 数值
|
||||
* @return {Integer} 位数
|
||||
*/
|
||||
function getDigitCount(value) {
|
||||
let result;
|
||||
|
||||
if (value === 0) {
|
||||
result = 1;
|
||||
} else {
|
||||
result = Math.floor(new Decimal(value).abs().log(10)
|
||||
.toNumber()) + 1;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 按照固定的步长获取[start, end)这个区间的数据
|
||||
* 并且需要处理js计算精度的问题
|
||||
*
|
||||
* @param {Decimal} start 起点
|
||||
* @param {Decimal} end 终点,不包含该值
|
||||
* @param {Decimal} step 步长
|
||||
* @return {Array} 若干数值
|
||||
*/
|
||||
function rangeStep(start, end, step) {
|
||||
let num = new Decimal(start);
|
||||
let i = 0;
|
||||
const result = [];
|
||||
|
||||
// magic number to prevent infinite loop
|
||||
while (num.lt(end) && i < 100000) {
|
||||
result.push(num.toNumber());
|
||||
|
||||
num = num.add(step);
|
||||
i++;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
/**
|
||||
* 对数值进行线性插值
|
||||
*
|
||||
* @param {Number} a 定义域的极点
|
||||
* @param {Number} b 定义域的极点
|
||||
* @param {Number} t [0, 1]内的某个值
|
||||
* @return {Number} 定义域内的某个值
|
||||
*/
|
||||
const interpolateNumber = curry((a, b, t) => {
|
||||
const newA = +a;
|
||||
const newB = +b;
|
||||
|
||||
return newA + t * (newB - newA);
|
||||
});
|
||||
/**
|
||||
* 线性插值的逆运算
|
||||
*
|
||||
* @param {Number} a 定义域的极点
|
||||
* @param {Number} b 定义域的极点
|
||||
* @param {Number} x 可以认为是插值后的一个输出值
|
||||
* @return {Number} 当x在 a ~ b这个范围内时,返回值属于[0, 1]
|
||||
*/
|
||||
const uninterpolateNumber = curry((a, b, x) => {
|
||||
let diff = b - (+a);
|
||||
|
||||
diff = diff || Infinity;
|
||||
|
||||
return (x - a) / diff;
|
||||
});
|
||||
/**
|
||||
* 线性插值的逆运算,并且有截断的操作
|
||||
*
|
||||
* @param {Number} a 定义域的极点
|
||||
* @param {Number} b 定义域的极点
|
||||
* @param {Number} x 可以认为是插值后的一个输出值
|
||||
* @return {Number} 当x在 a ~ b这个区间内时,返回值属于[0, 1],
|
||||
* 当x不在 a ~ b这个区间时,会截断到 a ~ b 这个区间
|
||||
*/
|
||||
const uninterpolateTruncation = curry((a, b, x) => {
|
||||
let diff = b - (+a);
|
||||
|
||||
diff = diff || Infinity;
|
||||
|
||||
return Math.max(0, Math.min(1, (x - a) / diff));
|
||||
});
|
||||
|
||||
export default {
|
||||
rangeStep,
|
||||
getDigitCount,
|
||||
|
||||
interpolateNumber,
|
||||
uninterpolateNumber,
|
||||
uninterpolateTruncation,
|
||||
};
|
||||
94
frontend/node_modules/recharts-scale/src/util/utils.js
generated
vendored
Normal file
94
frontend/node_modules/recharts-scale/src/util/utils.js
generated
vendored
Normal file
@ -0,0 +1,94 @@
|
||||
const identity = (i) => i;
|
||||
|
||||
export const PLACE_HOLDER = {
|
||||
'@@functional/placeholder': true,
|
||||
};
|
||||
|
||||
const isPlaceHolder = (val) => val === PLACE_HOLDER;
|
||||
|
||||
const curry0 = (fn) => function _curried(...args) {
|
||||
if (args.length === 0 || args.length === 1 && isPlaceHolder(args[0])) {
|
||||
return _curried;
|
||||
}
|
||||
|
||||
return fn(...args);
|
||||
};
|
||||
|
||||
const curryN = (n, fn) => {
|
||||
if (n === 1) {
|
||||
return fn;
|
||||
}
|
||||
|
||||
return curry0((...args) => {
|
||||
const argsLength = args.filter((arg) => arg !== PLACE_HOLDER).length;
|
||||
|
||||
if (argsLength >= n) {
|
||||
return fn(...args);
|
||||
}
|
||||
|
||||
return curryN(n - argsLength, curry0((...restArgs) => {
|
||||
const newArgs = args.map((arg) => (isPlaceHolder(arg) ? restArgs.shift() : arg));
|
||||
|
||||
return fn(...newArgs, ...restArgs);
|
||||
}));
|
||||
});
|
||||
};
|
||||
|
||||
export const curry = (fn) => curryN(fn.length, fn);
|
||||
|
||||
export const range = (begin, end) => {
|
||||
const arr = [];
|
||||
|
||||
for (let i = begin; i < end; ++i) {
|
||||
arr[i - begin] = i;
|
||||
}
|
||||
|
||||
return arr;
|
||||
};
|
||||
|
||||
export const map = curry((fn, arr) => {
|
||||
if (Array.isArray(arr)) {
|
||||
return arr.map(fn);
|
||||
}
|
||||
|
||||
return Object.keys(arr).map((key) => arr[key]).map(fn);
|
||||
});
|
||||
|
||||
export const compose = (...args) => {
|
||||
if (!args.length) {
|
||||
return identity;
|
||||
}
|
||||
|
||||
const fns = args.reverse();
|
||||
// first function can receive multiply arguments
|
||||
const firstFn = fns[0];
|
||||
const tailsFn = fns.slice(1);
|
||||
|
||||
return (...composeArgs) => tailsFn.reduce((res, fn) => fn(res),
|
||||
firstFn(...composeArgs));
|
||||
};
|
||||
|
||||
export const reverse = (arr) => {
|
||||
if (Array.isArray(arr)) {
|
||||
return arr.reverse();
|
||||
}
|
||||
|
||||
// can be string
|
||||
return arr.split('').reverse.join('');
|
||||
};
|
||||
|
||||
export const memoize = (fn) => {
|
||||
let lastArgs = null;
|
||||
let lastResult = null;
|
||||
|
||||
return (...args) => {
|
||||
if (lastArgs && args.every((val, i) => val === lastArgs[i])) {
|
||||
return lastResult;
|
||||
}
|
||||
|
||||
lastArgs = args;
|
||||
lastResult = fn(...args);
|
||||
|
||||
return lastResult;
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user