Skip to content

Commit

Permalink
Merge pull request #307 from forio/develop
Browse files Browse the repository at this point in the history
Release build 2.1.0
  • Loading branch information
safetybelt authored Aug 14, 2024
2 parents 7c2d67b + a6c3641 commit c7a159c
Showing 1 changed file with 28 additions and 34 deletions.
62 changes: 28 additions & 34 deletions src/scripts/utils/contour-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -473,14 +473,12 @@ export const roundToNextTick = function (num) {
};

export const niceMinMax = function (min, max, ticks, startAtZero) {
// return divFloat(Math.ceil(mulFloat(value, Math.pow(10, digits))), Math.pow(10, digits));

// check for errors... min cannot be > max
if (min > max) {
return {
min: min,
max: min,
tickValues: []
tickValues: [],
};
}

Expand Down Expand Up @@ -513,29 +511,32 @@ export const niceMinMax = function (min, max, ticks, startAtZero) {
if (startAtZero == null) {
startAtZero = min === 0 || origMax < 0;
}

// if min and max are both above/below 0 and startAtZero is true, set the min or max to 0
if (startAtZero && max < 0) {
max = 0;
} else if (startAtZero && min > 0) {
min = 0;
}

var exponent;
if (min === max) {
if (max === 0) {
exponent = -1.0;
exponent = -1;
} else {
exponent = log10(Math.abs(max));
}
} else {
if(startAtZero && Math.abs(max) > 0) {
exponent = log10(Math.abs(max)) - 0.5;
var absMax = Math.max(Math.abs(max), Math.abs(min));
if (startAtZero && absMax > 0) {
exponent = log10(absMax) - 0.5;
} else {
exponent = log10(max-min) - 0.5;
exponent = log10(max - min) - 0.5;
}
}

var defaultRounding = -(exponent >= 0 ? trunc(exponent) : Math.floor(exponent));

// var defaultRounding = -trunc((min === max ?
// max === 0 ? -1.0 : log10(Math.abs(max)) :
// startAtZero ? log10(Math.abs(max)) : log10(max-min)
// ) - 0.5);

var excelRoundUp = function (value, up) {
up = up != null ? up : 0;
var roundFn = function (v) { return v >= 0 ? Math.ceil(v) : Math.floor(v); };
Expand All @@ -546,36 +547,28 @@ export const niceMinMax = function (min, max, ticks, startAtZero) {
var negativeMinAmount = excelRoundUp(Math.max(0, -min) / ticks, defaultRounding - 1);

var intermediateMax = min === max ? max === 0 ? 1 : excelRoundUp(max + negativeMinAmount, defaultRounding)
: excelRoundUp(max + negativeMinAmount,defaultRounding);

var iMin = 0;
if ((!startAtZero && min !== max) || max === 0) {
var inter = min + negativeMinAmount;
var dig = digits(inter);
var roundToDigits;
if (inter > 0) {
roundToDigits = -Math.floor(log10(inter));
} else {
roundToDigits = (Math.max(1, Math.abs(dig-2)));
}

iMin = -roundTo(-inter, roundToDigits);
iMin = iMin === 0 ? 0 : iMin;
// old version:
// iMin = excelRound(min + negativeMinAmount, defaultRounding + (min < 0 ? 1 : 0))
: excelRoundUp(max + negativeMinAmount, defaultRounding);

var intermediateMin = 0;
var inter = min + negativeMinAmount;
var dig = digits(inter);
var roundToDigits;
if (inter > 0) {
roundToDigits = -Math.floor(log10(inter));
} else {
roundToDigits = Math.max(1, Math.abs(dig - 2));
}

var intermediateMin = iMin;
intermediateMin = -roundTo(-inter, roundToDigits);

var interval = excelRoundUp(divFloat(subFloat(intermediateMax, intermediateMin),ticks), defaultRounding);
var interval = excelRoundUp(divFloat(subFloat(intermediateMax, intermediateMin), ticks), defaultRounding);
var finalMin = subFloat(intermediateMin, negativeMinAmount);
var finalMax = addFloat(finalMin, mulFloat(ticks, interval));
var ticksValues = [finalMin];
var prevTick = finalMin;

for (var j=1; j < ticks; j++) {
for (var j = 1; j < ticks; j++) {
var newTick = addFloat(prevTick, interval);

ticksValues.push(newTick);
prevTick = newTick;
}
Expand All @@ -588,7 +581,7 @@ export const niceMinMax = function (min, max, ticks, startAtZero) {
return {
min: swap ? -finalMax : finalMin,
max: swap ? -finalMin : finalMax,
tickValues: ticksValues.map(function (a) { return swap ? -a : a; })
tickValues: ticksValues.map(function (a) { return swap ? -a : a; }),
};
};

Expand All @@ -602,6 +595,7 @@ export const niceMinMax = function (min, max, ticks, startAtZero) {
return tick === Math.round(tick);
});
});

return foundSomethingRound ? minMax : defaultMinMax;
};

Expand Down

0 comments on commit c7a159c

Please sign in to comment.