jqplot.ciParser.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. * Class: $.jqplot.ciParser
  34. * Data Renderer function which converts a custom JSON data object into jqPlot data format.
  35. * Set this as a callable on the jqplot dataRenderer plot option:
  36. *
  37. * > plot = $.jqplot('mychart', [data], { dataRenderer: $.jqplot.ciParser, ... });
  38. *
  39. * Where data is an object in JSON format or a JSON encoded string conforming to the
  40. * City Index API spec.
  41. *
  42. * Note that calling the renderer function is handled internally by jqPlot. The
  43. * user does not have to call the function. The parameters described below will
  44. * automatically be passed to the ciParser function.
  45. *
  46. * Parameters:
  47. * data - JSON encoded string or object.
  48. * plot - reference to jqPlot Plot object.
  49. *
  50. * Returns:
  51. * data array in jqPlot format.
  52. *
  53. */
  54. $.jqplot.ciParser = function (data, plot) {
  55. var ret = [],
  56. line,
  57. temp,
  58. i, j, k, kk;
  59. if (typeof(data) == "string") {
  60. data = $.jqplot.JSON.parse(data, handleStrings);
  61. }
  62. else if (typeof(data) == "object") {
  63. for (k in data) {
  64. for (i=0; i<data[k].length; i++) {
  65. for (kk in data[k][i]) {
  66. data[k][i][kk] = handleStrings(kk, data[k][i][kk]);
  67. }
  68. }
  69. }
  70. }
  71. else {
  72. return null;
  73. }
  74. // function handleStrings
  75. // Checks any JSON encoded strings to see if they are
  76. // encoded dates. If so, pull out the timestamp.
  77. // Expects dates to be represented by js timestamps.
  78. function handleStrings(key, value) {
  79. var a;
  80. if (value != null) {
  81. if (value.toString().indexOf('Date') >= 0) {
  82. //here we will try to extract the ticks from the Date string in the "value" fields of JSON returned data
  83. a = /^\/Date\((-?[0-9]+)\)\/$/.exec(value);
  84. if (a) {
  85. return parseInt(a[1], 10);
  86. }
  87. }
  88. return value;
  89. }
  90. }
  91. for (var prop in data) {
  92. line = [];
  93. temp = data[prop];
  94. switch (prop) {
  95. case "PriceTicks":
  96. for (i=0; i<temp.length; i++) {
  97. line.push([temp[i]['TickDate'], temp[i]['Price']]);
  98. }
  99. break;
  100. case "PriceBars":
  101. for (i=0; i<temp.length; i++) {
  102. line.push([temp[i]['BarDate'], temp[i]['Open'], temp[i]['High'], temp[i]['Low'], temp[i]['Close']]);
  103. }
  104. break;
  105. }
  106. ret.push(line);
  107. }
  108. return ret;
  109. };
  110. })(jQuery);