event_interval_data_graphs.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. generate_report_intervals_graph();
  29. }
  30. catch (err)
  31. {
  32. alert(err);
  33. }
  34. });
  35. /* * * * * * * * * * * * * * * * * * * * * * */
  36. function update_hours()
  37. {
  38. var low = convert_24hour_to_12hour(QueryParameters.startHour);
  39. var high = convert_24hour_to_12hour(QueryParameters.stopHour);
  40. $("#hours").val(low + " - " + high);
  41. }
  42. /* * * * * * * * * * * * * * * * * * * * * * */
  43. function convert_24hour_to_12hour(hour)
  44. {
  45. if (hour == 0 || hour == 24)
  46. return "12am";
  47. if (hour <= 11)
  48. return hour.toString() + "am";
  49. if (hour == 12)
  50. return "12pm";
  51. return (hour - 12).toString() + "pm";
  52. }
  53. /* * * * * * * * * * * * * * * * * * * * * * */
  54. function display_report_intervals_graph(data) {
  55. var datum = [];
  56. var event_datum = [];
  57. try
  58. {
  59. var event_start = false;
  60. // BUG: if there's only one interval, the graphing library crashes
  61. if (data.report_intervals.length == 1)
  62. data.report_intervals = [ ];
  63. for (i = 0; i < data.report_intervals.length; i++)
  64. {
  65. datum.push({x: i, y: data.report_intervals[i][1] });
  66. // mark the start of the event with a vertical line
  67. if (data.report_intervals[i][2] == 'active' && !event_start)
  68. {
  69. event_start = true;
  70. event_datum.push({x: i, y: 0 });
  71. }
  72. if (data.report_intervals[i][2] == 'active')
  73. event_datum.push({x: i, y: data.report_intervals[i][1] });
  74. else {
  75. // mark the end of the event with a vertical line
  76. if (event_start)
  77. event_datum.push({x: i - 1, y: 0});
  78. event_datum.push({x: i, y: 0});
  79. }
  80. }
  81. nv.addGraph(function() {
  82. var chart = nv.models.lineChart()
  83. .margin({left: 100}) //Adjust chart margins to give the x-axis some breathing room.
  84. .useInteractiveGuideline(true) //We want nice looking tooltips and a guideline!
  85. .showLegend(false) //Show the legend, allowing users to turn on/off line series.
  86. .showYAxis(true) //Show the y-axis
  87. .showXAxis(true) //Show the x-axis
  88. ;
  89. chart.xAxis //Chart x-axis settings
  90. .axisLabel($("#event_date").text() + " " + data.time_zone)
  91. .tickFormat(function(idx) {
  92. return data.report_intervals[idx][0];
  93. });
  94. chart.yAxis //Chart y-axis settings
  95. .axisLabel(data.unit_string)
  96. .tickFormat(d3.format('.02f'));
  97. d3.select('#chart svg') //Select the <svg> element you want to render the chart in.
  98. .datum([{ values: datum, key: data.unit_string, color: "#7CB5EC", area: true },
  99. { values: event_datum, key: 'Event' + ' ' + data.unit_string, color: "#000000", area: true} ]) //Populate the <svg> element with chart data...
  100. .call(chart); //Finally, render the chart!
  101. //Update the chart when window resizes.
  102. nv.utils.windowResize(function() { chart.update() });
  103. return chart;
  104. });
  105. } catch (err)
  106. {
  107. console.error(err);
  108. }
  109. }
  110. /* * * * * * * * * * * * * * * * * * * * * * */
  111. function query_group_interval_data()
  112. {
  113. var event_id = document.getElementById("event-id").value;
  114. var rid = document.getElementById("report-interval-description-select").value;
  115. jQuery.get( "/events/" + event_id + "/status_query?start_hour=" + QueryParameters.startHour + "&stop_hour=" + QueryParameters.stopHour + "&rid=" + rid, function(data) {
  116. })
  117. .done(function(data) {
  118. display_report_intervals_graph(data);
  119. })
  120. .fail(function(err) {
  121. console.error('[query_data] : ' + err.responseText);
  122. })
  123. .always(function() {
  124. // alert( "finished" );
  125. });
  126. }
  127. /* * * * * * * * * * * * * * * * * * * * * * */
  128. function generate_report_intervals_graph()
  129. {
  130. try
  131. {
  132. query_group_interval_data();
  133. }
  134. catch (err)
  135. {
  136. console.error(err);
  137. }
  138. }