-
Notifications
You must be signed in to change notification settings - Fork 0
/
NCPopDensity.js
307 lines (271 loc) · 11.2 KB
/
NCPopDensity.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
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
/* select color1/ color2 to change the color scheme
select toggle to not display the country boundray */
var margin = { top: 10, right: 10, bottom: 10, left: 10 };
var width = 1200 - margin.left - margin.right;
var height = 900 - margin.top - margin.bottom;
var svg = d3.select("body")
.append("svg")
.attr("width", width)
.attr("height", height);
//geomercator porjection, scaled and translated to fit on the screen
var projection = d3.geoMercator()
.center([0, 0])
.scale(4000)
.translate([-1150, 3300])
let proj = d3.geoPath().projection(projection)
//dictionary that connects .csv to .json
var popById = d3.map();
var nameById = d3.map();
var main = () => {
d3.json("NC.json").then(data => {
//console.log(data)
//Define Tooltip here, initially hidden
var tooltip = d3.select("body")
.append("div")
.attr("id", "tooltip")
.style("opacity", 0)
var x = d3.scaleSqrt()
.domain([20, 1100])
.rangeRound([440, 950]);
d3.csv('NC.csv').then(data2 => {
//console.log(data2)
data2.forEach(d => {
d['Population Density'] = +d['Population Density'];
popById.set(d.id, +d['Population Density']);
nameById.set(d.id, d.County);
})
//console.log(data2);
//console.log(popById);
//console.log(nameById);
//color scale, by threshold
var color = d3.scaleThreshold()
.domain([0,20, 50, 90, 200, 500, 1800])
.range(d3.schemeYlGn[9]);
//the scale
//inspiration: https://bl.ocks.org/mbostock/5562380
var g = svg.append("g")
.attr("class", "key")
.attr("transform", "translate(-85,40)");
//turns the range back to the domain, into an array, to use for the scale
var range = color.range().map(d=>{return color.invertExtent(d)})
range.splice(0,1) //get rid of undefined
range.splice(range.length - 2, 2)
//console.log(range)
// the scale, made of rectangles
g.selectAll("rect")
.data(range)
.enter().append("rect")
.attr("height", 8)
.attr("x", function (d) { return x(d[0]); })
.attr("width", function (d) { return x(d[1]) - x(d[0]); })
.attr("fill", function (d) { return color(d[0]); });
//text over the scale
//https://bl.ocks.org/mbostock/5562380
g.append("text")
.attr("class", "scale")
.attr("x", 360)
.attr("y", -7)
.attr("fill", "black")
.attr("text-anchor", "start")
.attr("font-weight", "bold")
.attr("font-size", "14px")
.text("Population (residents) per Square Mile of Land Area");
//the ticks on the scale
g.style("font", "14px times")
.call(d3.axisBottom(x)
.tickSize(13)
.tickValues(color.domain()))
.select(".domain")
.remove(); //get rid of horizontal line
//draw the topojson
//console.log(topojson.feature(data, data.objects.cb_2015_north_carolina_county_20m).features)
svg.append("g")
.selectAll("path")
.data(topojson.feature(data, data.objects.cb_2015_north_carolina_county_20m).features)
.enter()
.each(d => {
//console.log(d.properties.GEOID)
// Need to fill with relevant info, reading from the csv
d.county = nameById.get(d.properties.GEOID);
d.popdens = popById.get(d.properties.GEOID);
//console.log(d)
})
.append("path")
.attr("stroke", "grey")
.attr("stroke-width", 1)
.attr("fill", d => color(d.popdens)) //color by population density
.attr("d", proj) //the projection path
.on("mouseover", d => { //append tooltip and fill with relevant info
tooltip
.transition()
.duration(1)
.style("opacity", 1)
//Add tooltip html assigning left, middle, right ids for css
tooltip.html(`${d.county}<br>
<span id="left">Population Density</span>
<span id="middle">:</span>
<span id="right">${d.popdens} residents/mi^2</span>
`)
.style("left", `${d3.event.pageX}px`)
.style("top", `${d3.event.pageY}px`)
})
.on("mouseout", () => { //hide tooltip on mouseout
tooltip.transition().duration(1).style("opacity", 0)
})
})
});
}
var main1 = () => {
d3.json("NC.json").then(data => {
//console.log(data)
//Define Tooltip here, initially hidden
var tooltip = d3.select("body")
.append("div")
.attr("id", "tooltip")
.style("opacity", 0)
var x = d3.scaleSqrt()
.domain([20, 1100])
.rangeRound([440, 950]);
d3.csv('NC.csv').then(data2 => {
//console.log(data2)
data2.forEach(d => {
d['Population Density'] = +d['Population Density'];
popById.set(d.id, +d['Population Density']);
nameById.set(d.id, d.County);
})
//console.log(data2);
//console.log(popById);
//console.log(nameById);
//color scale, by threshold
var color = d3.scaleThreshold()
.domain([0,20, 50, 90, 200, 500, 1800])
.range(d3.schemePastel1);
//the scale
//inspiration: https://bl.ocks.org/mbostock/5562380
var g = svg.append("g")
.attr("class", "key")
.attr("transform", "translate(-85,40)");
//turns the range back to the domain, into an array, to use for the scale
var range = color.range().map(d=>{return color.invertExtent(d)})
range.splice(0,1) //get rid of undefined
range.splice(range.length - 2, 2)
//console.log(range)
// the scale, made of rectangles
g.selectAll("rect")
.data(range)
.enter().append("rect")
.attr("height", 8)
.attr("x", function (d) { return x(d[0]); })
.attr("width", function (d) { return x(d[1]) - x(d[0]); })
.attr("fill", function (d) { return color(d[0]); });
//text over the scale
//https://bl.ocks.org/mbostock/5562380
g.append("text")
.attr("class", "scale")
.attr("x", 360)
.attr("y", -7)
.attr("fill", "black")
.attr("text-anchor", "start")
.attr("font-weight", "bold")
.attr("font-size", "14px")
.text("Population (residents) per Square Mile of Land Area");
//the ticks on the scale
g.style("font", "14px times")
.call(d3.axisBottom(x)
.tickSize(13)
.tickValues(color.domain()))
.select(".domain")
.remove(); //get rid of horizontal line
//draw the topojson
//console.log(topojson.feature(data, data.objects.cb_2015_north_carolina_county_20m).features)
svg.append("g")
.selectAll("path")
.data(topojson.feature(data, data.objects.cb_2015_north_carolina_county_20m).features)
.enter()
.each(d => {
//console.log(d.properties.GEOID)
// Need to fill with relevant info, reading from the csv
d.county = nameById.get(d.properties.GEOID);
d.popdens = popById.get(d.properties.GEOID);
//console.log(d)
})
.append("path")
.attr("stroke", "grey")
.attr("stroke-width", 1)
.attr("fill", d => color(d.popdens)) //color by population density
.attr("d", proj) //the projection path
.on("mouseover", d => { //append tooltip and fill with relevant info
tooltip
.transition()
.duration(1)
.style("opacity", 1)
//Add tooltip html assigning left, middle, right ids for css
tooltip.html(`${d.county}<br>
<span id="left">Population Density</span>
<span id="middle">:</span>
<span id="right">${d.popdens} residents/mi^2</span>
`)
.style("left", `${d3.event.pageX}px`)
.style("top", `${d3.event.pageY}px`)
})
.on("mouseout", () => { //hide tooltip on mouseout
tooltip.transition().duration(1).style("opacity", 0)
})
})
});
}
var main2 = () => {
d3.json("NC.json").then(data => {
//console.log(data)
d3.csv('NC.csv').then(data2 => {
//console.log(data2)
data2.forEach(d => {
d['Population Density'] = +d['Population Density'];
popById.set(d.id, +d['Population Density']);
nameById.set(d.id, d.County);
})
//draw the topojson
//console.log(topojson.feature(data, data.objects.cb_2015_north_carolina_county_20m).features)
svg.append("g")
.selectAll("path")
.data(topojson.feature(data, data.objects.cb_2015_north_carolina_county_20m).features)
.enter()
.each(d => {
//console.log(d.properties.GEOID)
// Need to fill with relevant info, reading from the csv
d.county = nameById.get(d.properties.GEOID);
d.popdens = popById.get(d.properties.GEOID);
//console.log(d)
})
.append("path")
.attr("stroke", "black")
.attr("d", proj) //the projection path
})
});
}
//Toggle color scale 1 and 2
var toggle = () => {
var x = document.getElementById("select").value;
var t = document.getElementById("title");
switch(x){
case 'color1':
d3.selectAll("svg > *").remove();
t.style.color='#1b8b4f';
t.innerHTML = 'North Carolina Population Density'
main();
break;
case 'color2':
d3.selectAll("svg > *").remove();
t.style.color='#7570b3';
t.innerHTML = 'North Carolina Population Density'
main1();
break;
case 'toggle':
d3.selectAll("svg > *").remove();
t.style.color='black';
t.innerHTML = 'North Carolina Population Density'
main2();
break;
}
}
main();