-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
165 lines (131 loc) · 6.55 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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>US Chronic Disease Indicators</title>
<link href='https://api.tiles.mapbox.com/mapbox.js/v2.1.4/mapbox.css' rel='stylesheet' />
<script src='https://api.tiles.mapbox.com/mapbox.js/v2.1.4/mapbox.js'></script>
<script src='https://www.mapbox.com/mapbox.js/assets/data/us-states.js'></script>
<script src='http://d3js.org/colorbrewer.v1.min.js'></script>
</head>
<body>
<h1>US Chronic Disease Indicators</h1>
<select id="year-selector" title="Select a year or year range">
<option value="2013" selected="selected">2013</option>
</select>
<select id="indicator-selector" title="Select an indicator">
<option value="MTH1_0" selected="selected" >Recent mentally unhealthy days among adults aged ≥18 years</option>
</select>
<select id="gender-selector" title="Select a gender">
<option value="GENT" selected="selected">All Genders</option>
<option value="GENM">Male</option>
<option value="GENF">Female</option>
</select>
<!--button id="map-requestor" title="Refresh map based on selections">Update</button-->
<div id="map-container" style="height:40em; border:1px solid #000; margin:2em;"></div>
<div id="map-footer">
<p>
<a href="http://s2t2.github.io/us-chronic-disease-indicators">view</a> |
<a href="https://github.com/s2t2/us-chronic-disease-indicators">source</a> |
<a href="http://catalog.data.gov/dataset/u-s-chronic-disease-indicators-cdi">data</a>
</p>
</div>
<script src="//d3js.org/d3.v3.min.js" charset="utf-8"></script>
<script type="text/javascript">
// MAP
L.mapbox.accessToken = 'pk.eyJ1IjoiczJ0MiIsImEiOiJQVFE0UW1JIn0.0MfEvAJVxFx7BLGoL0USXA'
var mapbox_map_id = 'mapbox.streets'
var map_center = [40.111688665595956, -97.3388671875]
var map_zoom = 4
var map;
function updateMap(){
if (typeof(map) != "undefined") {
map.remove()
}
map = L.mapbox.map('map-container', mapbox_map_id, {scrollWheelZoom:false})
.setView(map_center, map_zoom)
.on("click", logMapClick)
.on("dragend", logMapView)
.on("zoomend", logMapView)
};
function logMapClick(cc){
console.log("MAP CLICKED ON GEOLOCATION: [" + cc.latlng.lat + ", " + cc.latlng.lng + "]")
};
function logMapView(){
var current_center = map.getCenter()
console.log("MAP VIEW: [" + current_center.lat + ", " + current_center.lng + "], " + map.getZoom() )
};
// SELECTIONS AND REQUESTS
var yearSelector = document.getElementById('year-selector');
var indicatorSelector = document.getElementById('indicator-selector');
var genderSelector = document.getElementById('gender-selector');
yearSelector.addEventListener("change", drawMap, false);
indicatorSelector.addEventListener("change", drawMap, false);
genderSelector.addEventListener("change", drawMap, false);
var rows;
var colorScale = d3.scale.linear();
function drawMap(request){
var year = yearSelector.value
var strat_id = genderSelector.value
var strat_name = genderSelector[genderSelector.selectedIndex].textContent.trim()
var indicator_id = indicatorSelector.value
var indicator_desc = indicatorSelector[indicatorSelector.selectedIndex].textContent.trim()
// UPDATE COLOR PALETTE AND COLOR SCALE RANGE
switch(strat_id){
case "GENT":
var palette = colorbrewer.Purples[9]
break;
case "GENM":
var palette = colorbrewer.Blues[9]
break;
case "GENF":
var palette = colorbrewer.Reds[9]
break;
default:
var palette = colorbrewer.Greys[9]
}
colorScale.range([ palette[2], palette[5], palette[8] ])
updateMap()
d3.csv("data/query_results.csv", function(error, csv_data){
if (error) return console.warn(error);
rows = csv_data.filter(function(row){
return row["year"] == year && row["indicatorid"] == indicator_id && row["stratificationid1"] == strat_id
})
var values = rows.map(function(row){return row["datavalue"]})
var quantiles = [ d3.min(values), d3.mean(values), d3.max(values) ]
colorScale.domain(quantiles)
rows.forEach(function(row) {
var state_geojson_features = statesData.features.filter(function(feature){ return feature.properties.name == row["locationdesc"]; })
if(state_geojson_features.length > 0){
var state_geojson_feature = state_geojson_features[0]
var state_name = state_geojson_feature.properties.name
var state_value = row["datavalue"]
var state_high = row["highconfidenceinterval"]
var state_low = row["lowconfidenceinterval"]
var geojson_options = {
style: {
fillColor: colorScale(state_value),
color: "#fff", //colorScale(state_value),
weight: 1.5,
opacity: .85,
fillOpacity: .75,
className: 'geojson state ' + state_name
}
}
var state = L.geoJson(state_geojson_feature, geojson_options)
var popup_content = "<strong>State</strong>: " + state_name + "<br/>" +
"<strong>Year</strong>: " + year + "<br/>" +
"<strong>Indicator</strong>: " + indicator_desc + "<br/>" +
"<strong>Gender</strong>: " + strat_name + "<br/>" +
"<strong>Age-adjusted Mean</strong>: " + state_value + "<br/>" +
"<strong>Confidence Interval</strong>: " + state_low + " to " + state_high
state.bindPopup(popup_content)
map.addLayer(state)
} // end if
}); // end forEach
}); // end d3.csv
}; // end drawMap
drawMap()
</script>
</body>
</html>