jqplot.byteFormatter.js 1.2 KB

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