Personal intranet/start page where I can view the weather, links, fitbit data, and the news.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

191 lines
6.2 KiB

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
  5. <script src="lib/moment.min.js"></script>
  6. <link href="vis/vis-timeline-graph2d.min.css" rel="stylesheet" type="text/css" />
  7. <script src="vis/vis.js"></script>
  8. <!-- jQuery library -->
  9. <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  10. </head>
  11. <div id="heartRateGraph"></div>
  12. <body>
  13. <div class="container">
  14. <div class="row">
  15. <div class="col-4 shadow-lg p-3 bg-white rounded">
  16. <label>Heart Rate JSON Files</label>
  17. <input type="file" id="files" name="files[]" multiple />
  18. <output id="list"></output>
  19. </div>
  20. <div class="col-4 shadow-lg p-3 bg-white rounded">
  21. <label>Activity Files</label>
  22. <input type="file" id="actFiles" name="files[]" multiple />
  23. <output id="actList"></output>
  24. </div>
  25. </div>
  26. <div class="row">
  27. </div>
  28. </div>
  29. </body>
  30. <footer>
  31. <script>
  32. function generateHeartRateGraph(jsonFiles)
  33. {
  34. var items = [];
  35. for(var i = 0; i < jsonFiles.length; i++)
  36. {
  37. console.log(jsonFiles[i].length);
  38. for(var j = 0; j < jsonFiles[i].length; j++)
  39. {
  40. var localTime = new Date(jsonFiles[i][j].dateTime);
  41. items.push({y:jsonFiles[i][j].value.bpm, x:localTime.setHours(localTime.getHours() - 4)});
  42. }
  43. }
  44. console.log("Finished Loading Heart Rate Data Into Graph");
  45. console.log(items);
  46. var dataset = new vis.DataSet(items);
  47. var options = {
  48. dataAxis: {
  49. showMinorLabels: true,
  50. left: {
  51. title: {
  52. text: "Heart Rate"
  53. }
  54. }
  55. }
  56. };
  57. var container = document.getElementById("heartRateGraph");
  58. var graph2d = new vis.Graph2d(container, dataset, options);
  59. graph2d.on('rangechanged', graphMoved);
  60. graphsOnPage.push(graph2d);
  61. }
  62. function generateTimeline(jsonFiles)
  63. {
  64. var items = [];
  65. for(var i = 0; i < jsonFiles.length; i++)
  66. {
  67. for(var j = 0; j < jsonFiles[i].length; j++)
  68. {
  69. if(jsonFiles[i][j].hasOwnProperty("dateOfSleep"))
  70. {
  71. var startT = new Date(jsonFiles[i][j].startTime);
  72. var finishT = new Date(jsonFiles[i][j].endTime);
  73. items.push({content: "Sleep",
  74. start:startT, end:finishT, group:0});
  75. }
  76. else
  77. {
  78. var localTime = new Date(jsonFiles[i][j].startTime);
  79. var timeAdjusted = localTime.setHours(localTime.getHours() - 4);
  80. var timeFinish = localTime.setMilliseconds(localTime.getMilliseconds() + jsonFiles[i][j].activeDuration);
  81. items.push({content: jsonFiles[i][j].activityName,
  82. start:timeAdjusted, end:timeFinish, group:0});
  83. }
  84. }
  85. }
  86. console.log("Finished Loading Heart Rate Data Into Graph");
  87. var dataset = new vis.DataSet(items);
  88. var options =
  89. {
  90. margin:
  91. {
  92. item:20,
  93. axis:40
  94. },
  95. showCurrentTime: false
  96. };
  97. var grpups = new vis.DataSet([
  98. {id: 0, content:"Activity", value:0}
  99. ]);
  100. var container = document.getElementById("heartRateGraph");
  101. var graph2d = new vis.Timeline(container, dataset, options);
  102. graph2d.setGroups(grpups);
  103. graph2d.on('rangechanged', graphMoved);
  104. graphsOnPage.push(graph2d);
  105. }
  106. var graphsOnPage = [];
  107. function graphMoved(moveEvent)
  108. {
  109. graphsOnPage.forEach((g)=>
  110. {
  111. g.setWindow(moveEvent.start, moveEvent.end);
  112. })
  113. }
  114. function fetchFilesAsJSONArray(evt)
  115. {
  116. return new Promise((res, rej)=>
  117. {
  118. var files = evt.target.files; // FileList object
  119. var promises = [];
  120. for (var i = 0, f; f = files[i]; i++)
  121. {
  122. promises.push(new Promise((resolve, reject)=>
  123. {
  124. var reader = new FileReader();
  125. reader.onload = function(e)
  126. {
  127. resolve(JSON.parse(reader.result));
  128. };
  129. reader.onerror= function(e)
  130. {
  131. reject(e);
  132. };
  133. reader.readAsBinaryString(files[i]);
  134. }));
  135. }
  136. Promise.all(promises).then((data)=>
  137. {
  138. res(data);
  139. }).catch((error)=>
  140. {
  141. console.log(error);
  142. console.log("Unable to Load Data");
  143. rej(error);
  144. })
  145. });
  146. }
  147. function handleFileSelect(evt)
  148. {
  149. fetchFilesAsJSONArray(evt).then((data)=>
  150. {
  151. generateHeartRateGraph(data);
  152. })
  153. }
  154. document.getElementById('files').addEventListener('change', handleFileSelect, false);
  155. function handleFileSelectEvents(evt)
  156. {
  157. fetchFilesAsJSONArray(evt).then((data)=>
  158. {
  159. generateTimeline(data);
  160. })
  161. }
  162. document.getElementById('actFiles').addEventListener('change', handleFileSelectEvents, false);
  163. </script>
  164. </footer>
  165. </html>