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 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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, id) {
  34. return {
  35. label: name,
  36. data: data,
  37. yAxisID: id,
  38. backgroundColor: [
  39. 'rgba(' + color + ', 0)'
  40. ],
  41. borderColor: [
  42. 'rgba(' + color + ', 1)'
  43. ],
  44. pointBorderColor: 'rgba(' + color + ', 1)',
  45. pointRadius: 2,
  46. borderWidth: 4
  47. };
  48. };
  49. var addToForm = function(form, element, name) {
  50. var fe = document.createElement('p');
  51. fe.innerHTML = name + ' ';
  52. fe.appendChild(element);
  53. form.appendChild(fe);
  54. };
  55. var renderUser = function(data) {
  56. var div = document.createElement('div');
  57. var ctx = document.createElement('canvas');
  58. var min_date = new Date('3000-01-01T00:00:00');
  59. var max_date = new Date('1000-01-01T00:00:00');
  60. var min_kilo = 1000;
  61. var max_kilo = 0;
  62. for (var i = 0; i < data.users.length; i++) {
  63. for (var j = 0; j < data.users[i].data.length; j++) {
  64. temp_date = new Date(data.users[i].data[j].x);
  65. temp_kilo = parseInt(data.users[i].data[j].y);
  66. if (temp_date < min_date) {
  67. min_date = temp_date;
  68. }
  69. if (temp_date > max_date) {
  70. max_date = temp_date;
  71. }
  72. if (temp_kilo < min_kilo) {
  73. min_kilo = temp_kilo;
  74. }
  75. if (temp_kilo > max_kilo) {
  76. max_kilo = temp_kilo;
  77. }
  78. }
  79. }
  80. var days = (max_date - min_date) / 1000 / 60 / 60 / 24;
  81. var pixels_per_day = 3000 / 365;
  82. var width_px = days * pixels_per_day;
  83. var kilos = (max_kilo - min_kilo);
  84. var pixels_per_kilo = 1400 / 100;
  85. var height_px = kilos * pixels_per_kilo;
  86. var datasets = [];
  87. for (var i = 0; i < data.users.length; i++) {
  88. for (var j = 0; j <= 1; j++) {
  89. var set = createDataset(data.users[i].name,
  90. data.users[i].data, colorFromIndex(i), j ? 'A' : 'B');
  91. datasets.push(set);
  92. }
  93. }
  94. var chart = new Chart(ctx, {
  95. type: 'line',
  96. data: {
  97. datasets: datasets
  98. },
  99. options: {
  100. maintainAspectRatio: false,
  101. scales: {
  102. xAxes: [{
  103. type: 'time',
  104. time: {
  105. unit: 'day'
  106. }
  107. }],
  108. yAxes: [{
  109. id: 'A',
  110. type: 'linear',
  111. position: 'left'
  112. }, {
  113. id: 'B',
  114. type: 'linear',
  115. position: 'right'
  116. }]
  117. },
  118. events: [
  119. 'mousemove', 'mouseout', 'click',
  120. 'touchstart', 'touchmove', 'touchend'
  121. ],
  122. tooltips: {
  123. mode: 'nearest',
  124. intersect: false,
  125. axis: 'xy'
  126. }
  127. }
  128. });
  129. if (data.users.length > 0) {
  130. div_wrapper = document.createElement('div');
  131. div_wrapper.setAttribute('style', 'width: ' + Math.ceil(width_px) + 'px;');
  132. div_wrapper.style.width = Math.ceil(width_px) + 'px';
  133. div_wrapper.style.height = height_px + 'px';
  134. div_wrapper.width = Math.ceil(width_px);
  135. div_wrapper.height = height_px;
  136. div_wrapper.appendChild(ctx);
  137. div_wrapper2 = document.createElement('div');
  138. div_wrapper2.id = 'scroller';
  139. div_wrapper2.setAttribute('style', 'overflow-x: auto; overflow-y: auto;');
  140. div_wrapper2.appendChild(div_wrapper);
  141. div.appendChild(div_wrapper2);
  142. div.appendChild(document.createElement('hr'));
  143. var date = new Date();
  144. var hours = date.getHours();
  145. hours = (hours < 10) ? '0' + hours : hours;
  146. var minutes = date.getMinutes();
  147. minutes = (minutes < 10) ? '0' + minutes : minutes;
  148. var form_append = document.createElement('form');
  149. form_append.setAttribute('method', 'post');
  150. form_append.setAttribute('action', 'weight.php');
  151. var p_append = document.createElement('p');
  152. p_append.innerHTML = 'Add new Data-Point:';
  153. form_append.appendChild(p_append);
  154. var name_append = document.createElement('select');
  155. for (var i = 0; i < data.users.length; i++) {
  156. var option = document.createElement('option');
  157. option.text = data.users[i].name;
  158. option.value = 'user_' + data.users[i].id;
  159. name_append.add(option);
  160. }
  161. name_append.setAttribute('name', 'username');
  162. name_append.required = true;
  163. addToForm(form_append, name_append, 'Username:');
  164. var date_append = document.createElement('input');
  165. date_append.setAttribute('type', 'date');
  166. date_append.setAttribute('name', 'date');
  167. date_append.setAttribute('placeholder', 'Date');
  168. date_append.required = true;
  169. date_append.value = date.toISOString().slice(0, 10);
  170. addToForm(form_append, date_append, 'Date:');
  171. var time_append = document.createElement('input');
  172. time_append.setAttribute('type', 'time');
  173. time_append.setAttribute('name', 'time');
  174. time_append.setAttribute('placeholder', 'Time');
  175. time_append.required = true;
  176. time_append.value = hours + ':' + minutes;
  177. addToForm(form_append, time_append, 'Time:');
  178. var weight_append = document.createElement('input');
  179. weight_append.setAttribute('type', 'number');
  180. weight_append.setAttribute('name', 'weight');
  181. weight_append.setAttribute('step', '0.1');
  182. weight_append.min = 42.0;
  183. weight_append.max = 999.9;
  184. weight_append.setAttribute('placeholder', 'Weight');
  185. weight_append.required = true;
  186. addToForm(form_append, weight_append, 'Weight:');
  187. var submit_append = document.createElement('input');
  188. submit_append.setAttribute('type', 'submit');
  189. submit_append.setAttribute('value', 'Add Data');
  190. form_append.appendChild(submit_append);
  191. div.appendChild(form_append);
  192. div.appendChild(document.createElement('hr'));
  193. }
  194. var form_new = document.createElement('form');
  195. form_new.setAttribute('method', 'post');
  196. form_new.setAttribute('action', 'weight.php');
  197. var p_new = document.createElement('p');
  198. p_new.innerHTML = 'Add new User:';
  199. form_new.appendChild(p_new);
  200. var name_new = document.createElement('input');
  201. name_new.setAttribute('type', 'text');
  202. name_new.setAttribute('name', 'username');
  203. name_new.setAttribute('placeholder', 'Name');
  204. name_new.required = true;
  205. addToForm(form_new, name_new, 'Username:');
  206. var submit_new = document.createElement('input');
  207. submit_new.setAttribute('type', 'submit');
  208. submit_new.setAttribute('value', 'Add User');
  209. form_new.appendChild(submit_new);
  210. div.appendChild(form_new);
  211. div.appendChild(document.createElement('hr'));
  212. document.getElementById("content").appendChild(div);
  213. document.getElementById("scroller").scrollLeft += width_px * 4;
  214. };
  215. var getJSON = function(url, callback) {
  216. var xhr = new XMLHttpRequest();
  217. xhr.open('GET', url, true);
  218. xhr.responseType = 'json';
  219. xhr.onload = function() {
  220. if (xhr.status === 200) {
  221. callback(null, xhr.response);
  222. } else {
  223. callback(xhr.status, xhr.response);
  224. }
  225. };
  226. xhr.send();
  227. };
  228. document.getElementById("status").textContent = "Loading from Database...";
  229. getJSON('weight.php', function(err, data) {
  230. if (err !== null) {
  231. document.getElementById("status").textContent = 'Error: ' + err;
  232. return;
  233. }
  234. if (typeof data.error !== 'undefined') {
  235. document.getElementById("status").textContent = 'Error: ' + data.error;
  236. return;
  237. }
  238. if (typeof data.users !== 'undefined') {
  239. var s = 'Received data for ' + data.users.length + ' users';
  240. if (data.users.length > 0) {
  241. s += ': ';
  242. for (var i = 0; i < data.users.length; i++) {
  243. s += '"' + data.users[i].name + '"';
  244. if (i < (data.users.length - 1)) {
  245. s += ', ';
  246. }
  247. }
  248. s += '. Rendering...';
  249. } else {
  250. s += '. Nothing to show!';
  251. }
  252. document.getElementById("status").textContent = s;
  253. renderUser(data);
  254. } else {
  255. document.getElementById("status").textContent = 'Error: no data';
  256. return;
  257. }
  258. });
  259. </script>
  260. </body>
  261. </html>