interval_data_graph.js 5.8 KB

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