forked from antsutherland/SeniorCapstoneProject
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dategraph.js
474 lines (345 loc) · 7.2 KB
/
dategraph.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
import React, { CSSProperties } from "react";
import { useCSVReader } from "react-papaparse";
import ReactDOM from "react-dom";
import {
Chart as ChartJS,
CategoryScale,
LinearScale,
PointElement,
LineElement,
Title,
Tooltip,
Legend,
} from "chart.js";
import { Line } from "react-chartjs-2";
ChartJS.register(
CategoryScale,
LinearScale,
PointElement,
LineElement,
Title,
Tooltip,
Legend
);
const styles = {
csvReader: {
display: "flex",
flexDirection: "row",
marginBottom: 10
},
browseFile: {
width: "20%",
},
acceptedFile: {
border: "1px solid #ccc",
height: 45,
lineHeight: 2.5,
paddingLeft: 10,
width: "80%"
},
remove: {
borderRadius: 0,
padding: "0 20px"
},
progressBarBackgroundColor: {
backgroundColor: "red"
}
};
//Function for swapping an array's rows and columns
const arrayColumn = (arr, n) => arr.map(x => x[n]);
//Globals used in a few functions
var labels = [];
var tdat = [];
var data = {};
var lsize;
//Function for drawing the graph
export function draw() {
var c1 = document.getElementById("Sun").checked;
var c2 = document.getElementById("Mon").checked;
var c3 = document.getElementById("Tue").checked;
var c4 = document.getElementById("Wed").checked;
var c5 = document.getElementById("Thu").checked;
var c6 = document.getElementById("Fri").checked;
var c7 = document.getElementById("Sat").checked;
console.log(c1);
var dsets = [];
var lset = [];
var dval = document.getElementById("drange").options[document.getElementById("drange").selectedIndex].value;
var l;
var d;
//Grab the columns associated with specific data types
if (dval == "Avg") {
l = "Average";
d = arrayColumn(tdat, 3);
}
else if (dval == "Min") {
l = "Minimum";
d = arrayColumn(tdat, 4);
}
else if (dval == "Max") {
l = "Maximum";
d = arrayColumn(tdat, 8);
}
d = d.slice(1, lsize);
//Options set in here so they can be variable. Only delineates every week
var options = {
responsive: true,
maintainAspectRatio: false,
plugins: {
legend: {
position: "top",
labels: {
font: {
size: 20
}
}
},
title: {
display: true,
text: l,
font: {
size: 30
}
},
},
scales : {
x: {
ticks: {
callback: function(index) {
return index % 24 === 0 ? "Week " + ((index / 24) + 1) : "";
},
autoSkip: false,
font: {
size: 20
}
},
grid : {
display: false
}
},
y: {
min: 0,
title: {
display: true,
text: "Blood Glucose (mg/dL)",
font: {
size: 24
}
},
ticks: {
font: {
size: 20
}
}
}
}
};
var sund = [];
var mond = [];
var tued = [];
var wedd = [];
var thud = [];
var frid = [];
var satd = [];
var dateval;
var count;
var track;
//Loops through assigning data to sepcific days
for (var i = 0; i < labels.length; i++) {
dateval = new Date(labels[i].split(" ")[0]);
if (i === 0) {
count = 0;
track = dateval.getDay();
}
if (dateval.getDay() == track) {
count = count + 1;
lset.push("Hour " + count);
}
switch (dateval.getDay()) {
case 0:
sund.push(d[i]);
break;
case 1:
mond.push(d[i]);
break;
case 2:
tued.push(d[i]);
break;
case 3:
wedd.push(d[i]);
break;
case 4:
thud.push(d[i]);
break;
case 5:
frid.push(d[i]);
break;
case 6:
satd.push(d[i]);
default:
break;
}
}
var sun = {
data: sund,
label: "Sunday",
borderColor: "rgb(230, 25, 75)",
backgroundColor: "rgba(230, 25, 75, 0.25)"
};
var mon = {
data: mond,
label: "Monday",
borderColor: "rgb(245, 130, 48)",
backgroundColor: "rgba(245, 130, 48, 0.25)"
};
var tue = {
data: tued,
label: "Tuesday",
borderColor: "rgb(255, 255, 25)",
backgroundColor: "rgba(255, 255, 25, 0.25)"
};
var wed = {
data: wedd,
label: "Wednesday",
borderColor: "rgb(60, 180, 75)",
backgroundColor: "rgba(60, 180, 75, 0.25)"
};
var thu = {
data: thud,
label: "Thursday",
borderColor: "rgb(0, 130, 200)",
backgroundColor: "rgba(0, 130, 200, 0.25)"
};
var fri = {
data: frid,
label: "Friday",
borderColor: "rgb(240, 50, 230)",
backgroundColor: "rgba(240, 50, 230, 0.25)"
};
var sat = {
label: "Saturday",
data: satd,
borderColor: "rgb(128, 128, 128)",
backgroundColor: "rgba(128, 128, 128, 0.25)"
};
if (c1 != true && c2 != true && c3 != true && c4 != true && c5 != true && c6 != true && c7 != true) {
alert("Please check at least one day.");
return;
}
//Add data to the graph's dataset if the specific box is checked
if (c1) {
dsets.push(sun);
}
if (c2) {
dsets.push(mon);
}
if (c3) {
dsets.push(tue);
}
if (c4) {
dsets.push(wed);
}
if (c5) {
dsets.push(thu);
}
if (c6) {
dsets.push(fri);
}
if (c7) {
dsets.push(sat);
}
console.log(dsets);
data = {
labels: lset,
datasets: dsets
};
var w = window.innerWidth;
var h = window.innerHeight;
if (w < 1000) {
w = 1000;
options.responsive = "false";
}
console.log(w);
console.log(options.responsive);
ReactDOM.render(<div id="gg"><Line overflow-x={"scroll"} width={w} height={h} options={options} data={data} /></div>, document.getElementById("root"));
};
//As of right now, the CSV Reader loading thing is from the offical react-papaparse website
export default function GraphStart() {
const { CSVReader } = useCSVReader();
return (
<>
<CSVReader
onUploadAccepted={(results: any) => {
console.log("---------------------------");
console.log(results);
console.log("---------------------------");
console.log(results.data[1]);
tdat = results.data;
labels = arrayColumn(results.data, 1);
labels = labels.slice(1, (labels.length - 1));
lsize = labels.length;
console.log(labels);
}}
>
{({
getRootProps,
acceptedFile,
ProgressBar,
getRemoveFileProps,
}: any) => (
<>
<div style={styles.csvReader}>
<button type="button" {...getRootProps()} style={styles.browseFile}>
Browse file
</button>
<div style={styles.acceptedFile}>
{acceptedFile && acceptedFile.name}
</div>
<button {...getRemoveFileProps()} style={styles.remove}>
Remove
</button>
</div>
<ProgressBar style={styles.progressBarBackgroundColor} />
</>
)}
</CSVReader>
<p>Choose the days you want displayed:</p>
<div>
<input type="checkbox" id="Sun"></input>
<label for="Sun">Sunday</label>
</div>
<div>
<input type="checkbox" id="Mon"></input>
<label for="Mon">Monday</label>
</div>
<div>
<input type="checkbox" id="Tue"></input>
<label for="Tue">Tuesday</label>
</div>
<div>
<input type="checkbox" id="Wed"></input>
<label for="Wed">Wednesday</label>
</div>
<div>
<input type="checkbox" id="Thu"></input>
<label for="Thu">Thursday</label>
</div>
<div>
<input type="checkbox" id="Fri"></input>
<label for="Fri">Friday</label>
</div>
<div>
<input type="checkbox" id="Sat"></input>
<label for="Sat">Saturday</label>
</div>
<p>Select an hourly data type:</p>
<select id="drange">
<option value="Avg">Average</option>
<option value="Min">Minimum</option>
<option value="Max">Maximum</option>
</select>
<p></p>
<button onClick={draw}>Draw Graph</button>
</>
);
}