-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathindex.js
233 lines (208 loc) · 6.27 KB
/
index.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
var types = [];
var parseTime = d3.timeParse('%Y-%m-%d %H:%M:%S %Z');
var loader = document.querySelector('.loader');
var u = new Utils();
var t;
/*
* Events listeners
*/
window.addEventListener('load', handleWindowLoad);
/*
* Subscriptions
*/
Events.on('data/load/done', handleDataLoadDone);
Events.on('data/map/done', handleDataMapDone);
Events.on('data/load/sleepcycle/done', handleSleepDataLoad);
/*
* Handles window load event
*/
function handleWindowLoad() {
loadData();
t = new TimeFilter('timeFilter');
}
/*
* Handles data load done event
*/
function handleDataLoadDone(data) {
hideLoader();
}
/*
* Hide loader DOM element using a classname
*/
function hideLoader() {
if (!loader.classList.contains('hide')) {
loader.classList.add('hide');
}
}
/*
* Loads data and passes the callback to a mapping function
*/
function loadData() {
d3.xml('./index.xml', mapData);
}
/*
* XML parsing source: https://bl.ocks.org/mbostock/ec585e034819c06f5c99
* - Maps data records to object: {type: string, value, number|null, startDate: Date, endDate: Date, creationDate: Date}
* - Emits event
*/
function mapData(error, data) {
if (error) {
throw error;
}
data = [].map.call(data.querySelectorAll('Record'), function (record) {
return {
type: parseType(record.getAttribute('type')),
value: parseValue(record.getAttribute('value')),
startDate: parseTime(record.getAttribute('startDate')),
endDate: parseTime(record.getAttribute('endDate')),
creationDate: parseTime(record.getAttribute('creationDate'))
}
});
Events.emit('data/map/done', {
data: data
});
}
/*
* Handles data mapping done event
* - Get sleepCycle data
* - Transform sleepCycle data to data entry per day
* - Get maximal date found in data
* - Set default date range to max date and set type to month filter view
* - Filter the transformed data to the defaul range
* - Create SleepCycle with dataObject as parameter
* - Emit event
*/
function handleDataMapDone(data) {
var sleepAnalysisData = getSleepAnalysisData(data.data);
var sleepAnalysisDataTransformed = u.createSleepCyclePerDay(sleepAnalysisData, [21, 3], [5, 11]);
var maxDate = u.getMaxDate(sleepAnalysisDataTransformed);
var defaultRange = u.getDefaultRange(maxDate, 'month')
var sleepAnalysisDataFiltered = u.filterDataOnDate(defaultRange.startDate, defaultRange.endDate, sleepAnalysisDataTransformed);
var SleepAnalysisSleepCycle = new SleepCycle({
data: sleepAnalysisDataFiltered,
selector: '#sleepCycle',
type: 'sleepCycle',
startDate: defaultRange.startDate,
endDate: defaultRange.endDate,
minTime: u.getMinTime(sleepAnalysisDataFiltered, 3),
maxTime: u.getMaxTime(sleepAnalysisDataFiltered),
yLabel: 'Hours'
});
Events.emit('data/load/sleepcycle/done', {
minDate: defaultRange.startDate,
maxDate: defaultRange.endDate,
data: data.data
});
}
/*
* Handles sleep data load event
*
* - Loads step count data.
* - Draws step count barchart.
* - Loads walking running distance data.
* - Draws walking running distance barchart.
* - Loads flights climbed data.
* - Draws flights climbed barchart.
*/
function handleSleepDataLoad(data) {
var stepCountData = getStepCountData(data.data);
var distanceWalkingRunningData = getDistanceWalkingRunningData(data.data);
var flightsClimbedData = getFlightsClimbedData(data.data);
/*
* Step count
*/
var stepCountDataTransformed = u.mergeDataPerDay(stepCountData, 'stepCount');
var stepCountDataFiltered = u.filterDataOnDate(data.minDate, data.maxDate, stepCountDataTransformed);
var StepCountBarChart = new BarChart({
data: stepCountDataFiltered,
selector: '#stepCount',
type: 'stepCount',
startDate: data.minDate,
endDate: data.maxDate,
maxValue: u.getMaxValue(stepCountDataFiltered),
yLabel: 'Steps',
gradientStartColor: '#88F3E2',
gradientStopColor: '#50E3C2'
});
/*
* Walking distance
*/
var distanceWalkingRunningDataTransformed = u.mergeDataPerDay(distanceWalkingRunningData, 'walkingRunningDistance');
var distanceWalkingRunningDataFiltered = u.filterDataOnDate(data.minDate, data.maxDate, distanceWalkingRunningDataTransformed);
var DistanceWalkingRunningBarChart = new BarChart({
data: distanceWalkingRunningDataFiltered,
selector: '#distanceWalkingRunning',
type: 'walkingRunningDistance',
startDate: data.minDate,
endDate: data.maxDate,
maxValue: u.getMaxValue(distanceWalkingRunningDataFiltered),
yLabel: 'Distance Walking or Running',
gradientStartColor: '#80C3F3',
gradientStopColor: '#4A90E2'
});
/*
* Flights climbed
*/
var flightsClimbedDataTransformed = u.mergeDataPerDay(flightsClimbedData, 'flightsClimbed');
var flightsClimbedDataFiltered = u.filterDataOnDate(data.minDate, data.maxDate, flightsClimbedDataTransformed);
var FlightsClimbedBarChart = new BarChart({
data: flightsClimbedDataFiltered,
selector: '#flightsClimbed',
type: 'flightsClimbed',
startDate: data.minDate,
endDate: data.maxDate,
maxValue: u.getMaxValue(flightsClimbedDataFiltered),
yLabel: 'Flights climbed',
gradientStartColor: '#B5EB45',
gradientStopColor: '#7ED321'
});
Events.emit('data/load/done');
}
/*
* Returns data of the type 'StepCount'
*/
function getStepCountData(data) {
return data.filter(function (row) {
return row.type === 'StepCount';
});
}
/*
* Returns data of the type 'DistanceWalkingRunning'
*/
function getDistanceWalkingRunningData(data) {
return data.filter(function (row) {
return row.type === 'DistanceWalkingRunning';
});
}
/*
* Returns data of the type 'FlightsClimbed'
*/
function getFlightsClimbedData(data) {
return data.filter(function (row) {
return row.type === 'FlightsClimbed';
});
}
/*
* Returns data of the type 'SleepAnalysis'
*/
function getSleepAnalysisData(data) {
return data.filter(function (row) {
return row.type === 'SleepAnalysis';
});
}
/*
* Returns value parsed to a number or null
*/
function parseValue(value) {
return isNaN(Number(value)) ? null : Number(value);
}
/*
* Returns filtered type, removes prefixes from found type
*/
function parseType(type) {
type = type.replace(/HKQuantityTypeIdentifier|HKCategoryTypeIdentifier/g, '');
if (types.indexOf(type) === -1) {
types.push(type)
}
return type;
}