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