PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/js/tradereport.js

http://github.com/ofosho/ChartPlotter
JavaScript | 214 lines | 172 code | 15 blank | 27 comment | 29 complexity | 8ca5bf0a04fc12a789a3e75c89cf8035 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /*
  2. JavaScript File written for GPNet
  3. Author: The Omar Chanouha (ofosho)
  4. ofosho@gatech.edu
  5. Associated PHP: tradereport.php
  6. Ajax PHP:
  7. Framework: JQuery.js v1.3.2
  8. Desription:
  9. Create Plot of Data
  10. */
  11. var datasets = [];
  12. var data = [];
  13. var legends = null;
  14. var updateLegendTimeout = null;
  15. var latestPosition = null;
  16. var plot = null;
  17. var overview = null;
  18. Array.prototype.getMax = function() {
  19. //get max number in array
  20. var max = this[0][1];
  21. var len = this.length;
  22. for (var i = 1; i < len; i++) if (this[i][1] > max) max = this[i][1];
  23. return max;
  24. }
  25. Array.prototype.getMin = function() {
  26. //get min number in array
  27. var min = this[0][1];
  28. var len = this.length;
  29. for (var i = 1; i < len; i++) if (this[i][1] < min) min = this[i][1];
  30. return min;
  31. }
  32. function addCommas(nStr){
  33. //add commas to a floating point number
  34. nStr += '';
  35. x = nStr.split('.');
  36. x1 = x[0];
  37. x2 = x.length > 1 ? '.' + x[1] : '';
  38. var rgx = /(\d+)(\d{3})/;
  39. while (rgx.test(x1)) {
  40. x1 = x1.replace(rgx, '$1' + ',' + '$2');
  41. }
  42. return x1 + x2;
  43. }
  44. $(document).ready(function(){
  45. //start report creation
  46. getHistoricalInfo();
  47. });
  48. function getHistoricalInfo(){
  49. //get historical info
  50. dataName = $('#file').val();
  51. $('#title').html(dataName);
  52. groupName = $('#group').val();
  53. $.ajax({
  54. url: '../php/getCSVasTable.php',
  55. method: 'GET',
  56. data: {file: dataName, group:groupName},
  57. datatype: 'html',
  58. success: function(data){
  59. $("#hist").append("<div class='histdata'><b class='tick'>"+dataName+"</b>"+data+"</div>");
  60. addData(dataName);
  61. plotChart();
  62. },
  63. error: function(){ alert('Something went wrong...') }});
  64. }
  65. function getValue(el,colName) {
  66. //format and add a new [x,y] to a dataset
  67. var point = [(new Date($(el).find(".date").html().replace("-","/"))).getTime(),$(el).find("."+ colName).html()];
  68. data.push(point);
  69. }
  70. function addDataSet(dataName,colName){
  71. //add data to the gobal datsets
  72. data = [];
  73. $('#'+dataName).find("tbody > tr").each(function(i,el){getValue(el,colName)});
  74. //var newset = {"label": dataName+'_'+colName, "data": data};
  75. var newset = {"label": colName, "data": data};
  76. datasets.push(newset);
  77. }
  78. function addData(dataName){
  79. $('#'+dataName).find("thead > tr > th").each(function(){
  80. if($(this).html() != 'date')
  81. addDataSet(dataName, $(this).html());
  82. });
  83. }
  84. function plotChart(){
  85. //plot the data
  86. var i = 0;
  87. $.each(datasets, function(key, val) {
  88. val.color = i;
  89. ++i;
  90. });
  91. var choiceContainer = $("#choices");
  92. choiceContainer.html('');
  93. $.each(datasets, function(key, val) {
  94. choiceContainer.append('<br/><input type="checkbox" name="' + key +
  95. '" checked="checked" ' + ' id="id' + key + '">' +
  96. '<label for="id' + key + '">'
  97. + val.label + '</label>');
  98. });
  99. choiceContainer.find("input").click(plotAccordingToChoices);
  100. plotAccordingToChoices();
  101. }
  102. function updateLegend() {
  103. //upddate the legend with values over the crosshair
  104. updateLegendTimeout = null;
  105. var pos = latestPosition;
  106. var axes = plot.getAxes();
  107. if (pos.x < axes.xaxis.min || pos.x > axes.xaxis.max ||
  108. pos.y < axes.yaxis.min || pos.y > axes.yaxis.max)
  109. return;
  110. var i, j, dataset = plot.getData();
  111. for (i = 0; i < dataset.length; ++i) {
  112. var series = dataset[i];
  113. // find the nearest points, x-wise
  114. for (j = 0; j < series.data.length; ++j)
  115. if (series.data[j][0] > pos.x)
  116. break;
  117. // now interpolate
  118. var y, p1 = series.data[j - 1], p2 = series.data[j];
  119. if (p1 == null)
  120. y = p2[1];
  121. else if (p2 == null)
  122. y = p1[1];
  123. else
  124. y = parseFloat(p1[1]) + (p2[1] - p1[1]) * (pos.x - p1[0]) / (p2[0] - p1[0]);
  125. legends.eq(i).text(addCommas(series.label +" = " + y));
  126. }
  127. var temp=i;
  128. var i, j, dataset = overview.getData();
  129. for (i = 0; i < dataset.length; ++i) {
  130. var series = dataset[i];
  131. // find the nearest points, x-wise
  132. for (j = 0; j < series.data.length; ++j)
  133. if (series.data[j][0] > pos.x)
  134. break;
  135. // now interpolate
  136. var y, p1 = series.data[j - 1], p2 = series.data[j];
  137. if (p1 == null)
  138. y = p2[1];
  139. else if (p2 == null)
  140. y = p1[1];
  141. else
  142. y = parseFloat(p1[1]) + (p2[1] - p1[1]) * (pos.x - p1[0]) / (p2[0] - p1[0]);
  143. legends.eq(i+temp).text(addCommas(series.label +" = " + y));
  144. }
  145. }
  146. function plotAccordingToChoices(){
  147. //replot the chart based on user input
  148. data = [];
  149. var choiceContainer = $("#choices");
  150. choiceContainer.find("input:checked").each(function () {
  151. var key = $(this).attr("name");
  152. //datasets[key]["yaxis"] = 2;
  153. if (key && datasets[key]["label"].match("olume") > -1)
  154. data.push(datasets[key]);
  155. });
  156. var options = { xaxis: { ticks: [], mode: "time" },
  157. xaxis:{tickFormatter: function(v,axis){return "";}},
  158. yaxis:{position:"right"},
  159. grid: {hoverable: true, autoHighlight: false, show:true},
  160. crosshair: { mode: "x" },
  161. selection:{mode: "x"},
  162. legend:{backgroundColor:null,backgroundOpacity:0}
  163. };
  164. plot = $.plot($("#placeholder"), data ,options);
  165. var data2 = [];
  166. data2.push(datasets[1]);
  167. overview = $.plot($("#overview"), data2, {
  168. series: {
  169. lines: { show: true, lineWidth: 1 },
  170. shadowSize: 0
  171. },
  172. grid: {backgroundColor: '#FFFFFF'},
  173. xaxis:{mode:"time",timeformat:"%b %d, %y"},
  174. yaxis:{position:"right",labelWidth:10,tickFormatter: function(v,axis){return v.toString().substring(0,2)}},
  175. selection: { mode: "x" },
  176. legend:{backgroundColor:null,backgroundOpacity:0},
  177. bars: {show: true}
  178. });
  179. legends = $(".legendLabel");
  180. $("table").draggable();
  181. $("#placeholder").bind("plotselected", function (event, ranges) {
  182. plot = $.plot($("#placeholder"), data,
  183. $.extend(true, {}, options, {
  184. xaxis: { min: ranges.xaxis.from, max: ranges.xaxis.to }
  185. }));
  186. legends = $(".legendLabel");
  187. $("table").draggable();
  188. // don't fire event on the overview to prevent eternal loop
  189. overview.setSelection(ranges, true);
  190. });
  191. $("#overview").bind("plotselected", function (event, ranges) {
  192. plot.setSelection(ranges);
  193. });
  194. $("#placeholder").bind("plothover", function (event, pos, item) {
  195. latestPosition = pos;
  196. if (!updateLegendTimeout)
  197. updateLegendTimeout = setTimeout(updateLegend, 50);
  198. });
  199. }