-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_components.js
78 lines (62 loc) · 2.4 KB
/
data_components.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
class DataSource {
constructor(timeComponent) {
this.timeComponent = timeComponent;
timeComponent.timeEventEmitter.addListener('timeChange', () => this.handleTimeChange());
this.startDate = this.timeComponent.startDate;
this.endDate = this.timeComponent.endDate;
this.sankeyData = {};
this.sankeyDataFull = {};
this.targetCity = null;
// TODO Create these events
this.dataEventEmitter = new EventEmitter();
this.dataEventEmitter.defineEvents(['sankeyDataAvailable', 'causesDataAvailable', 'airportChanged']);
this.aggregationBy = null;
this.retrieveData(this.startDate, this.endDate, undefined);
}
__setAirport(dataSrc, newAirport) {
dataSrc.airport = newAirport;
dataSrc.airportTimer = null;
dataSrc.dataEventEmitter.emit('airportChanged');
dataSrc.getSankey(newAirport);
}
setAirport(newAirport) {
if (this.airportTimer !== null) {
clearTimeout(this.airportTimer);
}
this.airportTimer = setTimeout(() => this.__setAirport(this, newAirport), 10);
}
handleAggregationChange() {
// TODO: make the timeslider emit an event when the aggregation changes
}
handleTimeChange() {
this.startDate = this.timeComponent.selectedStartDate;
this.endDate = this.timeComponent.selectedEndDate;
this.retrieveData(this.startDate, this.endDate, this.aggregationBy);
}
getSankey(airport) {
this.sankeyData = this.sankeyDataFull[airport];
this.dataEventEmitter.emit('sankeyDataAvailable');
}
retrieveData(from, to, aggregation) {
// TODO Aggregation of sankey data
let month = from.getMonth() + 1;
let monthStr = "";
if (month < 10) {
monthStr += "0";
}
monthStr += month;
const jsonFile = "sankey_data/data_" + from.getFullYear() + "_" + monthStr + ".json";
d3.json(jsonFile, (js) => {
this.sankeyDataFull = js;
for (let airport in js) {
js[airport].links.forEach((l) => {
l.source = +l.source;
l.target = +l.target;
l.value = +l.value;
});
}
this.sankeyData = this.sankeyDataFull[this.airport];
this.dataEventEmitter.emit('sankeyDataAvailable');
});
}
}