jquery.flot.saturated.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. (function ($) {
  2. 'use strict';
  3. var saturated = {
  4. saturate: function (a) {
  5. if (a === Infinity) {
  6. return Number.MAX_VALUE;
  7. }
  8. if (a === -Infinity) {
  9. return -Number.MAX_VALUE;
  10. }
  11. return a;
  12. },
  13. delta: function(min, max, noTicks) {
  14. return ((max - min) / noTicks) === Infinity ? (max / noTicks - min / noTicks) : (max - min) / noTicks
  15. },
  16. multiply: function (a, b) {
  17. return saturated.saturate(a * b);
  18. },
  19. // returns c * bInt * a. Beahves properly in the case where c is negative
  20. // and bInt * a is bigger that Number.MAX_VALUE (Infinity)
  21. multiplyAdd: function (a, bInt, c) {
  22. if (isFinite(a * bInt)) {
  23. return saturated.saturate(a * bInt + c);
  24. } else {
  25. var result = c;
  26. for (var i = 0; i < bInt; i++) {
  27. result += a;
  28. }
  29. return saturated.saturate(result);
  30. }
  31. },
  32. // round to nearby lower multiple of base
  33. floorInBase: function(n, base) {
  34. return base * Math.floor(n / base);
  35. }
  36. };
  37. $.plot.saturated = saturated;
  38. })(jQuery);