ven_interval_data_graphs.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /* * * * * * * * * * * * * * * * * * * * * * */
  2. /* * * Interval Data View Graphing */
  3. /* * * * * * * * * * * * * * * * * * * * * * */
  4. var QueryParameters = {};
  5. QueryParameters.startHour = 0;
  6. QueryParameters.stopHour = 24;
  7. jQuery(document).ready(function($)
  8. {
  9. try
  10. {
  11. // When the `#inteval-select` value is changed, re-generate the graph (new interval value will be referenced)
  12. $('#report-interval-description-select').change(function() {
  13. generate_report_intervals_graph();
  14. });
  15. $("#hours-slider").slider({
  16. range: true,
  17. min: 0,
  18. max: 24,
  19. values: [ 0, 24 ],
  20. slide: function( event, ui ) {
  21. QueryParameters.startHour = ui.values[0];
  22. QueryParameters.stopHour = ui.values[1];
  23. update_hours();
  24. generate_report_intervals_graph();
  25. }
  26. });
  27. update_hours();
  28. // initialize the date selector
  29. var today = Date.now();
  30. $("#report_date").datepicker({
  31. dateFormat: "DD M d yy",
  32. showButtonPanel: true,
  33. onSelect: function(date) {
  34. generate_report_intervals_graph();
  35. }
  36. });
  37. $("#report_date").datepicker("setDate", today);
  38. generate_report_intervals_graph();
  39. }
  40. catch (err)
  41. {
  42. alert(err);
  43. }
  44. });
  45. /* * * * * * * * * * * * * * * * * * * * * * */
  46. function update_hours()
  47. {
  48. var low = convert_24hour_to_12hour(QueryParameters.startHour);
  49. var high = convert_24hour_to_12hour(QueryParameters.stopHour);
  50. $("#hours").val(low + " - " + high);
  51. }
  52. /* * * * * * * * * * * * * * * * * * * * * * */
  53. function convert_24hour_to_12hour(hour)
  54. {
  55. if (hour == 0 || hour == 24)
  56. return "12am";
  57. if (hour <= 11)
  58. return hour.toString() + "am";
  59. if (hour == 12)
  60. return "12pm";
  61. return (hour - 12).toString() + "pm";
  62. }
  63. /* * * * * * * * * * * * * * * * * * * * * * */
  64. function increment_date_by_days(number_of_days)
  65. {
  66. var datepicker_date = $("#report_date").datepicker("getDate");
  67. var new_date = new Date(datepicker_date.getFullYear(), datepicker_date.getMonth(), datepicker_date.getDate() + number_of_days);
  68. $("#report_date").datepicker("setDate", new_date);
  69. generate_report_intervals_graph();
  70. }
  71. /* * * * * * * * * * * * * * * * * * * * * * */
  72. function display_report_intervals_graph(data) {
  73. var datum = [];
  74. try
  75. {
  76. // BUG: if there's only one interval, the graphing library crashes
  77. if (data.report_intervals.length == 1)
  78. data.report_intervals = [ ];
  79. m = Math.ceil(data.report_intervals.length / 10);
  80. for (i = 0; i < data.report_intervals.length; i++)
  81. {
  82. datum.push({x: i, y: data.report_intervals[i][1] });
  83. }
  84. nv.addGraph(function() {
  85. var chart = nv.models.lineChart()
  86. .margin({left: 100}) //Adjust chart margins to give the x-axis some breathing room.
  87. .useInteractiveGuideline(true) //We want nice looking tooltips and a guideline!
  88. .showLegend(false) //Show the legend, allowing users to turn on/off line series.
  89. .showYAxis(true) //Show the y-axis
  90. .showXAxis(true) //Show the x-axis
  91. ;
  92. chart.xAxis //Chart x-axis settings
  93. .axisLabel($("#report_date").datepicker( "getDate").toString().substring(0, 15) + " " + data.time_zone)
  94. .tickFormat(function(idx) {
  95. return data.report_intervals[idx][0];
  96. });
  97. chart.yAxis //Chart y-axis settings
  98. .axisLabel(data.unit_string)
  99. .tickFormat(d3.format('.02f'));
  100. d3.select('#chart svg') //Select the <svg> element you want to render the chart in.
  101. .datum([{ values: datum, key: data.unit_string, color: "#7CB5EC", area: true}]) //Populate the <svg> element with chart data...
  102. .call(chart); //Finally, render the chart!
  103. //Update the chart when window resizes.
  104. nv.utils.windowResize(function() { chart.update() });
  105. return chart;
  106. });
  107. } catch (err)
  108. {
  109. console.error(err);
  110. }
  111. }
  112. /* * * * * * * * * * * * * * * * * * * * * * */
  113. function query_report_intervals_data()
  114. {
  115. var url = window.location.pathname;
  116. var ven_id = document.getElementById("ven-id").value;
  117. var report_interval_description_id = document.getElementById("report-interval-description-select").value;
  118. var date_to_pick = $("#report_date").datepicker("getDate");
  119. var date = $.datepicker.formatDate('yy-mm-dd', date_to_pick);
  120. jQuery.get( "/vens/" + ven_id + "/report_intervals_query?date=" + date + "&start_hour=" + QueryParameters.startHour + "&stop_hour=" + QueryParameters.stopHour + "&rid=" + report_interval_description_id, function(data) {
  121. })
  122. .done(function(data) {
  123. display_report_intervals_graph(data);
  124. })
  125. .fail(function(err) {
  126. console.error('[query_data] : ' + err.responseText);
  127. })
  128. .always(function() {
  129. // alert( "finished" );
  130. });
  131. }
  132. /* * * * * * * * * * * * * * * * * * * * * * */
  133. function generate_report_intervals_graph()
  134. {
  135. try
  136. {
  137. var datepicker_date = $("#report_date").datepicker("getDate");
  138. var date_string = $("#report_date").val();
  139. $(".date").text(date_string);
  140. if(datepicker_date == null)
  141. {
  142. alert('Please choose a date');
  143. }
  144. else
  145. {
  146. query_report_intervals_data();
  147. }
  148. $(".date-incrementers").show();
  149. }
  150. catch (err)
  151. {
  152. console.error(err);
  153. }
  154. }