| | |
| | |
| | |
| | |
| | |
| | import Decimal from 'decimal.js-light'; |
| | import { compose, range, memoize, map, reverse } from './util/utils'; |
| | import { getDigitCount, rangeStep } from './util/arithmetic'; |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | export const getValidInterval = ([min, max]: [number, number]) => { |
| | let [validMin, validMax] = [min, max]; |
| |
|
| | |
| | if (min > max) { |
| | [validMin, validMax] = [max, min]; |
| | } |
| |
|
| | return [validMin, validMax]; |
| | }; |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | export const getFormatStep = (roughStep: Decimal, allowDecimals: boolean, correctionFactor: number) => { |
| | if (roughStep.lte(0)) { |
| | return new Decimal(0); |
| | } |
| |
|
| | const digitCount = getDigitCount(roughStep.toNumber()); |
| | |
| | |
| | const digitCountValue = new Decimal(10).pow(digitCount); |
| | const stepRatio = roughStep.div(digitCountValue); |
| | |
| | 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 ? new Decimal(formatStep.toNumber()) : new Decimal(Math.ceil(formatStep.toNumber())); |
| | }; |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | export const getTickOfSingleValue = (value: number, tickCount: number, allowDecimals: boolean): Array<number> => { |
| | let step: Decimal = new Decimal(1); |
| | |
| | let middle = new Decimal(value); |
| |
|
| | if (!middle.isint() && allowDecimals) { |
| | const absVal = Math.abs(value); |
| |
|
| | if (absVal < 1) { |
| | |
| | step = new Decimal(10).pow(getDigitCount(value) - 1); |
| |
|
| | middle = new Decimal(Math.floor(middle.div(step).toNumber())).mul(step); |
| | } else if (absVal > 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: number) => middle.add(new Decimal(n - middleIndex).mul(step)).toNumber()), |
| | range, |
| | ); |
| |
|
| | return fn(0, tickCount); |
| | }; |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | export const calculateStep = ( |
| | min: number, |
| | max: number, |
| | tickCount: number, |
| | allowDecimals: boolean, |
| | correctionFactor: number = 0, |
| | ): { |
| | step: Decimal; |
| | tickMin: Decimal; |
| | tickMax: Decimal; |
| | } => { |
| | |
| | if (!Number.isFinite((max - min) / (tickCount - 1))) { |
| | return { |
| | step: new Decimal(0), |
| | tickMin: new Decimal(0), |
| | tickMax: new Decimal(0), |
| | }; |
| | } |
| |
|
| | |
| | const step = getFormatStep(new Decimal(max).sub(min).div(tickCount - 1), allowDecimals, correctionFactor); |
| |
|
| | |
| | let middle; |
| |
|
| | |
| | if (min <= 0 && max >= 0) { |
| | middle = new Decimal(0); |
| | } else { |
| | |
| | middle = new Decimal(min).add(max).div(2); |
| | |
| | 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) { |
| | |
| | return calculateStep(min, max, tickCount, allowDecimals, correctionFactor + 1); |
| | } |
| | if (scaleCount < tickCount) { |
| | |
| | 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)), |
| | }; |
| | }; |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | function getNiceTickValuesFn([min, max]: [number, number], tickCount = 6, allowDecimals = true): number[] { |
| | |
| | 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); |
| | } |
| |
|
| | |
| | const { step, tickMin, tickMax } = calculateStep(cormin, cormax, count, allowDecimals, 0); |
| |
|
| | const values = rangeStep(tickMin, tickMax.add(new Decimal(0.1).mul(step)), step); |
| |
|
| | return min > max ? reverse(values) : values; |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | function getTickValuesFixedDomainFn([min, max]: readonly [number, number], tickCount: number, allowDecimals = true) { |
| | |
| | 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); |
| | let values = [...rangeStep(new Decimal(cormin), new Decimal(cormax), step), cormax]; |
| |
|
| | if (allowDecimals === false) { |
| | |
| | |
| | |
| | |
| | |
| | |
| | values = values.map(value => Math.round(value)); |
| | } |
| |
|
| | return min > max ? reverse(values) : values; |
| | } |
| |
|
| | export const getNiceTickValues = memoize(getNiceTickValuesFn); |
| | export const getTickValuesFixedDomain = memoize(getTickValuesFixedDomainFn); |
| |
|