jqplot.byteFormatter.js 1017 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. "use strict";
  2. /**
  3. * jqplot formatter for byte values
  4. *
  5. * @package phpMyAdmin
  6. */
  7. (function ($) {
  8. 'use strict';
  9. var formatByte = function formatByte(value, index) {
  10. var val = value;
  11. var i = index;
  12. var units = [Messages.strB, Messages.strKiB, Messages.strMiB, Messages.strGiB, Messages.strTiB, Messages.strPiB, Messages.strEiB];
  13. while (val >= 1024 && i <= 6) {
  14. val /= 1024;
  15. i++;
  16. }
  17. var format = '%.1f';
  18. if (Math.floor(val) === val) {
  19. format = '%.0f';
  20. }
  21. return $.jqplot.sprintf(format + ' ' + units[i], val);
  22. };
  23. /**
  24. * The index indicates what unit the incoming data will be in.
  25. * 0 for bytes, 1 for kilobytes and so on...
  26. */
  27. $.jqplot.byteFormatter = function (index) {
  28. var i = index || 0;
  29. return function (format, value) {
  30. var val = value;
  31. if (typeof val === 'number') {
  32. val = parseFloat(val) || 0;
  33. return formatByte(val, i);
  34. } else {
  35. return String(val);
  36. }
  37. };
  38. };
  39. })(jQuery);