-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsurvey-results.js
186 lines (155 loc) · 6.33 KB
/
survey-results.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
ResidentResearch = window.ResidentResearch || {}
ResidentResearch.surveyResults = function() {
var spreadsheet_url= null,
responses= null, tracts= null,
city= {name: null, geoid: null},
maps= {
recommendation: {data: null, map: null},
survey: {data: null, map: null}
};
var addRecommendationControls = function() {
var buttons = new DemographicsControl(maps.recommendation.data, tracts);
maps.recommendation.map.addControl(buttons);
};
var loaded_tract_data = function(t) {
tracts = t;
addRecommendationControls();
function onerror(reason, ttop) {
alert('Failed to load spreadsheet: ' + reason);
}
var message = ('Loaded data for ' + tracts.length + ' tracts in ' + city.name + '. Loading spreadsheet…');
update_status(message);
jQuery( "body" ).trigger( "surveyMessage", [message] );
remove_overlay('recommendation-map', 'loading');
load_spreadsheet(get_query_variable('gdoc'), 'Survey Results All', loaded_spreadsheet, onerror);
};
var update_response_access_info = function(response, access) {
if(response.fields['Web on computer at home?']) {
access['computer-at-home'] += 1;
}
if(response.fields['Web on cell phone?']) {
access['cell-phone'] += 1;
}
if(response.fields['Web on computer at work?']) {
access['computer-at-work'] += 1;
}
if(response.fields['Web on public computer?']) {
access['public-computer'] += 1;
}
if(response.fields["Web on friend's computer?"]) {
access['friend-computer'] += 1;
}
return access;
};
var update_response_language_info = function(response, languages) {
var language = response.fields['Language spoken at home'];
if(language && language.toLocaleLowerCase() in languages) {
languages[language.toLocaleLowerCase()] += 1;
}
return languages;
};
var update_languages_text = function(languages) {
for(var id in languages) {
update_text(id, languages[id]);
}
}
var update_access_text = function(access) {
for(var id in access) {
update_text(id, access[id]);
}
}
var loaded_spreadsheet = function(all_responses, spreadsheet_url) {
responses = all_responses;
spreadsheet_url = spreadsheet_url;
var geo_responses = [],
languages = {
"english": 0,
"spanish": 0,
"chinese": 0,
"vietnamese": 0
},
access = {
"computer-at-home": 0,
"cell-phone": 0,
"computer-at-work": 0,
"public-computer": 0,
"friend-computer": 0
};
for(var i = 0; i < all_responses.length; i++) {
var response = all_responses[i];
jQuery( "body" ).trigger( "surveyMessage", ['Reading reponse '+(i+1)+' of '+all_responses.length+'...'] );
languages = update_response_language_info(response, languages);
access = update_response_access_info(response, access);
if(response.feature) {
geo_responses.push(response);
}
}
update_languages_text(languages);
update_access_text(access);
var message = ('Found ' + tracts.length + ' tracts in ' + city.name + ' and <a target="_blank" href="' + spreadsheet_url + '">a spreadsheet with ' + geo_responses.length + ' geographic responses</a>.');
update_status(message);
jQuery( "body" ).trigger( "surveyMessage", [message] );
_.defer(correlate_geographies,geo_responses, tracts, correlated_spreadsheet);
};
var correlated_spreadsheet = function(t) {
tracts = t;
var regressions = [
['Hispanic population', calculate_regression(tracts, 'B03002012', false)],
['Black population', calculate_regression(tracts, 'B03002004', false)],
['White population', calculate_regression(tracts, 'B03002003', false)],
['Owner-occupied housing', calculate_regression(tracts, 'B25003002', false )],
['Median household income', calculate_regression(tracts, 'B19013001', false)]
];
// Sort from most to least significant
regressions.sort(function(a, b) { return Math.abs(b[1]) - Math.abs(a[1]) });
var text_function = function(d) {
return d[0] + ': ' + d[1].toFixed(3);
}
update_overlay('survey-map', 'loading', 'Calculating reponse '+(i+1)+' of '+regressions.length+'...');
create_list(regressions, 'regressions',text_function);
update_status('Found ' + tracts.length + ' tracts in ' + city.name + ' and <a target="_blank" href="'+spreadsheet_url+'">a spreadsheet with ' + responses.length + ' responses</a>.');
update_overlay('survey-map', 'loading', 'Update the map');
render_survey_map();
};
var render_survey_map = function() {
var geojson = create_geojson_features(tracts, 'GeometryCollection');
var style_function = get_style_function(tracts, RESPONSES, GREENS);
maps.survey.setData(geojson);
maps.survey.setStyle(style_function);
maps.survey.reloadStyle();
maps.recommendation.setData(geojson);
maps.recommendation.reloadStyle();
remove_overlay('survey-map', 'loading');
};
var loaded_tracts = function(cityGeoid, cityName, tracts) {
city.name = cityName;
city.geoid = cityGeoid;
var geojson = create_geojson_features(tracts, 'GeometryCollection');
maps.survey = ResidentResearch.map();
maps.survey.init('survey-map', geojson);
maps.recommendation = ResidentResearch.map();
maps.recommendation.init('recommendation-map', geojson);
create_overlay('survey-map', 'loading', '<h1>Loading city tracts...</h1>');
create_overlay('recommendation-map', 'loading', '<h1>Loading city tracts...</h1>');
jQuery('body').on("surveyMessage", surveyProgressEventHandler);
jQuery('body').on("recommendationMessage", recommendationProgressEventHandler);
message = 'Found ' + tracts.length + ' tracts. Loading data…';
update_status(message);
jQuery( "body" ).trigger( "surveyMessage", [message] );
jQuery( "body" ).trigger( "recommendationMessage", [message] );
load_tract_data(tracts, loaded_tract_data);
};
var create_geojson_features = function(tracts, type) {
var geojson = {features: [], type: type };
for(var i = 0; i < tracts.length; i++) {
geojson.features.push(tracts[i].feature);
}
return geojson;
};
return {
init: function() {
update_status('Loading city tracts…');
load_city_tracts(get_query_variable('cityname'), loaded_tracts);
}
};
}