-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.html
313 lines (231 loc) · 6.93 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<link rel="stylesheet" href="css/leaflet.css" />
<link rel="stylesheet" href="css/map.css" />
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css">
<!-- D3 -->
<script src="js/d3.v3.min.js"></script>
<!-- Leaflet -->
<script src="js/leaflet.js"></script>
<!-- Tabletop for Google Docs data pull -->
<script src="js/tabletop.js"></script>
<!-- Underscore for awesome collection comprehensions -->
<script src="js/underscore-min.js"></script>
<!-- ParseURI.js because it's dead-simple and small -->
<!-- See http://blog.stevenlevithan.com/archives/parseuri for details -->
<script src="js/parseuri.js"></script>
<!-- JQuery -->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<!-- Colorbrewer (from D3) -->
<script src="js/colorbrewer.js"></script>
<!-- Markdown lib to render markdown -->
<script src="js/markdown.min.js"></script>
<script src="//netdna.bootstrapcdn.com/bootstrap/3.0.3/js/bootstrap.min.js"></script>
<body>
<div id="map"></div>
<div class="container">
<div id="getting-started"></div>
</div>
</body>
<script>
//////////////////////////////// Begin DG ////////////////////////////////
window.onload = function() {
key = parseUri(location).queryKey.key
if(key) {
uri_parameters = parseUri(location).queryKey;
configureMapOptions(uri_parameters);
getDataFromGoogle(key, scaleAndMapData);
}
else {
renderGettingStarted();
}
};
function renderGettingStarted() {
$("#map").remove();
$.get('GETTING_STARTED.md', function(raw_markdown) {
$("#getting-started").append(markdown.toHTML(raw_markdown))
});
}
// Retrieves the spreadsheet with key (gdoc_key) and passes it to the callback (passed as: myCallback(data, tabletop) )
function getDataFromGoogle(gdoc_key, callback) {
gdoc_url = "https://docs.google.com/spreadsheet/pub?hl=en_US&hl=en_US&key=" + gdoc_key + "&output=html"
Tabletop.init( { key: gdoc_url,
callback: callback,
simpleSheet: true } )
}
// Main workhorse
function scaleAndMapData(data, tabletop) {
console.log(data);
scaledData = scaleData(data);
mapData(scaledData);
}
function scaleData(data) {
values_only_array = extractValuesFromObjectArray(data)
quantizer = new Quantizer(values_only_array, Config.scale)
_.each(data, function(data_object) { data_object.scaled_value = quantizer.quantizeNumber(data_object.value) } )
return data;
}
function mapData(data) {
//loadStyle("css/leaflet.css");
//loadStyle("css/map.css");
var map = L.map('map').setView([37.8, -96], 4);
// Do shit inside here for now
if(Config.geo_unit === 'county') {
geofile = 'data/us-counties.json';
}
else {
geofile = 'data/us-states.json';
}
$.getJSON(geofile, function(geo_data) {
geo_data = addDataToGeoJson(data, geo_data)
geojson = L.geoJson(geo_data, {
style: style,
onEachFeature: onEachFeature
}).addTo(map);
info.addTo(map);
});
}
// HELPFUL SHIT
// Config for map (from URI parameters)
var Config = {
// Attributes with defaults
color: 'green',
scale: 5,
geo_unit: 'state'
};
// Set configuration for map by processing the URI parameters
function configureMapOptions(options) {
pairs = _.pairs(options)
_.each(pairs, function(pair) {
k = pair[0]
v = pair[1]
// Hacky; may have more params that need to be coerced to int
if(k === 'scale') {
v = parseInt(v)
}
Config[k] = v
})
}
// Given an array of objects, returns an array of the values contained in the 'value' slots of each object
function extractValuesFromObjectArray(data_object_array) {
return _.map(data_object_array, function(data_object) { return data_object.value; } );
}
/*
//Test for extractValuesFromObjectArray()
obj_arr = [ { name: "dave", value: 2 }, { name: "berg", value: 5 } ]
console.log(obj_arr);
console.log(extractValuesFromObjectArray(obj_arr));
*/
// Quantizing data into buckets
// Given
function Quantizer(data_array, scale) {
min = d3.min(data_array)
max = d3.max(data_array)
//console.log("min: " + min)
//console.log("max: " + max)
this.quantizeNumber = d3.scale.quantize()
.domain([min,max])
.range(d3.range(1,scale + 1)) // Start with only mapping on 1-5 color scale
}
/*
//Test for Quantizer
data = [1,2,3,10];
var quantizer = new Quantizer(data)
console.log(quantizer.quantizeNumber(2));
console.log(quantizer.quantizeNumber(10));
*/
function showError(error_text) {
// Can change below to some other flash-like thing
alert(error_text)
}
// Adds `scaled_value` and `value` from data objects into geojson features as, eg, feature.properties.scaled_value
function addDataToGeoJson(data, geojson) {
var data_hash = {};
_.each(data, function(data_object) { data_hash[data_object['state']] = data_object } )
_.each(geojson.features, function(feature) {
geo_name = feature.properties.name;
data_object = data_hash[geo_name];
if(data_object) {
feature.properties.scaled_value = data_object.scaled_value;
feature.properties.value = data_object.value;
}
else {
feature.properties.scaled_value = -1;
feature.properties.value = -1;
}
});
return geojson;
}
function style(feature) {
color = getColor(feature.properties.scaled_value, Config.scale);
return {
fillColor: color,
weight: 2,
opacity: 1,
color: 'white',
//dashArray: '3',
fillOpacity: 1
};
}
// Make sure it deals with -1 (no data)
function getColor(num, scale) {
if(Config.color === 'blue') {
// Index in the array is 1 less than the scaled_number
return colorbrewer.Blues[scale][num - 1];
}
// Default to green
else {
return colorbrewer.Greens[scale][num - 1];
}
}
/*
function loadStyle(path) {
$("<link/>", {
rel: "stylesheet",
type: "text/css",
href: path,
}).appendTo("head");
}
*/
// Info box shit
// control that shows state info on hover
var info = L.control();
info.onAdd = function (map) {
this._div = L.DomUtil.create('div', 'info');
this.update();
return this._div;
};
info.update = function (props) {
this._div.innerHTML = (props ?
'<b>' + props.name + '</b><br />' + props.value
: 'Hover over a state');
};
function onEachFeature(feature, layer) {
layer.on({
mouseover: highlightFeature,
mouseout: resetHighlight//,
//click: zoomToFeature
});
}
function resetHighlight(e) {
geojson.resetStyle(e.target);
info.update();
}
function highlightFeature(e) {
var layer = e.target;
layer.setStyle({
weight: 5,
color: '#666',
dashArray: ''//,
//fillOpacity: 0.7
});
if (!L.Browser.ie && !L.Browser.opera) {
layer.bringToFront();
}
info.update(layer.feature.properties);
}
//////////////////////////////// End DG ////////////////////////////////
</script>
</html>