align_yaxes.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. import _ from 'lodash';
  2. /**
  3. * To align two Y axes by Y level
  4. * @param yAxes data [{min: min_y1, min: max_y1}, {min: min_y2, max: max_y2}]
  5. * @param level Y level
  6. */
  7. export function alignYLevel(yAxes, level) {
  8. if (isNaN(level) || !checkCorrectAxis(yAxes)) {
  9. return;
  10. }
  11. var [yLeft, yRight] = yAxes;
  12. moveLevelToZero(yLeft, yRight, level);
  13. expandStuckValues(yLeft, yRight);
  14. // one of graphs on zero
  15. var zero = yLeft.min === 0 || yRight.min === 0 || yLeft.max === 0 || yRight.max === 0;
  16. var oneSide = checkOneSide(yLeft, yRight);
  17. if (zero && oneSide) {
  18. yLeft.min = yLeft.max > 0 ? 0 : yLeft.min;
  19. yLeft.max = yLeft.max > 0 ? yLeft.max : 0;
  20. yRight.min = yRight.max > 0 ? 0 : yRight.min;
  21. yRight.max = yRight.max > 0 ? yRight.max : 0;
  22. } else {
  23. if (checkOppositeSides(yLeft, yRight)) {
  24. if (yLeft.min >= 0) {
  25. yLeft.min = -yLeft.max;
  26. yRight.max = -yRight.min;
  27. } else {
  28. yLeft.max = -yLeft.min;
  29. yRight.min = -yRight.max;
  30. }
  31. } else {
  32. var rate = getRate(yLeft, yRight);
  33. if (oneSide) {
  34. // all graphs above the Y level
  35. if (yLeft.min > 0) {
  36. yLeft.min = yLeft.max / rate;
  37. yRight.min = yRight.max / rate;
  38. } else {
  39. yLeft.max = yLeft.min / rate;
  40. yRight.max = yRight.min / rate;
  41. }
  42. } else {
  43. if (checkTwoCross(yLeft, yRight)) {
  44. yLeft.min = yRight.min ? yRight.min * rate : yLeft.min;
  45. yRight.min = yLeft.min ? yLeft.min / rate : yRight.min;
  46. yLeft.max = yRight.max ? yRight.max * rate : yLeft.max;
  47. yRight.max = yLeft.max ? yLeft.max / rate : yRight.max;
  48. } else {
  49. yLeft.min = yLeft.min > 0 ? yRight.min * rate : yLeft.min;
  50. yRight.min = yRight.min > 0 ? yLeft.min / rate : yRight.min;
  51. yLeft.max = yLeft.max < 0 ? yRight.max * rate : yLeft.max;
  52. yRight.max = yRight.max < 0 ? yLeft.max / rate : yRight.max;
  53. }
  54. }
  55. }
  56. }
  57. restoreLevelFromZero(yLeft, yRight, level);
  58. }
  59. function expandStuckValues(yLeft, yRight) {
  60. // wide Y min and max using increased wideFactor
  61. var wideFactor = 0.25;
  62. if (yLeft.max === yLeft.min) {
  63. yLeft.min -= wideFactor;
  64. yLeft.max += wideFactor;
  65. }
  66. if (yRight.max === yRight.min) {
  67. yRight.min -= wideFactor;
  68. yRight.max += wideFactor;
  69. }
  70. }
  71. function moveLevelToZero(yLeft, yRight, level) {
  72. if (level !== 0) {
  73. yLeft.min -= level;
  74. yLeft.max -= level;
  75. yRight.min -= level;
  76. yRight.max -= level;
  77. }
  78. }
  79. function restoreLevelFromZero(yLeft, yRight, level) {
  80. if (level !== 0) {
  81. yLeft.min += level;
  82. yLeft.max += level;
  83. yRight.min += level;
  84. yRight.max += level;
  85. }
  86. }
  87. function checkCorrectAxis(axis) {
  88. return axis.length === 2 && checkCorrectAxes(axis[0]) && checkCorrectAxes(axis[1]);
  89. }
  90. function checkCorrectAxes(axes) {
  91. return 'min' in axes && 'max' in axes;
  92. }
  93. function checkOneSide(yLeft, yRight) {
  94. // on the one hand with respect to zero
  95. return (yLeft.min >= 0 && yRight.min >= 0) || (yLeft.max <= 0 && yRight.max <= 0);
  96. }
  97. function checkTwoCross(yLeft, yRight) {
  98. // both across zero
  99. return yLeft.min <= 0 && yLeft.max >= 0 && yRight.min <= 0 && yRight.max >= 0;
  100. }
  101. function checkOppositeSides(yLeft, yRight) {
  102. // on the opposite sides with respect to zero
  103. return (yLeft.min >= 0 && yRight.max <= 0) || (yLeft.max <= 0 && yRight.min >= 0);
  104. }
  105. function getRate(yLeft, yRight) {
  106. var rateLeft, rateRight, rate;
  107. if (checkTwoCross(yLeft, yRight)) {
  108. rateLeft = yRight.min ? yLeft.min / yRight.min : 0;
  109. rateRight = yRight.max ? yLeft.max / yRight.max : 0;
  110. } else {
  111. if (checkOneSide(yLeft, yRight)) {
  112. var absLeftMin = Math.abs(yLeft.min);
  113. var absLeftMax = Math.abs(yLeft.max);
  114. var absRightMin = Math.abs(yRight.min);
  115. var absRightMax = Math.abs(yRight.max);
  116. var upLeft = _.max([absLeftMin, absLeftMax]);
  117. var downLeft = _.min([absLeftMin, absLeftMax]);
  118. var upRight = _.max([absRightMin, absRightMax]);
  119. var downRight = _.min([absRightMin, absRightMax]);
  120. rateLeft = downLeft ? upLeft / downLeft : upLeft;
  121. rateRight = downRight ? upRight / downRight : upRight;
  122. } else {
  123. if (yLeft.min > 0 || yRight.min > 0) {
  124. rateLeft = yLeft.max / yRight.max;
  125. rateRight = 0;
  126. } else {
  127. rateLeft = 0;
  128. rateRight = yLeft.min / yRight.min;
  129. }
  130. }
  131. }
  132. rate = rateLeft > rateRight ? rateLeft : rateRight;
  133. return rate;
  134. }