align_yaxes.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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: any, level: any) {
  8. if (isNaN(level) || !checkCorrectAxis(yAxes)) {
  9. return;
  10. }
  11. const [yLeft, yRight] = yAxes;
  12. moveLevelToZero(yLeft, yRight, level);
  13. expandStuckValues(yLeft, yRight);
  14. // one of graphs on zero
  15. const zero = yLeft.min === 0 || yRight.min === 0 || yLeft.max === 0 || yRight.max === 0;
  16. const 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. const 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: { max: number; min: number }, yRight: { max: number; min: number }) {
  60. // wide Y min and max using increased wideFactor
  61. const 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: { min: number; max: number }, yRight: { min: number; max: number }, level: number) {
  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(
  80. yLeft: { min: number; max: number },
  81. yRight: { min: number; max: number },
  82. level: number
  83. ) {
  84. if (level !== 0) {
  85. yLeft.min += level;
  86. yLeft.max += level;
  87. yRight.min += level;
  88. yRight.max += level;
  89. }
  90. }
  91. interface AxisSide {
  92. max: number;
  93. min: number;
  94. }
  95. function checkCorrectAxis(axis: any[]) {
  96. return axis.length === 2 && checkCorrectAxes(axis[0]) && checkCorrectAxes(axis[1]);
  97. }
  98. function checkCorrectAxes(axes: any) {
  99. return 'min' in axes && 'max' in axes;
  100. }
  101. function checkOneSide(yLeft: AxisSide, yRight: AxisSide) {
  102. // on the one hand with respect to zero
  103. return (yLeft.min >= 0 && yRight.min >= 0) || (yLeft.max <= 0 && yRight.max <= 0);
  104. }
  105. function checkTwoCross(yLeft: AxisSide, yRight: AxisSide) {
  106. // both across zero
  107. return yLeft.min <= 0 && yLeft.max >= 0 && yRight.min <= 0 && yRight.max >= 0;
  108. }
  109. function checkOppositeSides(yLeft: AxisSide, yRight: AxisSide) {
  110. // on the opposite sides with respect to zero
  111. return (yLeft.min >= 0 && yRight.max <= 0) || (yLeft.max <= 0 && yRight.min >= 0);
  112. }
  113. function getRate(yLeft: AxisSide, yRight: AxisSide) {
  114. let rateLeft, rateRight, rate;
  115. if (checkTwoCross(yLeft, yRight)) {
  116. rateLeft = yRight.min ? yLeft.min / yRight.min : 0;
  117. rateRight = yRight.max ? yLeft.max / yRight.max : 0;
  118. } else {
  119. if (checkOneSide(yLeft, yRight)) {
  120. const absLeftMin = Math.abs(yLeft.min);
  121. const absLeftMax = Math.abs(yLeft.max);
  122. const absRightMin = Math.abs(yRight.min);
  123. const absRightMax = Math.abs(yRight.max);
  124. const upLeft = _.max([absLeftMin, absLeftMax]);
  125. const downLeft = _.min([absLeftMin, absLeftMax]);
  126. const upRight = _.max([absRightMin, absRightMax]);
  127. const downRight = _.min([absRightMin, absRightMax]);
  128. rateLeft = downLeft ? upLeft / downLeft : upLeft;
  129. rateRight = downRight ? upRight / downRight : upRight;
  130. } else {
  131. if (yLeft.min > 0 || yRight.min > 0) {
  132. rateLeft = yLeft.max / yRight.max;
  133. rateRight = 0;
  134. } else {
  135. rateLeft = 0;
  136. rateRight = yLeft.min / yRight.min;
  137. }
  138. }
  139. }
  140. rate = rateLeft > rateRight ? rateLeft : rateRight;
  141. return rate;
  142. }