-
Notifications
You must be signed in to change notification settings - Fork 1
/
diabgraph.js
368 lines (263 loc) · 5.69 KB
/
diabgraph.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
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'
}
};
//A simple function to swap an array's rows and columns
const arrayColumn = (arr, n) => arr.map(x => x[n]);
//Chart.js options
export const options = {
responsive: true,
maintainAspectRatio: false,
plugins: {
legend: {
position: 'top',
labels: {
font: {
size: 20
}
}
},
title: {
display: true,
text: 'Diabetes Data',
font: {
size: 30
}
},
},
scales : {
x: {
ticks: {
font: {
size: 20
}
},
grid : {
display: false
}
},
y: {
min: 0,
title : {
display: true,
text: "Blood Glucose (mg/dL)",
font: {
size: 24
}
},
ticks: {
font: {
size: 20
}
}
}
}
};
//Globals used in several functions
var labels = [];
var tdat = [];
var data = {};
var lsize;
//Draws the graph
export function draw() {
var c1 = document.getElementById("Avg").checked;
var c2 = document.getElementById("Min").checked;
var c3 = document.getElementById("Max").checked;
console.log(c1);
var dsets = [];
var lset = labels;
var sub;
var glen;
var dval = document.getElementById("trange").options[document.getElementById("trange").selectedIndex].value;
//Determines how far back in the array the graph will start reading
if (dval == "1day") {
sub = 24;
}
else if (dval == "3day") {
sub = 72;
}
else if (dval == "1week") {
sub = 168;
}
else if (dval == "2week") {
sub = 336
}
else if (dval == "1month") {
sub = 720;
}
else if (dval == "2month") {
sub = 1440;
}
else if (dval == "3month") {
sub = 2160;
}
glen = lsize - sub;
if (glen < 0) {
alert("Not enough user data. Please select a shorter time span.");
return;
}
lset = lset.slice(glen, lsize);
console.log(lset);
//Data for the graph
var cavg = {
label: "Average",
data: arrayColumn(tdat, 3).slice(glen, lsize),
borderColor: 'rgb(255, 99, 132)',
backgroundColor: 'rgba(255, 99, 132, 0.5)'
};
var cmin = {
label: "Minimum",
data: arrayColumn(tdat, 4).slice(glen, lsize),
borderColor: 'rgb(99, 132, 255)',
backgroundColor: 'rgba(99, 132, 255, 0.5)'
};
var cmax = {
label: "Maximum",
data: arrayColumn(tdat, 8).slice(glen, lsize),
borderColor: 'rgb(132, 255, 99)',
backgroundColor: 'rgba(132, 255, 99, 0.5)'
};
if (c1 != true && c2 != true && c3 != true) {
alert("Please check at least one data type.");
return;
}
//Adds the data to the graph's set if the appropriate checkbox is clicked
if (c1) {
dsets.push(cavg);
}
if (c2) {
dsets.push(cmin);
}
if (c3) {
dsets.push(cmax);
}
console.log(dsets);
data = {
labels: lset,
datasets: dsets
};
var w = window.innerWidth;
var h = window.innerHeight;
if (w < 1000) {
w = 1200;
ReactDOM.render(<div id="gg" style={{height:"150%", width: "300%"}}><Line overflow-x={"scroll"} width={w} height={h} options={options} data={data} /></div>, document.getElementById("root"));
}
else {
ReactDOM.render(<div id="gg"><Line overflow-x={"scroll"} width={w} height={h} options={options} data={data} /></div>, document.getElementById("root"));
}
console.log(w);
console.log(h);
console.log(options.responsive);
console.log(options.maintainAspectRatio);
};
//As of right now, the CSVReader loading thing is from the official 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 your hourly data:</p>
<div>
<input type="checkbox" id="Avg"></input>
<label for="Avg">Average</label>
</div>
<div>
<input type="checkbox" id="Min"></input>
<label for="Min">Minimum</label>
</div>
<div>
<input type="checkbox" id="Max"></input>
<label for="Max">Maximum</label>
</div>
<p>Select a time range:</p>
<select id="trange">
<option value="1day">1 Day</option>
<option value="3day">3 Days</option>
<option value="1week">1 Week</option>
<option value="2week">2 Weeks</option>
<option value="1month">1 Month</option>
<option value="2month">2 Months</option>
<option value="3month">3 Months</option>
</select>
<p></p>
<button onClick={draw}>Draw Graph</button>
</>
);
}