Simple PHP & SQL weight tracker for multiple persons
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.

index.html 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <html lang="en">
  2. <head>
  3. <meta charset="utf-8" />
  4. <title>Weight-Track</title>
  5. <script src="https://cdn.jsdelivr.net/npm/chart.js@2.9.0/dist/Chart.bundle.min.js" integrity="sha256-tSYaQkuUF17Z5HozxXRWckY1j6uVLYFEHGEooJ6YsP0=" crossorigin="anonymous"></script>
  6. </head>
  7. <body>
  8. <div id="content">
  9. <h1>Weight-Track</h1>
  10. <p id="status">Please wait while Weight-Track is loading... (JavaScript needed)</p>
  11. </div>
  12. <div id="footer">
  13. <p>Made with <a href="https://www.chartjs.org/docs/latest/">Chart.js</a></p>
  14. </div>
  15. <script type="text/javascript">
  16. var colorFromIndex = function(index) {
  17. var colors = [
  18. '129, 38, 204',
  19. '48, 181, 36',
  20. '255, 99, 132',
  21. '54, 162, 235',
  22. '255, 206, 86',
  23. '75, 192, 192',
  24. '153, 102, 255',
  25. '255, 159, 64'
  26. ];
  27. if (index < colors.length) {
  28. return colors[index];
  29. } else {
  30. return '120, 120, 120';
  31. }
  32. };
  33. var createDataset = function(name, data, color) {
  34. return {
  35. label: name,
  36. data: data,
  37. backgroundColor: [
  38. 'rgba(' + color + ', 0)'
  39. ],
  40. borderColor: [
  41. 'rgba(' + color + ', 1)'
  42. ],
  43. pointBorderColor: 'rgba(' + color + ', 1)',
  44. pointRadius: 2,
  45. borderWidth: 4
  46. };
  47. };
  48. var addToForm = function(form, element, name) {
  49. var fe = document.createElement('p');
  50. fe.innerHTML = name + ' ';
  51. fe.appendChild(element);
  52. form.appendChild(fe);
  53. };
  54. var renderUser = function(data) {
  55. var div = document.createElement('div');
  56. var ctx = document.createElement('canvas');
  57. var datasets = [];
  58. for (var i = 0; i < data.users.length; i++) {
  59. var set = createDataset(data.users[i].name,
  60. data.users[i].data, colorFromIndex(i));
  61. datasets.push(set);
  62. }
  63. var chart = new Chart(ctx, {
  64. type: 'line',
  65. data: {
  66. datasets: datasets
  67. },
  68. options: {
  69. scales: {
  70. xAxes: [{
  71. type: 'time',
  72. time: {
  73. unit: 'day'
  74. }
  75. }]
  76. },
  77. events: [
  78. 'mousemove', 'mouseout', 'click',
  79. 'touchstart', 'touchmove', 'touchend'
  80. ],
  81. tooltips: {
  82. mode: 'nearest',
  83. intersect: false,
  84. axis: 'x'
  85. }
  86. }
  87. });
  88. if (data.users.length > 0) {
  89. div.appendChild(ctx);
  90. div.appendChild(document.createElement('hr'));
  91. var date = new Date();
  92. var hours = date.getHours();
  93. hours = (hours < 10) ? '0' + hours : hours;
  94. var minutes = date.getMinutes();
  95. minutes = (minutes < 10) ? '0' + minutes : minutes;
  96. var form_append = document.createElement('form');
  97. form_append.setAttribute('method', 'post');
  98. form_append.setAttribute('action', 'weight.php');
  99. var p_append = document.createElement('p');
  100. p_append.innerHTML = 'Add new Data-Point:';
  101. form_append.appendChild(p_append);
  102. var name_append = document.createElement('select');
  103. for (var i = 0; i < data.users.length; i++) {
  104. var option = document.createElement('option');
  105. option.text = data.users[i].name;
  106. option.value = 'user_' + data.users[i].id;
  107. name_append.add(option);
  108. }
  109. name_append.setAttribute('name', 'username');
  110. name_append.required = true;
  111. addToForm(form_append, name_append, 'Username:');
  112. var date_append = document.createElement('input');
  113. date_append.setAttribute('type', 'date');
  114. date_append.setAttribute('name', 'date');
  115. date_append.setAttribute('placeholder', 'Date');
  116. date_append.required = true;
  117. date_append.value = date.toISOString().slice(0, 10);
  118. addToForm(form_append, date_append, 'Date:');
  119. var time_append = document.createElement('input');
  120. time_append.setAttribute('type', 'time');
  121. time_append.setAttribute('name', 'time');
  122. time_append.setAttribute('placeholder', 'Time');
  123. time_append.required = true;
  124. time_append.value = hours + ':' + minutes;
  125. addToForm(form_append, time_append, 'Time:');
  126. var weight_append = document.createElement('input');
  127. weight_append.setAttribute('type', 'number');
  128. weight_append.setAttribute('name', 'weight');
  129. weight_append.setAttribute('step', '0.1');
  130. weight_append.min = 42.0;
  131. weight_append.max = 999.9;
  132. weight_append.setAttribute('placeholder', 'Weight');
  133. weight_append.required = true;
  134. addToForm(form_append, weight_append, 'Weight:');
  135. var submit_append = document.createElement('input');
  136. submit_append.setAttribute('type', 'submit');
  137. submit_append.setAttribute('value', 'Add Data');
  138. form_append.appendChild(submit_append);
  139. div.appendChild(form_append);
  140. div.appendChild(document.createElement('hr'));
  141. }
  142. var form_new = document.createElement('form');
  143. form_new.setAttribute('method', 'post');
  144. form_new.setAttribute('action', 'weight.php');
  145. var p_new = document.createElement('p');
  146. p_new.innerHTML = 'Add new User:';
  147. form_new.appendChild(p_new);
  148. var name_new = document.createElement('input');
  149. name_new.setAttribute('type', 'text');
  150. name_new.setAttribute('name', 'username');
  151. name_new.setAttribute('placeholder', 'Name');
  152. name_new.required = true;
  153. addToForm(form_new, name_new, 'Username:');
  154. var submit_new = document.createElement('input');
  155. submit_new.setAttribute('type', 'submit');
  156. submit_new.setAttribute('value', 'Add User');
  157. form_new.appendChild(submit_new);
  158. div.appendChild(form_new);
  159. div.appendChild(document.createElement('hr'));
  160. document.getElementById("content").appendChild(div);
  161. };
  162. var getJSON = function(url, callback) {
  163. var xhr = new XMLHttpRequest();
  164. xhr.open('GET', url, true);
  165. xhr.responseType = 'json';
  166. xhr.onload = function() {
  167. if (xhr.status === 200) {
  168. callback(null, xhr.response);
  169. } else {
  170. callback(xhr.status, xhr.response);
  171. }
  172. };
  173. xhr.send();
  174. };
  175. document.getElementById("status").textContent = "Loading from Database...";
  176. getJSON('weight.php', function(err, data) {
  177. if (err !== null) {
  178. document.getElementById("status").textContent = 'Error: ' + err;
  179. return;
  180. }
  181. if (typeof data.error !== 'undefined') {
  182. document.getElementById("status").textContent = 'Error: ' + data.error;
  183. return;
  184. }
  185. if (typeof data.users !== 'undefined') {
  186. var s = 'Received data for ' + data.users.length + ' users';
  187. if (data.users.length > 0) {
  188. s += ': ';
  189. for (var i = 0; i < data.users.length; i++) {
  190. s += '"' + data.users[i].name + '"';
  191. if (i < (data.users.length - 1)) {
  192. s += ', ';
  193. }
  194. }
  195. s += '. Rendering...';
  196. } else {
  197. s += '. Nothing to show!';
  198. }
  199. document.getElementById("status").textContent = s;
  200. renderUser(data);
  201. } else {
  202. document.getElementById("status").textContent = 'Error: no data';
  203. return;
  204. }
  205. });
  206. </script>
  207. </body>
  208. </html>