| 1 | "use strict";
|
|---|
| 2 |
|
|---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
|---|
| 4 | value: true
|
|---|
| 5 | });
|
|---|
| 6 | exports.getValidInterval = exports.getTickValuesFixedDomain = exports.getTickOfSingleValue = exports.getNiceTickValues = exports.getFormatStep = exports.calculateStep = void 0;
|
|---|
| 7 | var _decimal = _interopRequireDefault(require("decimal.js-light"));
|
|---|
| 8 | var _arithmetic = require("./util/arithmetic");
|
|---|
| 9 | function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|---|
| 10 | /**
|
|---|
| 11 | * @fileOverview calculate tick values of scale
|
|---|
| 12 | * @author xile611, arcthur
|
|---|
| 13 | * @date 2015-09-17
|
|---|
| 14 | */
|
|---|
| 15 |
|
|---|
| 16 | /**
|
|---|
| 17 | * Calculate a interval of a minimum value and a maximum value
|
|---|
| 18 | *
|
|---|
| 19 | * @param {Number} min The minimum value
|
|---|
| 20 | * @param {Number} max The maximum value
|
|---|
| 21 | * @return {Array} An interval
|
|---|
| 22 | */
|
|---|
| 23 | var getValidInterval = _ref => {
|
|---|
| 24 | var [min, max] = _ref;
|
|---|
| 25 | var [validMin, validMax] = [min, max];
|
|---|
| 26 |
|
|---|
| 27 | // exchange
|
|---|
| 28 | if (min > max) {
|
|---|
| 29 | [validMin, validMax] = [max, min];
|
|---|
| 30 | }
|
|---|
| 31 | return [validMin, validMax];
|
|---|
| 32 | };
|
|---|
| 33 |
|
|---|
| 34 | /**
|
|---|
| 35 | * Calculate the step which is easy to understand between ticks, like 10, 20, 25
|
|---|
| 36 | *
|
|---|
| 37 | * @param roughStep The rough step calculated by dividing the difference by the tickCount
|
|---|
| 38 | * @param allowDecimals Allow the ticks to be decimals or not
|
|---|
| 39 | * @param correctionFactor A correction factor
|
|---|
| 40 | * @return The step which is easy to understand between two ticks
|
|---|
| 41 | */
|
|---|
| 42 | exports.getValidInterval = getValidInterval;
|
|---|
| 43 | var getFormatStep = (roughStep, allowDecimals, correctionFactor) => {
|
|---|
| 44 | if (roughStep.lte(0)) {
|
|---|
| 45 | return new _decimal.default(0);
|
|---|
| 46 | }
|
|---|
| 47 | var digitCount = (0, _arithmetic.getDigitCount)(roughStep.toNumber());
|
|---|
| 48 | // The ratio between the rough step and the smallest number which has a bigger
|
|---|
| 49 | // order of magnitudes than the rough step
|
|---|
| 50 | var digitCountValue = new _decimal.default(10).pow(digitCount);
|
|---|
| 51 | var stepRatio = roughStep.div(digitCountValue);
|
|---|
| 52 | // When an integer and a float multiplied, the accuracy of result may be wrong
|
|---|
| 53 | var stepRatioScale = digitCount !== 1 ? 0.05 : 0.1;
|
|---|
| 54 | var amendStepRatio = new _decimal.default(Math.ceil(stepRatio.div(stepRatioScale).toNumber())).add(correctionFactor).mul(stepRatioScale);
|
|---|
| 55 | var formatStep = amendStepRatio.mul(digitCountValue);
|
|---|
| 56 | return allowDecimals ? new _decimal.default(formatStep.toNumber()) : new _decimal.default(Math.ceil(formatStep.toNumber()));
|
|---|
| 57 | };
|
|---|
| 58 |
|
|---|
| 59 | /**
|
|---|
| 60 | * calculate the ticks when the minimum value equals to the maximum value
|
|---|
| 61 | *
|
|---|
| 62 | * @param value The minimum value which is also the maximum value
|
|---|
| 63 | * @param tickCount The count of ticks
|
|---|
| 64 | * @param allowDecimals Allow the ticks to be decimals or not
|
|---|
| 65 | * @return array of ticks
|
|---|
| 66 | */
|
|---|
| 67 | exports.getFormatStep = getFormatStep;
|
|---|
| 68 | var getTickOfSingleValue = (value, tickCount, allowDecimals) => {
|
|---|
| 69 | var step = new _decimal.default(1);
|
|---|
| 70 | // calculate the middle value of ticks
|
|---|
| 71 | var middle = new _decimal.default(value);
|
|---|
| 72 | if (!middle.isint() && allowDecimals) {
|
|---|
| 73 | var absVal = Math.abs(value);
|
|---|
| 74 | if (absVal < 1) {
|
|---|
| 75 | // The step should be a float number when the difference is smaller than 1
|
|---|
| 76 | step = new _decimal.default(10).pow((0, _arithmetic.getDigitCount)(value) - 1);
|
|---|
| 77 | middle = new _decimal.default(Math.floor(middle.div(step).toNumber())).mul(step);
|
|---|
| 78 | } else if (absVal > 1) {
|
|---|
| 79 | // Return the maximum integer which is smaller than 'value' when 'value' is greater than 1
|
|---|
| 80 | middle = new _decimal.default(Math.floor(value));
|
|---|
| 81 | }
|
|---|
| 82 | } else if (value === 0) {
|
|---|
| 83 | middle = new _decimal.default(Math.floor((tickCount - 1) / 2));
|
|---|
| 84 | } else if (!allowDecimals) {
|
|---|
| 85 | middle = new _decimal.default(Math.floor(value));
|
|---|
| 86 | }
|
|---|
| 87 | var middleIndex = Math.floor((tickCount - 1) / 2);
|
|---|
| 88 | var ticks = [];
|
|---|
| 89 | for (var i = 0; i < tickCount; i++) {
|
|---|
| 90 | ticks.push(middle.add(new _decimal.default(i - middleIndex).mul(step)).toNumber());
|
|---|
| 91 | }
|
|---|
| 92 | return ticks;
|
|---|
| 93 | };
|
|---|
| 94 |
|
|---|
| 95 | /**
|
|---|
| 96 | * Calculate the step
|
|---|
| 97 | *
|
|---|
| 98 | * @param min The minimum value of an interval
|
|---|
| 99 | * @param max The maximum value of an interval
|
|---|
| 100 | * @param tickCount The count of ticks
|
|---|
| 101 | * @param allowDecimals Allow the ticks to be decimals or not
|
|---|
| 102 | * @param correctionFactor A correction factor
|
|---|
| 103 | * @return The step, minimum value of ticks, maximum value of ticks
|
|---|
| 104 | */
|
|---|
| 105 | exports.getTickOfSingleValue = getTickOfSingleValue;
|
|---|
| 106 | var _calculateStep = exports.calculateStep = function calculateStep(min, max, tickCount, allowDecimals) {
|
|---|
| 107 | var correctionFactor = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 0;
|
|---|
| 108 | // dirty hack (for recharts' test)
|
|---|
| 109 | if (!Number.isFinite((max - min) / (tickCount - 1))) {
|
|---|
| 110 | return {
|
|---|
| 111 | step: new _decimal.default(0),
|
|---|
| 112 | tickMin: new _decimal.default(0),
|
|---|
| 113 | tickMax: new _decimal.default(0)
|
|---|
| 114 | };
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | // The step which is easy to understand between two ticks
|
|---|
| 118 | var step = getFormatStep(new _decimal.default(max).sub(min).div(tickCount - 1), allowDecimals, correctionFactor);
|
|---|
| 119 |
|
|---|
| 120 | // A medial value of ticks
|
|---|
| 121 | var middle;
|
|---|
| 122 |
|
|---|
| 123 | // When 0 is inside the interval, 0 should be a tick
|
|---|
| 124 | if (min <= 0 && max >= 0) {
|
|---|
| 125 | middle = new _decimal.default(0);
|
|---|
| 126 | } else {
|
|---|
| 127 | // calculate the middle value
|
|---|
| 128 | middle = new _decimal.default(min).add(max).div(2);
|
|---|
| 129 | // minus modulo value
|
|---|
| 130 | middle = middle.sub(new _decimal.default(middle).mod(step));
|
|---|
| 131 | }
|
|---|
| 132 | var belowCount = Math.ceil(middle.sub(min).div(step).toNumber());
|
|---|
| 133 | var upCount = Math.ceil(new _decimal.default(max).sub(middle).div(step).toNumber());
|
|---|
| 134 | var scaleCount = belowCount + upCount + 1;
|
|---|
| 135 | if (scaleCount > tickCount) {
|
|---|
| 136 | // When more ticks need to cover the interval, step should be bigger.
|
|---|
| 137 | return _calculateStep(min, max, tickCount, allowDecimals, correctionFactor + 1);
|
|---|
| 138 | }
|
|---|
| 139 | if (scaleCount < tickCount) {
|
|---|
| 140 | // When less ticks can cover the interval, we should add some additional ticks
|
|---|
| 141 | upCount = max > 0 ? upCount + (tickCount - scaleCount) : upCount;
|
|---|
| 142 | belowCount = max > 0 ? belowCount : belowCount + (tickCount - scaleCount);
|
|---|
| 143 | }
|
|---|
| 144 | return {
|
|---|
| 145 | step,
|
|---|
| 146 | tickMin: middle.sub(new _decimal.default(belowCount).mul(step)),
|
|---|
| 147 | tickMax: middle.add(new _decimal.default(upCount).mul(step))
|
|---|
| 148 | };
|
|---|
| 149 | };
|
|---|
| 150 |
|
|---|
| 151 | /**
|
|---|
| 152 | * Calculate the ticks of an interval. Ticks can appear outside the interval
|
|---|
| 153 | * if it makes them more rounded and nice.
|
|---|
| 154 | *
|
|---|
| 155 | * @param tuple of [min,max] min: The minimum value, max: The maximum value
|
|---|
| 156 | * @param tickCount The count of ticks
|
|---|
| 157 | * @param allowDecimals Allow the ticks to be decimals or not
|
|---|
| 158 | * @return array of ticks
|
|---|
| 159 | */
|
|---|
| 160 | var getNiceTickValues = exports.getNiceTickValues = function getNiceTickValues(_ref2) {
|
|---|
| 161 | var [min, max] = _ref2;
|
|---|
| 162 | var tickCount = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 6;
|
|---|
| 163 | var allowDecimals = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
|
|---|
| 164 | // More than two ticks should be return
|
|---|
| 165 | var count = Math.max(tickCount, 2);
|
|---|
| 166 | var [cormin, cormax] = getValidInterval([min, max]);
|
|---|
| 167 | if (cormin === -Infinity || cormax === Infinity) {
|
|---|
| 168 | var _values = cormax === Infinity ? [cormin, ...Array(tickCount - 1).fill(Infinity)] : [...Array(tickCount - 1).fill(-Infinity), cormax];
|
|---|
| 169 | return min > max ? _values.reverse() : _values;
|
|---|
| 170 | }
|
|---|
| 171 | if (cormin === cormax) {
|
|---|
| 172 | return getTickOfSingleValue(cormin, tickCount, allowDecimals);
|
|---|
| 173 | }
|
|---|
| 174 |
|
|---|
| 175 | // Get the step between two ticks
|
|---|
| 176 | var {
|
|---|
| 177 | step,
|
|---|
| 178 | tickMin,
|
|---|
| 179 | tickMax
|
|---|
| 180 | } = _calculateStep(cormin, cormax, count, allowDecimals, 0);
|
|---|
| 181 | var values = (0, _arithmetic.rangeStep)(tickMin, tickMax.add(new _decimal.default(0.1).mul(step)), step);
|
|---|
| 182 | return min > max ? values.reverse() : values;
|
|---|
| 183 | };
|
|---|
| 184 |
|
|---|
| 185 | /**
|
|---|
| 186 | * Calculate the ticks of an interval.
|
|---|
| 187 | * Ticks will be constrained to the interval [min, max] even if it makes them less rounded and nice.
|
|---|
| 188 | *
|
|---|
| 189 | * @param tuple of [min,max] min: The minimum value, max: The maximum value
|
|---|
| 190 | * @param tickCount The count of ticks. This function may return less than tickCount ticks if the interval is too small.
|
|---|
| 191 | * @param allowDecimals Allow the ticks to be decimals or not
|
|---|
| 192 | * @return array of ticks
|
|---|
| 193 | */
|
|---|
| 194 | var getTickValuesFixedDomain = exports.getTickValuesFixedDomain = function getTickValuesFixedDomain(_ref3, tickCount) {
|
|---|
| 195 | var [min, max] = _ref3;
|
|---|
| 196 | var allowDecimals = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
|
|---|
| 197 | // More than two ticks should be return
|
|---|
| 198 | var [cormin, cormax] = getValidInterval([min, max]);
|
|---|
| 199 | if (cormin === -Infinity || cormax === Infinity) {
|
|---|
| 200 | return [min, max];
|
|---|
| 201 | }
|
|---|
| 202 | if (cormin === cormax) {
|
|---|
| 203 | return [cormin];
|
|---|
| 204 | }
|
|---|
| 205 | var count = Math.max(tickCount, 2);
|
|---|
| 206 | var step = getFormatStep(new _decimal.default(cormax).sub(cormin).div(count - 1), allowDecimals, 0);
|
|---|
| 207 | var values = [...(0, _arithmetic.rangeStep)(new _decimal.default(cormin), new _decimal.default(cormax), step), cormax];
|
|---|
| 208 | if (allowDecimals === false) {
|
|---|
| 209 | /*
|
|---|
| 210 | * allowDecimals is false means that we want to have integer ticks.
|
|---|
| 211 | * The step is guaranteed to be an integer in the code above which is great start
|
|---|
| 212 | * but when the first step is not an integer, it will start stepping from a decimal value anyway.
|
|---|
| 213 | * So we need to round all the values to integers after the fact.
|
|---|
| 214 | */
|
|---|
| 215 | values = values.map(value => Math.round(value));
|
|---|
| 216 | }
|
|---|
| 217 | return min > max ? values.reverse() : values;
|
|---|
| 218 | }; |
|---|