This repository was archived by the owner on Dec 4, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathvisualization.js
More file actions
321 lines (304 loc) · 14 KB
/
Copy pathvisualization.js
File metadata and controls
321 lines (304 loc) · 14 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
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
import Ember from 'ember';
import numeral from 'numeral';
const {computed, observer, get:get } = Ember;
export default Ember.Controller.extend({
i18n: Ember.inject.service(),
featureToggle: Ember.inject.service(),
queryParams: ['search', 'startDate', 'endDate'],
search: null,
rcaFilter: 'less',
startDate: null,
endDate: null,
searchText: null,
drawerSettingsIsOpen: false,
drawerChangeGraphIsOpen: false,
drawerQuestionsIsOpen: false,
firstYear: computed.alias('featureToggle.first_year'),
lastYear: computed.alias('featureToggle.last_year'),
metadata: computed.alias('model.metaData'),
source: computed.alias('model.source'),
entityId: computed.alias('model.entity.id'),
entity: computed.alias('model.entity'),
entityType: computed.alias('model.entity_type'),
visualization: computed.alias('model.visualization'),
variable: computed.alias('model.variable'),
isGeo: computed.equal('visualization','geo'),
isScatter: computed.equal('visualization','scatter'),
isFixedHeight: computed('model.visualization', function() {
let vis = this.get('model.visualization');
return _.contains(['geo', 'treemap', 'scatter', 'similarity'], vis) ? true : false;
}),
isOneYear: computed('startDate', 'endDate', function() {
return this.get('startDate') == this.get('endDate');
}),
isFiltered: computed('search', function() {
return Boolean(this.get('search'));
}),
graph: computed('model.metaData', 'source', function() {
let source = this.get('source');
if(source === 'industries') {
return this.get('model.metaData.industrySpace');
} else if (source === 'products') {
return this.get('model.metaData.productSpace');
}
}),
canYearToggle: computed('visualization', function() {
let visualization = this.get('visualization');
return visualization != 'multiples';
}),
years: computed('startDate', 'endDate', function() {
let start = parseInt(this.get('startDate'), 10);
let end = parseInt(this.get('endDate'), 10);
if(start === end) { return start; }
return `${start}–${end}`;
}),
dateExtent: computed('model.data.[]', function() {
if(this.get('model.data').length) {
return d3.extent(this.get('model.data'), function(d) { return d.year; });
}
return [this.get('firstYear'), this.get('lastYear')];
}),
dateRange: computed('dateExtent', function() {
return d3.range(this.get('dateExtent')[0], this.get('dateExtent')[1] + 1);
}),
entity_and_id: computed('entityId', 'entityType', function() {
return `${this.get('model.entity_type')}-${this.get('model.entity.id')}`;
}),
profileLink: computed('entityType', function() {
return `${this.get('model.entity_type')}.show`;
}),
legend: computed('source', function() {
let legend = this.get(`metadata.legend.${this.get('source')}`);
return _.values(legend);
}),
needsLegend: computed('model.visualization', function() {
let vis = this.get('model.visualization');
return _.contains(['scatter', 'similarity'], vis) ? true : false;
}),
rca: computed('source', function() {
let source = this.get('source');
if(source === 'products') {
return 'export_rca';
} else if( source === 'industries') {
return 'rca';
}
}),
name: computed('entityType', 'model.entity.name', 'i18n.locale', function() {
if(this.get('entityType') === 'location') {
return this.get('model.entity.name');
} else {
return `${this.get('model.entity.name')} (${this.get('entity.code')})`;
}
}),
i18nString: computed('entityType', 'entity', 'variable', 'i18n.locale', function() {
let i18nString = `${this.get('entityType')}.${this.get('source')}`;
let visualization = this.get('visualization');
if(visualization === 'scatter' || visualization === 'similarity') {
return `${i18nString}.${visualization}`;
}
return `${i18nString}.${this.get('variable')}`;
}),
pageTitle: computed('i18nString','entity.level', function() {
let i18nString = `graph_builder.page_title.${this.get('i18nString')}`;
if(this.get('entityType') === 'location') {
i18nString += `.${this.get('entity.level')}`;
}
return this.get('i18n').t(i18nString);
}),
visualizationExplanation: computed('i18nString', function() {
let i18nString = `graph_builder.explanation.${this.get('i18nString')}`;
return this.get('i18n').t(i18nString);
}),
builderModHeader: computed('model.name','i18nString', function() {
//locale file under graph_builder.builder_mod_header.<entity>.<source>.<variable>
let i18nString = `graph_builder.builder_mod_header.${this.get('i18nString')}`;
return this.get('i18n').t(i18nString, { name: this.get('model.entity.name') });
}),
recircCopy: computed('model','i18n.locale', 'entityType', function() {
//locale file under graph_builder.recirc.header
return this.get('i18n').t(`graph_builder.recirc.header.${this.get('entityType')}`);
}),
searchPlaceholderCopy: computed('source','i18n.locale', function() {
return this.get('i18n').t(`graph_builder.search.placeholder.${this.get('source')}`);
}),
headerValue: computed('model', 'visualization','filteredData', 'variable', 'i18n.locale', function() {
let allowedVariables = ['export_value', 'import_value', 'wages', 'employment'];
let variable = this.get('variable');
let data = this.get('filteredData');
let visualization = this.get('visualization');
if(! _.contains(allowedVariables, variable)){ return ''; }
if(visualization === 'multiples') { return ''; }
var sum = _.sum(data, variable);
if(variable === 'employment') {
return numeral(sum).format('0,00');
}
if(variable === 'export_value' || variable === 'import_value') {
return '$' + numeral(sum).format('0,0') + ' USD';
}
return numeral(sum).format('$ 0,0');
}),
otherPossibleGraphs: computed('model.visualization', 'model.source', function() {
let vis = this.get('model.visualization');
let source = this.get('model.source');
if(_.contains(['locations', 'departments'], source) && _.contains(['geo', 'treemap', 'multiples'], vis)){
return [
{ type: 'multiples', description: 'graph_builder.change_graph.multiples_description', available: true },
{ type: 'treemap', description: 'graph_builder.change_graph.treemap_description', available: true },
{ type: 'geo', description: 'graph_builder.change_graph.geo_description', available: true },
{ type: 'scatter', description: 'graph_builder.change_graph.scatter_description', available: false },
{ type: 'similarity', description: 'graph_builder.change_graph.similarity_description', available: false }
];
} else if (source === 'occupations' && _.contains(['treemap'], vis)){
return [
{ type: 'multiples', description: 'graph_builder.change_graph.multiples_description', available: false },
{ type: 'treemap', description: 'graph_builder.change_graph.treemap_description', available: true },
{ type: 'geo', description: 'graph_builder.change_graph.geo_description', available: false },
{ type: 'scatter', description: 'graph_builder.change_graph.scatter_description', available: false },
{ type: 'similarity', description: 'graph_builder.change_graph.similarity_description', available: false }
];
} else if (vis === 'scatter'){
return [
{ type: 'multiples', description: 'graph_builder.change_graph.multiples_description', available: false },
{ type: 'treemap', description: 'graph_builder.change_graph.treemap_description', available: false },
{ type: 'geo', description: 'graph_builder.change_graph.geo_description', available: false },
{ type: 'scatter', description: 'graph_builder.change_graph.scatter_description', available: true },
{ type: 'similarity', description: 'graph_builder.change_graph.similarity_description', available: false }
];
} else if (vis === 'similarity'){
return [
{ type: 'multiples', description: 'graph_builder.change_graph.multiples_description', available: false },
{ type: 'treemap', description: 'graph_builder.change_graph.treemap_description', available: false },
{ type: 'geo', description: 'graph_builder.change_graph.geo_description', available: false },
{ type: 'scatter', description: 'graph_builder.change_graph.scatter_description', available: false },
{ type: 'similarity', description: 'graph_builder.change_graph.similarity_description', available: true }
];
} else {
return [
{ type: 'multiples', description: 'graph_builder.change_graph.multiples_description', available: true },
{ type: 'treemap', description: 'graph_builder.change_graph.treemap_description', available: true },
{ type: 'geo', description: 'graph_builder.change_graph.geo_description', available: false },
{ type: 'scatter', description: 'graph_builder.change_graph.scatter_description', available: false },
{ type: 'similarity', description: 'graph_builder.change_graph.similarity_description', available: false }
];
}
}),
varDependent: computed('variable', 'source', function() {
// if variable exists, it is varDependent
if(this.get('variable')) { return this.get('variable'); }
let source = this.get('source');
if(source === 'products') {
return 'export_value';
} else if(source === 'locations') {
return '';
} else if(source === 'industries') {
return 'wages';
}
}),
singularEntity: computed('model.entity_type', 'i18n.locale', function() {
let entityType = this.get('model.entity_type');
return this.get('i18n').t(`general.${entityType}`);
}),
immutableData: computed('model.data.[]','endDate', 'startDate' , function() {
return this.filterToSelectedYears(this.get('model.data'), this.get('startDate'), this.get('endDate'));
}),
filteredData: computed('immutableData.[]', 'search', 'startDate', 'endDate', 'rcaFilter', function() {
let data = this.get('immutableData');
if(this.get('search')){ data = this.searchFilter(data); }
if(this.get('visualization') === 'scatter'){
let rca = this.get('rca');
let rcaFilter = this.get('rcaFilter');
if(rcaFilter === 'less') {
return _.filter(data, (d) => { return _.get(d,rca) <= 1;});
}
if (rcaFilter === 'greater') {
return _.filter(data, (d) => { return _.get(d,rca) > 1;});
}
}
return data;
}),
visualizationComponent: computed('visualization', function(){
let visualization = this.get('visualization');
if(visualization === 'treemap') {
return 'vistk-treemap';
} else if(visualization === 'multiples') {
return 'small-multiples-set';
} else if(visualization === 'scatter') {
return 'vistk-scatterplot';
} else if(visualization === 'similarity') {
return 'vistk-network';
} else if(visualization === 'piescatter') {
return 'vistk-piescatter';
} else if (visualization === 'geo') {
return 'geo-map';
}
}),
recircUrl: computed('model.entity_type', 'model.entity.code', function() {
let entityType = this.get('model.entity_type');
if(entityType === 'location') { return `assets/img/hero_images/${this.get('model.entity_type')}/${this.get('model.entity.code')}.jpg`; }
if(entityType === 'product') { return 'assets/img/hero_images/product/product_1.jpg'; }
if(entityType === 'industry') { return 'assets/img/hero_images/industry/industry_1.jpg'; }
}),
searchFilter: function(data) {
let search = _.deburr(this.get('search'));
var regexp = new RegExp(search.replace(/(\S+)/g, function(s) { return "\\b(" + s + ")(.*)"; })
.replace(/\s+/g, ''), "gi");
return _.filter(data, (d) => {
let longName = get(d,`name_${this.get('i18n').display}`);
let shortName = get(d,`name_short_${this.get('i18n').display}`);
let code = get(d, 'code');
return _.deburr(`${shortName} ${longName} ${code}`).match(regexp);
});
},
filterToSelectedYears: function(data, start, end) {
let timeRange = d3.range(parseInt(start), parseInt(end) + 1); // Makes the range inclusive
return _.filter(data, function(d) {
return _.contains(timeRange, get(d, 'year'));
});
},
scrollTopWhenUpdate: observer('variable', function() {
window.scrollTo(0,0);
}),
actions: {
resetSearch: function() {
this.set('search', null);
this.set('searchText', null);
},
search: function() {
this.set('search', this.get('searchText'));
},
toggleVisualization: function(visualization) {
let model = this.get('model');
this.set('drawerChangeGraphIsOpen', false); // Close graph change drawer
this.set('searchText', this.get('search'));
var startDate = this.get('lastYear');
var endDate = this.get('lastYear');
if(visualization === 'multiples') {
startDate = this.get('firstYear');
endDate = this.get('lastYear');
}
if(this.get('visualization') === visualization) { return; } //do nothing if currently on the same visualization
this.transitionToRoute(`${model.entity_type}.visualization`, model.entity.id, model.source, visualization, this.get('variable'), {
queryParams: {startDate: startDate, endDate: endDate, search: this.get('search') }
});
},
changeQuestion: function() {
this.set('drawerQuestionsIsOpen', false); // Close question change drawer
},
toggleDrawerSettings: function() {
this.set('drawerChangeGraphIsOpen', false); // Turn off other drawers
this.set('drawerQuestionsIsOpen', false); // Turn off other drawers
this.toggleProperty('drawerSettingsIsOpen'); // toggle on 'Settings'
},
toggleDrawerChangeGraph: function() {
this.set('drawerSettingsIsOpen', false); // Turn off other drawers
this.set('drawerQuestionsIsOpen', false); // Turn off other drawers
this.toggleProperty('drawerChangeGraphIsOpen'); // toggle on 'Change Graph'
},
zoomOut: function() {
if(this.get('zoom') === 1) { this.decrementProperty('zoom'); }
},
zoomIn: function() {
if(this.get('zoom') === 0) { this.incrementProperty('zoom'); }
}
}
});