-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSonoUnificado
258 lines (199 loc) · 6.46 KB
/
SonoUnificado
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
//Y - A X I S D A T A
// -> Y <-
let pages = dv
.pages('"Jornada/Notas Diárias"')
.filter(p => {
// Converter o nome do arquivo da página atual para um objeto moment e formatá-lo
let dateOfFile = window.moment(p.file.name, "YYYY-MM-DD");
let formattedDate = dateOfFile.isValid() ? dateOfFile.format("YYYY-MM-DD") : null;
// Verificar se a data formatada está dentro do intervalo especificado
return formattedDate && window.moment(formattedDate).isSameOrAfter(window.moment(input.START)) && window.moment(formattedDate).isSameOrBefore(window.moment(input.END));
})
.sort(p => p.file.name);
const ratingData = pages.notadoDia;
const sleepStart = pages.sleepStart;
const sleepEnd = pages.sleepEnd;
const sleepRatingData = pages.notadoSono;
// went to bed / wake-up
// HH:mm 24h
const sleepStartStop = sleepStart.values.map((value, index) => {
return [value, sleepEnd.values[index]];
});
const sleepStartStopNumber = sleepStart.values.map((value, index) => {
let startHours = parseInt(value.split(':')[0]);
let startMinutes = parseInt(value.split(':')[1]);
let endHours = parseInt(sleepEnd.values[index].split(':')[0]);
let endMinutes = parseInt(sleepEnd.values[index].split(':')[1]);
// Converte horas e minutos em formato decimal
let startDecimal = startHours + startMinutes / 60;
let endDecimal = endHours + endMinutes / 60;
// Ajusta o horário de início se for após as 20h para refletir como valor negativo no gráfico
if (startHours > 20) {
startDecimal -= 24;
}
return [startDecimal, endDecimal];
});
// Sleep Duration
// Supondo que sleepStart.values e sleepEnd.values contêm os horários de início e fim do sono respectivamente
let sleepDurationDecimals = sleepStart.values.map((startValue, index) => {
// Extrai horas e minutos do horário de início
let [startHour, startMinute] = startValue.split(':').map(Number);
let startMinutesTotal = startHour * 60 + startMinute;
// Extrai horas e minutos do horário de fim
let endValue = sleepEnd.values[index];
let [endHour, endMinute] = endValue.split(':').map(Number);
let endMinutesTotal = endHour * 60 + endMinute;
// Ajusta para quando o sono passa da meia-noite
if (endMinutesTotal < startMinutesTotal) {
endMinutesTotal += 24 * 60; // Adiciona 24 horas em minutos ao total de fim
}
// Calcula a duração total em minutos
let durationMinutes = endMinutesTotal - startMinutesTotal;
// Converte a duração para decimal (horas)
return durationMinutes / 60;
});
// Label X data config
// -> X <-
let timeDate = pages.map(p => {
let dateOfFile = window.moment(p.file.name,"YYYY-MM-DD");
return dateOfFile.isValid() ? dateOfFile.format("YYYY-MM-DD") : "Invalid Date";
s
})
.filter(date => date !== "Invalid Date") // Filtra datas inválidas.
.sort((a, b) => window.moment(a).diff(window.moment(b))); // diff é uma função que calcula a diferença Ordena do mais velho para o mais novo.
// Config Chart
const chartData = {
beginAtZero: true,
data: {
labels: timeDate,
datasets: [
{
yAxisID: 'A',
type: 'line',
label: 'day rating',
data: ratingData.values,
borderColor: ['rgb(255, 99, 132)'],
borderWidth: 1,
bestFit: true,
},
{
yAxisID: 'B',
type: 'line',
label: 'Sleep Duration',
data: sleepDurationData,
borderColor: ['rgb(54, 162, 235)'],
borderWidth: 1,
bestFit: true,
},
{
yAxisID: 'A',
type: 'line',
label: 'Sleep rate',
data: sleepRatingData.values,
borderColor: ['rgb(255, 206, 86)'],
borderWidth: 1,
bestFit: true,
},
{
yAxisID: 'B',
type: 'bar',
label: 'wake-up/went to bed',
data: sleepStartStopNumber,
backgroundColor: ['rgb(153, 102, 255, 0.2)'],
borderColor: ['rgb(153, 102, 255, 1)'],
bestFit: true,
borderWidth: { top: 0, bottom: 0, right: 0, left: 2 },
},
],
},
options: {
//Explicações de options até scales abaixo //
plugins: {
tooltip: {
callbacks: {
label: function(context) {
if (context.dataset.label === 'wake-up/went to bed') {
let value = context.raw;
const formatTime = (time) => {
let hours = Math.floor(time);
let minutes = Math.floor((time - hours) * 60);
hours = hours < 0 ? 24 + hours : hours;
return hours.toString().padStart(2, '0') + ':' + minutes.toString().padStart(2, '0');
};
const startTime = formatTime(value[0]);
const endTime = formatTime(value[1]);
return `went to bed: ${startTime}, wake-up: ${endTime}`;
}
// Se o dataset for "Horas de sono", mostra horas e minutos literalmente
else if (context.dataset.label === 'Sleep Duration') {
let totalMinutes = context.raw * 60; // Converte horas em minutos
let hours = Math.floor(totalMinutes / 60);
let minutes = Math.floor(totalMinutes % 60); // Usa Math.floor para evitar arredondamento
return `${context.dataset.label}: ${hours}h ${minutes}m`;
}
// Para outros datasets, mantém o formato padrão
else {
return `${context.dataset.label}: ${context.formattedValue}`;
}
}
}
}
},
scales: {
A: {
type: 'linear',
position: 'right',
beginAtZero: true,
min: 0,
max: 10,
title: {
display: true,
text: 'Rating(0-10) - Day or Sleep', // Título para a escala A
font: {
size: 15 // Ajuste o tamanho da fonte conforme necessário
}
}
},
B: {
type: 'linear',
position: 'left',
max: 13,
min: -3,
title: {
display: true,
text: 'Sleep Duratiomm and Bedtime/Sleep Duration ', // Título para a escala B
font: {
size: 15 // Ajuste o tamanho da fonte conforme necessário
}
},
ticks: {
fontSize: 40,
count: 17,
maxTicksLimit: 20,
callback: function (value, index, ticks) {
if (value < 0) {
value = 24 + value;
}
let minutes = value * 60;
let hours = Math.floor(minutes / 60);
minutes = minutes % 60;
if (minutes < 10) {
minutes = '0' + minutes;
}
if (hours < 10) {
hours = '0' + hours;
}
return hours + ':' + minutes;
},
},
},
x: {
ticks: {
maxRotation: 0,
minRotation: 0,
},
},
},
},
};
input.func(chartData, dv.container);