ticks.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import { getDataMinMax } from 'app/core/time_series2';
  2. /**
  3. * Calculate tick step.
  4. * Implementation from d3-array (ticks.js)
  5. * https://github.com/d3/d3-array/blob/master/src/ticks.js
  6. * @param start Start value
  7. * @param stop End value
  8. * @param count Ticks count
  9. */
  10. export function tickStep(start: number, stop: number, count: number): number {
  11. let e10 = Math.sqrt(50),
  12. e5 = Math.sqrt(10),
  13. e2 = Math.sqrt(2);
  14. let step0 = Math.abs(stop - start) / Math.max(0, count),
  15. step1 = Math.pow(10, Math.floor(Math.log(step0) / Math.LN10)),
  16. error = step0 / step1;
  17. if (error >= e10) {
  18. step1 *= 10;
  19. } else if (error >= e5) {
  20. step1 *= 5;
  21. } else if (error >= e2) {
  22. step1 *= 2;
  23. }
  24. return stop < start ? -step1 : step1;
  25. }
  26. export function getScaledDecimals(decimals, tick_size) {
  27. return decimals - Math.floor(Math.log(tick_size) / Math.LN10);
  28. }
  29. /**
  30. * Calculate tick size based on min and max values, number of ticks and precision.
  31. * Implementation from Flot.
  32. * @param min Axis minimum
  33. * @param max Axis maximum
  34. * @param noTicks Number of ticks
  35. * @param tickDecimals Tick decimal precision
  36. */
  37. export function getFlotTickSize(min: number, max: number, noTicks: number, tickDecimals: number) {
  38. var delta = (max - min) / noTicks,
  39. dec = -Math.floor(Math.log(delta) / Math.LN10),
  40. maxDec = tickDecimals;
  41. var magn = Math.pow(10, -dec),
  42. norm = delta / magn, // norm is between 1.0 and 10.0
  43. size;
  44. if (norm < 1.5) {
  45. size = 1;
  46. } else if (norm < 3) {
  47. size = 2;
  48. // special case for 2.5, requires an extra decimal
  49. if (norm > 2.25 && (maxDec == null || dec + 1 <= maxDec)) {
  50. size = 2.5;
  51. ++dec;
  52. }
  53. } else if (norm < 7.5) {
  54. size = 5;
  55. } else {
  56. size = 10;
  57. }
  58. size *= magn;
  59. return size;
  60. }
  61. /**
  62. * Calculate axis range (min and max).
  63. * Implementation from Flot.
  64. */
  65. export function getFlotRange(panelMin, panelMax, datamin, datamax) {
  66. const autoscaleMargin = 0.02;
  67. let min = +(panelMin != null ? panelMin : datamin);
  68. let max = +(panelMax != null ? panelMax : datamax);
  69. let delta = max - min;
  70. if (delta === 0.0) {
  71. // Grafana fix: wide Y min and max using increased wideFactor
  72. // when all series values are the same
  73. var wideFactor = 0.25;
  74. var widen = Math.abs(max === 0 ? 1 : max * wideFactor);
  75. if (panelMin === null) {
  76. min -= widen;
  77. }
  78. // always widen max if we couldn't widen min to ensure we
  79. // don't fall into min == max which doesn't work
  80. if (panelMax == null || panelMin != null) {
  81. max += widen;
  82. }
  83. } else {
  84. // consider autoscaling
  85. var margin = autoscaleMargin;
  86. if (margin != null) {
  87. if (panelMin == null) {
  88. min -= delta * margin;
  89. // make sure we don't go below zero if all values
  90. // are positive
  91. if (min < 0 && datamin != null && datamin >= 0) {
  92. min = 0;
  93. }
  94. }
  95. if (panelMax == null) {
  96. max += delta * margin;
  97. if (max > 0 && datamax != null && datamax <= 0) {
  98. max = 0;
  99. }
  100. }
  101. }
  102. }
  103. return { min, max };
  104. }
  105. /**
  106. * Calculate tick decimals.
  107. * Implementation from Flot.
  108. */
  109. export function getFlotTickDecimals(data, axis) {
  110. let { datamin, datamax } = getDataMinMax(data);
  111. let { min, max } = getFlotRange(axis.min, axis.max, datamin, datamax);
  112. let noTicks = 3;
  113. let tickDecimals, maxDec;
  114. let delta = (max - min) / noTicks;
  115. let dec = -Math.floor(Math.log(delta) / Math.LN10);
  116. let magn = Math.pow(10, -dec);
  117. // norm is between 1.0 and 10.0
  118. let norm = delta / magn;
  119. let size;
  120. if (norm < 1.5) {
  121. size = 1;
  122. } else if (norm < 3) {
  123. size = 2;
  124. // special case for 2.5, requires an extra decimal
  125. if (norm > 2.25 && (maxDec == null || dec + 1 <= maxDec)) {
  126. size = 2.5;
  127. ++dec;
  128. }
  129. } else if (norm < 7.5) {
  130. size = 5;
  131. } else {
  132. size = 10;
  133. }
  134. size *= magn;
  135. tickDecimals = Math.max(0, maxDec != null ? maxDec : dec);
  136. // grafana addition
  137. const scaledDecimals = tickDecimals - Math.floor(Math.log(size) / Math.LN10);
  138. return { tickDecimals, scaledDecimals };
  139. }