jqplot.sprintf.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /**
  2. * jqPlot
  3. * Pure JavaScript plotting plugin using jQuery
  4. *
  5. * Version: @VERSION
  6. * Revision: @REVISION
  7. *
  8. * Copyright (c) 2009-2013 Chris Leonello
  9. * jqPlot is currently available for use in all personal or commercial projects
  10. * under both the MIT (http://www.opensource.org/licenses/mit-license.php) and GPL
  11. * version 2.0 (http://www.gnu.org/licenses/gpl-2.0.html) licenses. This means that you can
  12. * choose the license that best suits your project and use it accordingly.
  13. *
  14. * Although not required, the author would appreciate an email letting him
  15. * know of any substantial use of jqPlot. You can reach the author at:
  16. * chris at jqplot dot com or see http://www.jqplot.com/info.php .
  17. *
  18. * If you are feeling kind and generous, consider supporting the project by
  19. * making a donation at: http://www.jqplot.com/donate.php .
  20. *
  21. * sprintf functions contained in jqplot.sprintf.js by Ash Searle:
  22. *
  23. * version 2007.04.27
  24. * author Ash Searle
  25. * http://hexmen.com/blog/2007/03/printf-sprintf/
  26. * http://hexmen.com/js/sprintf.js
  27. * The author (Ash Searle) has placed this code in the public domain:
  28. * "This code is unrestricted: you are free to use it however you like."
  29. *
  30. */
  31. (function($) {
  32. /**
  33. * JavaScript printf/sprintf functions.
  34. *
  35. * This code has been adapted from the publicly available sprintf methods
  36. * by Ash Searle. His original header follows:
  37. *
  38. * This code is unrestricted: you are free to use it however you like.
  39. *
  40. * The functions should work as expected, performing left or right alignment,
  41. * truncating strings, outputting numbers with a required precision etc.
  42. *
  43. * For complex cases, these functions follow the Perl implementations of
  44. * (s)printf, allowing arguments to be passed out-of-order, and to set the
  45. * precision or length of the output based on arguments instead of fixed
  46. * numbers.
  47. *
  48. * See http://perldoc.perl.org/functions/sprintf.html for more information.
  49. *
  50. * Implemented:
  51. * - zero and space-padding
  52. * - right and left-alignment,
  53. * - base X prefix (binary, octal and hex)
  54. * - positive number prefix
  55. * - (minimum) width
  56. * - precision / truncation / maximum width
  57. * - out of order arguments
  58. *
  59. * Not implemented (yet):
  60. * - vector flag
  61. * - size (bytes, words, long-words etc.)
  62. *
  63. * Will not implement:
  64. * - %n or %p (no pass-by-reference in JavaScript)
  65. *
  66. * @version 2007.04.27
  67. * @author Ash Searle
  68. *
  69. * You can see the original work and comments on his blog:
  70. * http://hexmen.com/blog/2007/03/printf-sprintf/
  71. * http://hexmen.com/js/sprintf.js
  72. */
  73. /**
  74. * @Modifications 2009.05.26
  75. * @author Chris Leonello
  76. *
  77. * Added %p %P specifier
  78. * Acts like %g or %G but will not add more significant digits to the output than present in the input.
  79. * Example:
  80. * Format: '%.3p', Input: 0.012, Output: 0.012
  81. * Format: '%.3g', Input: 0.012, Output: 0.0120
  82. * Format: '%.4p', Input: 12.0, Output: 12.0
  83. * Format: '%.4g', Input: 12.0, Output: 12.00
  84. * Format: '%.4p', Input: 4.321e-5, Output: 4.321e-5
  85. * Format: '%.4g', Input: 4.321e-5, Output: 4.3210e-5
  86. *
  87. * Example:
  88. * >>> $.jqplot.sprintf('%.2f, %d', 23.3452, 43.23)
  89. * "23.35, 43"
  90. * >>> $.jqplot.sprintf("no value: %n, decimal with thousands separator: %'d", 23.3452, 433524)
  91. * "no value: , decimal with thousands separator: 433,524"
  92. */
  93. $.jqplot.sprintf = function() {
  94. function pad(str, len, chr, leftJustify) {
  95. var padding = (str.length >= len) ? '' : Array(1 + len - str.length >>> 0).join(chr);
  96. return leftJustify ? str + padding : padding + str;
  97. }
  98. function thousand_separate(value) {
  99. var value_str = new String(value);
  100. for (var i=10; i>0; i--) {
  101. if (value_str == (value_str = value_str.replace(/^(\d+)(\d{3})/, "$1"+$.jqplot.sprintf.thousandsSeparator+"$2"))) break;
  102. }
  103. return value_str;
  104. }
  105. function justify(value, prefix, leftJustify, minWidth, zeroPad, htmlSpace) {
  106. var diff = minWidth - value.length;
  107. if (diff > 0) {
  108. var spchar = ' ';
  109. if (htmlSpace) { spchar = ' '; }
  110. if (leftJustify || !zeroPad) {
  111. value = pad(value, minWidth, spchar, leftJustify);
  112. } else {
  113. value = value.slice(0, prefix.length) + pad('', diff, '0', true) + value.slice(prefix.length);
  114. }
  115. }
  116. return value;
  117. }
  118. function formatBaseX(value, base, prefix, leftJustify, minWidth, precision, zeroPad, htmlSpace) {
  119. // Note: casts negative numbers to positive ones
  120. var number = value >>> 0;
  121. prefix = prefix && number && {'2': '0b', '8': '0', '16': '0x'}[base] || '';
  122. value = prefix + pad(number.toString(base), precision || 0, '0', false);
  123. return justify(value, prefix, leftJustify, minWidth, zeroPad, htmlSpace);
  124. }
  125. function formatString(value, leftJustify, minWidth, precision, zeroPad, htmlSpace) {
  126. if (precision != null) {
  127. value = value.slice(0, precision);
  128. }
  129. return justify(value, '', leftJustify, minWidth, zeroPad, htmlSpace);
  130. }
  131. var a = arguments, i = 0, format = a[i++];
  132. return format.replace($.jqplot.sprintf.regex, function(substring, valueIndex, flags, minWidth, _, precision, type) {
  133. if (substring == '%%') { return '%'; }
  134. // parse flags
  135. var leftJustify = false, positivePrefix = '', zeroPad = false, prefixBaseX = false, htmlSpace = false, thousandSeparation = false;
  136. for (var j = 0; flags && j < flags.length; j++) switch (flags.charAt(j)) {
  137. case ' ': positivePrefix = ' '; break;
  138. case '+': positivePrefix = '+'; break;
  139. case '-': leftJustify = true; break;
  140. case '0': zeroPad = true; break;
  141. case '#': prefixBaseX = true; break;
  142. case '&': htmlSpace = true; break;
  143. case '\'': thousandSeparation = true; break;
  144. }
  145. // parameters may be null, undefined, empty-string or real valued
  146. // we want to ignore null, undefined and empty-string values
  147. if (!minWidth) {
  148. minWidth = 0;
  149. }
  150. else if (minWidth == '*') {
  151. minWidth = +a[i++];
  152. }
  153. else if (minWidth.charAt(0) == '*') {
  154. minWidth = +a[minWidth.slice(1, -1)];
  155. }
  156. else {
  157. minWidth = +minWidth;
  158. }
  159. // Note: undocumented perl feature:
  160. if (minWidth < 0) {
  161. minWidth = -minWidth;
  162. leftJustify = true;
  163. }
  164. if (!isFinite(minWidth)) {
  165. throw new Error('$.jqplot.sprintf: (minimum-)width must be finite');
  166. }
  167. if (!precision) {
  168. precision = 'fFeE'.indexOf(type) > -1 ? 6 : (type == 'd') ? 0 : void(0);
  169. }
  170. else if (precision == '*') {
  171. precision = +a[i++];
  172. }
  173. else if (precision.charAt(0) == '*') {
  174. precision = +a[precision.slice(1, -1)];
  175. }
  176. else {
  177. precision = +precision;
  178. }
  179. // grab value using valueIndex if required?
  180. var value = valueIndex ? a[valueIndex.slice(0, -1)] : a[i++];
  181. switch (type) {
  182. case 's': {
  183. if (value == null) {
  184. return '';
  185. }
  186. return formatString(String(value), leftJustify, minWidth, precision, zeroPad, htmlSpace);
  187. }
  188. case 'c': return formatString(String.fromCharCode(+value), leftJustify, minWidth, precision, zeroPad, htmlSpace);
  189. case 'b': return formatBaseX(value, 2, prefixBaseX, leftJustify, minWidth, precision, zeroPad,htmlSpace);
  190. case 'o': return formatBaseX(value, 8, prefixBaseX, leftJustify, minWidth, precision, zeroPad, htmlSpace);
  191. case 'x': return formatBaseX(value, 16, prefixBaseX, leftJustify, minWidth, precision, zeroPad, htmlSpace);
  192. case 'X': return formatBaseX(value, 16, prefixBaseX, leftJustify, minWidth, precision, zeroPad, htmlSpace).toUpperCase();
  193. case 'u': return formatBaseX(value, 10, prefixBaseX, leftJustify, minWidth, precision, zeroPad, htmlSpace);
  194. case 'i': {
  195. var number = parseInt(+value, 10);
  196. if (isNaN(number)) {
  197. return '';
  198. }
  199. var prefix = number < 0 ? '-' : positivePrefix;
  200. var number_str = thousandSeparation ? thousand_separate(String(Math.abs(number))): String(Math.abs(number));
  201. value = prefix + pad(number_str, precision, '0', false);
  202. //value = prefix + pad(String(Math.abs(number)), precision, '0', false);
  203. return justify(value, prefix, leftJustify, minWidth, zeroPad, htmlSpace);
  204. }
  205. case 'd': {
  206. var number = Math.round(+value);
  207. if (isNaN(number)) {
  208. return '';
  209. }
  210. var prefix = number < 0 ? '-' : positivePrefix;
  211. var number_str = thousandSeparation ? thousand_separate(String(Math.abs(number))): String(Math.abs(number));
  212. value = prefix + pad(number_str, precision, '0', false);
  213. return justify(value, prefix, leftJustify, minWidth, zeroPad, htmlSpace);
  214. }
  215. case 'e':
  216. case 'E':
  217. case 'f':
  218. case 'F':
  219. case 'g':
  220. case 'G':
  221. {
  222. var number = +value;
  223. if (isNaN(number)) {
  224. return '';
  225. }
  226. var prefix = number < 0 ? '-' : positivePrefix;
  227. var method = ['toExponential', 'toFixed', 'toPrecision']['efg'.indexOf(type.toLowerCase())];
  228. var textTransform = ['toString', 'toUpperCase']['eEfFgG'.indexOf(type) % 2];
  229. var number_str = Math.abs(number)[method](precision);
  230. // Apply the decimal mark properly by splitting the number by the
  231. // decimalMark, applying thousands separator, and then placing it
  232. // back in.
  233. var parts = number_str.toString().split('.');
  234. parts[0] = thousandSeparation ? thousand_separate(parts[0]) : parts[0];
  235. number_str = parts.join($.jqplot.sprintf.decimalMark);
  236. value = prefix + number_str;
  237. var justified = justify(value, prefix, leftJustify, minWidth, zeroPad, htmlSpace)[textTransform]();
  238. return justified;
  239. }
  240. case 'p':
  241. case 'P':
  242. {
  243. // make sure number is a number
  244. var number = +value;
  245. if (isNaN(number)) {
  246. return '';
  247. }
  248. var prefix = number < 0 ? '-' : positivePrefix;
  249. var parts = String(Number(Math.abs(number)).toExponential()).split(/e|E/);
  250. var sd = (parts[0].indexOf('.') != -1) ? parts[0].length - 1 : String(number).length;
  251. var zeros = (parts[1] < 0) ? -parts[1] - 1 : 0;
  252. if (Math.abs(number) < 1) {
  253. if (sd + zeros <= precision) {
  254. value = prefix + Math.abs(number).toPrecision(sd);
  255. }
  256. else {
  257. if (sd <= precision - 1) {
  258. value = prefix + Math.abs(number).toExponential(sd-1);
  259. }
  260. else {
  261. value = prefix + Math.abs(number).toExponential(precision-1);
  262. }
  263. }
  264. }
  265. else {
  266. var prec = (sd <= precision) ? sd : precision;
  267. value = prefix + Math.abs(number).toPrecision(prec);
  268. }
  269. var textTransform = ['toString', 'toUpperCase']['pP'.indexOf(type) % 2];
  270. return justify(value, prefix, leftJustify, minWidth, zeroPad, htmlSpace)[textTransform]();
  271. }
  272. case 'n': return '';
  273. default: return substring;
  274. }
  275. });
  276. };
  277. $.jqplot.sprintf.thousandsSeparator = ',';
  278. // Specifies the decimal mark for floating point values. By default a period '.'
  279. // is used. If you change this value to for example a comma be sure to also
  280. // change the thousands separator or else this won't work since a simple String
  281. // replace is used (replacing all periods with the mark specified here).
  282. $.jqplot.sprintf.decimalMark = '.';
  283. $.jqplot.sprintf.regex = /%%|%(\d+\$)?([-+#0&\' ]*)(\*\d+\$|\*|\d+)?(\.(\*\d+\$|\*|\d+))?([nAscboxXuidfegpEGP])/g;
  284. $.jqplot.getSignificantFigures = function(number) {
  285. var parts = String(Number(Math.abs(number)).toExponential()).split(/e|E/);
  286. // total significant digits
  287. var sd = (parts[0].indexOf('.') != -1) ? parts[0].length - 1 : parts[0].length;
  288. var zeros = (parts[1] < 0) ? -parts[1] - 1 : 0;
  289. // exponent
  290. var expn = parseInt(parts[1], 10);
  291. // digits to the left of the decimal place
  292. var dleft = (expn + 1 > 0) ? expn + 1 : 0;
  293. // digits to the right of the decimal place
  294. var dright = (sd <= dleft) ? 0 : sd - expn - 1;
  295. return {significantDigits: sd, digitsLeft: dleft, digitsRight: dright, zeros: zeros, exponent: expn} ;
  296. };
  297. $.jqplot.getPrecision = function(number) {
  298. return $.jqplot.getSignificantFigures(number).digitsRight;
  299. };
  300. })(jQuery);