-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodel.js
More file actions
167 lines (144 loc) · 4.3 KB
/
model.js
File metadata and controls
167 lines (144 loc) · 4.3 KB
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
/**
* This class contains the data model used in this visualization tool.
*
* Selections, aggregations and maps are created and cached for increased performance.
*
* @param {serviceName, regions, transitions, states, additionalData, dataServiceParameters, networkName, directedTransitions, geographicNodes}
*/
class Model {
constructor(param) {
const states = param.states || null;
const transitions = param.transitions || [];
// parameters that were used to load data from a data service
this.dataServiceParameters = param.dataServiceParameters;
// custom data stored by the connector
this.additionalData = param.additionalData || {};
// common information about the data model
this.properties = {
serviceName: param.serviceName || "serviceName",
networkName: param.networkName || "networkName",
geographicNodes: param.geographicNodes || false,
directedTransitions: param.directedTransitions || false,
dynamicTransitions: param.dynamicTransitions || false,
showRegionNames: param.showRegionNames || false
};
this.regions = param.regions || [];
this.regionMap = new Map();
this.transitions;
this.transitionMap = new Map();
// Preprocess regions and populate map
this.regions.forEach(r => {
if (this.properties.geographicNodes) {
this.preprocessGeoRegion(r);
}
r.highlight = { h: false };
this.regionMap.set(+r.properties.id, r);
});
// Preprocess transitions (if needed)
// and populate map.
this.transitions = transitions.map(t => {
const tr = {
id: +t.id,
weight: +t.weight,
oWeight: +t.weight,
highlight: { h: false },
startTimestamp: t.startTimestamp,
endTimestamp: t.endTimestamp,
source: this.regionMap.get(+t.sourceRegionId),
target: this.regionMap.get(+t.targetRegionId),
sourceRegionId: t.sourceRegionId,
targetRegionId: t.targetRegionId,
text: t.text
};
this.transitionMap.set(+t.id, tr);
return tr;
});
/**
* Preprocessing steps for states.
*/
// aggregated data array, each entry looks like this: {<date> timestamp, <Array> amount}
this.states;
this.timeRange = [
-8640000000000000,
8640000000000000
];
if (states) {
// set date ranges
const minDate = lib.baseTimestamp;
const maxDate = lib.addDays(minDate, states.length + 1);
this.timeRange = [minDate, maxDate];
this.states = states;
}
}
/**
* Preprocessing steps for geographical regions such as computing
* geographical bounding boxes returns array of regions
*/
preprocessGeoRegion(r) {
// calculate centroid
r.properties.centroid = d3.geo.centroid(r);
// calculate bounding box
r.properties.bbox = d3.geo.bounds(r);
}
/**
* Serialization for [_broadcaster.js](..types/_broadcaster.js),
* so model can be shared between browser windows.
*/
serialize() {
const clone = {
additionalData: {},
serviceName: this.properties.serviceName,
networkName: this.properties.networkName,
geographicNodes: this.properties.geographicNodes,
directedTransitions: this.properties.directedTransitions,
dynamicTransitions: this.properties.dynamicTransitions,
showRegionNames: this.properties.showRegionNames,
dataServiceParameters: this.dataServiceParameters
};
clone.transitions = this.transitions.map((transition) => {
const result = {
id: transition.id,
text: transition.text,
weight: transition.weight,
sourceRegionId: transition.source.properties.id,
targetRegionId: transition.target.properties.id
};
if (transition.endTimestamp) {
result.endTimestamp = transition.endTimestamp;
}
if (transition.startTimestamp) {
result.startTimestamp = transition.startTimestamp;
}
return result;
});
clone.regions = this.regions.map((region) => {
const result = {
properties: {
code: region.properties.code,
id: region.properties.id,
name: region.properties.name
}
};
if (region.geometry) {
result.geometry = {
coordinates: region.geometry.coordinates,
type: region.geometry.type
};
}
if (region.type) {
result.type = region.type;
}
return result;
});
if (this.states) {
clone.states = this.states.map((state) => {
return {
amount: state.amount,
date: state.date
};
});
}
Object.assign(clone.additionalData, this.additionalData);
return clone;
};
}