Replaced array values to variables yLeft and yRight for easy reading code.

This commit is contained in:
ilgizar 2018-03-12 23:12:45 +05:00
parent 11ae926388
commit 8c82e5701c

View File

@ -6,112 +6,118 @@ import _ from 'lodash';
* @param align Y level * @param align Y level
*/ */
export function alignYLevel(yaxis, alignLevel) { export function alignYLevel(yaxis, alignLevel) {
moveLevelToZero(yaxis, alignLevel); var [yLeft, yRight] = yaxis;
moveLevelToZero(yLeft, yRight, alignLevel);
expandStuckValues(yaxis); expandStuckValues(yLeft, yRight);
// one of graphs on zero // one of graphs on zero
var zero = yaxis[0].min === 0 || yaxis[1].min === 0 || yaxis[0].max === 0 || yaxis[1].max === 0; var zero = yLeft.min === 0 || yRight.min === 0 || yLeft.max === 0 || yRight.max === 0;
var oneSide = checkOneSide(yaxis); var oneSide = checkOneSide(yLeft, yRight);
if (zero && oneSide) { if (zero && oneSide) {
yaxis[0].min = yaxis[0].max > 0 ? 0 : yaxis[0].min; yLeft.min = yLeft.max > 0 ? 0 : yLeft.min;
yaxis[0].max = yaxis[0].max > 0 ? yaxis[0].max : 0; yLeft.max = yLeft.max > 0 ? yLeft.max : 0;
yaxis[1].min = yaxis[1].max > 0 ? 0 : yaxis[1].min; yRight.min = yRight.max > 0 ? 0 : yRight.min;
yaxis[1].max = yaxis[1].max > 0 ? yaxis[1].max : 0; yRight.max = yRight.max > 0 ? yRight.max : 0;
} else { } else {
// on the opposite sides with respect to zero if (checkOppositeSides(yLeft, yRight)) {
if ((yaxis[0].min >= 0 && yaxis[1].max <= 0) || (yaxis[0].max <= 0 && yaxis[1].min >= 0)) { if (yLeft.min >= 0) {
if (yaxis[0].min >= 0) { yLeft.min = -yLeft.max;
yaxis[0].min = -yaxis[0].max; yRight.max = -yRight.min;
yaxis[1].max = -yaxis[1].min;
} else { } else {
yaxis[0].max = -yaxis[0].min; yLeft.max = -yLeft.min;
yaxis[1].min = -yaxis[1].max; yRight.min = -yRight.max;
} }
} else { } else {
var rate = getRate(yaxis); var rate = getRate(yLeft, yRight);
if (oneSide) { if (oneSide) {
if (yaxis[0].min > 0) { // all graphs above the Y level
yaxis[0].min = yaxis[0].max / rate; if (yLeft.min > 0) {
yaxis[1].min = yaxis[1].max / rate; yLeft.min = yLeft.max / rate;
yRight.min = yRight.max / rate;
} else { } else {
yaxis[0].max = yaxis[0].min / rate; yLeft.max = yLeft.min / rate;
yaxis[1].max = yaxis[1].min / rate; yRight.max = yRight.min / rate;
} }
} else { } else {
if (checkTwoCross(yaxis)) { if (checkTwoCross(yLeft, yRight)) {
yaxis[0].min = yaxis[1].min ? yaxis[1].min * rate : yaxis[0].min; yLeft.min = yRight.min ? yRight.min * rate : yLeft.min;
yaxis[1].min = yaxis[0].min ? yaxis[0].min / rate : yaxis[1].min; yRight.min = yLeft.min ? yLeft.min / rate : yRight.min;
yaxis[0].max = yaxis[1].max ? yaxis[1].max * rate : yaxis[0].max; yLeft.max = yRight.max ? yRight.max * rate : yLeft.max;
yaxis[1].max = yaxis[0].max ? yaxis[0].max / rate : yaxis[1].max; yRight.max = yLeft.max ? yLeft.max / rate : yRight.max;
} else { } else {
yaxis[0].min = yaxis[0].min > 0 ? yaxis[1].min * rate : yaxis[0].min; yLeft.min = yLeft.min > 0 ? yRight.min * rate : yLeft.min;
yaxis[1].min = yaxis[1].min > 0 ? yaxis[0].min / rate : yaxis[1].min; yRight.min = yRight.min > 0 ? yLeft.min / rate : yRight.min;
yaxis[0].max = yaxis[0].max < 0 ? yaxis[1].max * rate : yaxis[0].max; yLeft.max = yLeft.max < 0 ? yRight.max * rate : yLeft.max;
yaxis[1].max = yaxis[1].max < 0 ? yaxis[0].max / rate : yaxis[1].max; yRight.max = yRight.max < 0 ? yLeft.max / rate : yRight.max;
} }
} }
} }
} }
restoreLevelFromZero(yaxis, alignLevel); restoreLevelFromZero(yLeft, yRight, alignLevel);
} }
function expandStuckValues(yaxis) { function expandStuckValues(yLeft, yRight) {
// wide Y min and max using increased wideFactor // wide Y min and max using increased wideFactor
var wideFactor = 0.25; var wideFactor = 0.25;
if (yaxis[0].max === yaxis[0].min) { if (yLeft.max === yLeft.min) {
yaxis[0].min -= wideFactor; yLeft.min -= wideFactor;
yaxis[0].max += wideFactor; yLeft.max += wideFactor;
} }
if (yaxis[1].max === yaxis[1].min) { if (yRight.max === yRight.min) {
yaxis[1].min -= wideFactor; yRight.min -= wideFactor;
yaxis[1].max += wideFactor; yRight.max += wideFactor;
} }
} }
function moveLevelToZero(yaxis, alignLevel) { function moveLevelToZero(yLeft, yRight, alignLevel) {
if (alignLevel !== 0) { if (alignLevel !== 0) {
yaxis[0].min -= alignLevel; yLeft.min -= alignLevel;
yaxis[0].max -= alignLevel; yLeft.max -= alignLevel;
yaxis[1].min -= alignLevel; yRight.min -= alignLevel;
yaxis[1].max -= alignLevel; yRight.max -= alignLevel;
} }
} }
function restoreLevelFromZero(yaxis, alignLevel) { function restoreLevelFromZero(yLeft, yRight, alignLevel) {
if (alignLevel !== 0) { if (alignLevel !== 0) {
yaxis[0].min += alignLevel; yLeft.min += alignLevel;
yaxis[0].max += alignLevel; yLeft.max += alignLevel;
yaxis[1].min += alignLevel; yRight.min += alignLevel;
yaxis[1].max += alignLevel; yRight.max += alignLevel;
} }
} }
function checkOneSide(yaxis) { function checkOneSide(yLeft, yRight) {
// on the one hand with respect to zero // on the one hand with respect to zero
return (yaxis[0].min >= 0 && yaxis[1].min >= 0) || (yaxis[0].max <= 0 && yaxis[1].max <= 0); return (yLeft.min >= 0 && yRight.min >= 0) || (yLeft.max <= 0 && yRight.max <= 0);
} }
function checkTwoCross(yaxis) { function checkTwoCross(yLeft, yRight) {
// both across zero // both across zero
return yaxis[0].min <= 0 && yaxis[0].max >= 0 && yaxis[1].min <= 0 && yaxis[1].max >= 0; return yLeft.min <= 0 && yLeft.max >= 0 && yRight.min <= 0 && yRight.max >= 0;
} }
function getRate(yaxis) { function checkOppositeSides(yLeft, yRight) {
// on the opposite sides with respect to zero
return (yLeft.min >= 0 && yRight.max <= 0) || (yLeft.max <= 0 && yRight.min >= 0);
}
function getRate(yLeft, yRight) {
var rateLeft, rateRight, rate; var rateLeft, rateRight, rate;
if (checkTwoCross(yaxis)) { if (checkTwoCross(yLeft, yRight)) {
rateLeft = yaxis[1].min ? yaxis[0].min / yaxis[1].min : 0; rateLeft = yRight.min ? yLeft.min / yRight.min : 0;
rateRight = yaxis[1].max ? yaxis[0].max / yaxis[1].max : 0; rateRight = yRight.max ? yLeft.max / yRight.max : 0;
} else { } else {
if (checkOneSide(yaxis)) { if (checkOneSide(yLeft, yRight)) {
var absLeftMin = Math.abs(yaxis[0].min); var absLeftMin = Math.abs(yLeft.min);
var absLeftMax = Math.abs(yaxis[0].max); var absLeftMax = Math.abs(yLeft.max);
var absRightMin = Math.abs(yaxis[1].min); var absRightMin = Math.abs(yRight.min);
var absRightMax = Math.abs(yaxis[1].max); var absRightMax = Math.abs(yRight.max);
var upLeft = _.max([absLeftMin, absLeftMax]); var upLeft = _.max([absLeftMin, absLeftMax]);
var downLeft = _.min([absLeftMin, absLeftMax]); var downLeft = _.min([absLeftMin, absLeftMax]);
var upRight = _.max([absRightMin, absRightMax]); var upRight = _.max([absRightMin, absRightMax]);
@ -120,12 +126,12 @@ function getRate(yaxis) {
rateLeft = downLeft ? upLeft / downLeft : upLeft; rateLeft = downLeft ? upLeft / downLeft : upLeft;
rateRight = downRight ? upRight / downRight : upRight; rateRight = downRight ? upRight / downRight : upRight;
} else { } else {
if (yaxis[0].min > 0 || yaxis[1].min > 0) { if (yLeft.min > 0 || yRight.min > 0) {
rateLeft = yaxis[0].max / yaxis[1].max; rateLeft = yLeft.max / yRight.max;
rateRight = 0; rateRight = 0;
} else { } else {
rateLeft = 0; rateLeft = 0;
rateRight = yaxis[0].min / yaxis[1].min; rateRight = yLeft.min / yRight.min;
} }
} }
} }