-
Notifications
You must be signed in to change notification settings - Fork 0
/
choroplethmap.html
162 lines (134 loc) · 4.44 KB
/
choroplethmap.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Social Data - Project</title>
<script type="text/javascript" src="d3/d3.min.js"></script>
<script src="//d3js.org/queue.v1.min.js"></script>
<style>
path {
fill: none;
stroke:black;
stroke-width: 1px;
}
body {
font-family: Arial, sans-serif;
}
.city {
font: 10px sans-serif;
font-weight: bold;
}
.legend {
font-size: 12px;
}
div.tooltip {
position: absolute;
text-align: center;
width: 150px;
height: 25px;
padding: 2px;
font-size: 10px;
background: #FFFFE0;
border: 1px;
border-radius: 8px;
pointer-events: none;
}
</style>
</head>
<body>
<script type="text/javascript">
// D3 code will go here
//Width and height
var width = 760;
var height = 600;
// Setting color domains(intervals of values) for our map (6 colors)
var color_domain = [50, 150, 350, 750, 1500, 2000, 2500, 3000, 3500, 4000, 4500, 5500, 6000]
var ext_color_domain = [0, 50, 150, 350, 750, 1500, 2000, 2500, 3000, 3500, 4000, 4500, 5500, 6000]
var legend_labels = ["< 50", "50+", "150+", "350+", "750+", "1500+", "2000+", "2500+", "3000+", "3500+", "4000+", "4500+", "5500+", " > 6000"]
var color = d3.scale.threshold()
.domain(color_domain)
.range(["#adfcad", "#ffcb40", "#ffba00", "#ff7d73", "#ff4e40", "#ff1300", "#e51100", "#cc0f00", "#b20d00", "#990b00", "#660700", "#330300", "#190100", "#000000"]);
var divTooltip = d3.select("body").append("div")
.attr("class", "tooltip")
.style("opacity", 0);
//Create SVG element
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height)
.style("margin", "10px auto");
var rateByZip = d3.map();
var quantize = d3.scale.quantize()
.domain([0, .15])
.range(d3.range(9).map(function(i) { return "q" + i + "-9"; }));
var projection = d3.geo.mercator()
.center([-73.93, 40.70])
.scale(60000)
.translate([width/2, height/2]);
var path = d3.geo.path().projection(projection);
//Reading map file and data
queue()
.defer(d3.json, "Data/Project/nycZipCode.geojson")
.defer(d3.csv, "Data/Project/ChoroData.csv")
.await(ready);
//Start of Choropleth drawing
function ready(error, map, data) {
var rateById = {};
var nameById = {};
var poNameById = {};
data.forEach(function(d) {
rateById[d['ZIPCODE']] = +d.COUNT;
nameById[d['ZIPCODE']] = d['ZIPCODE'];
});
map.features.forEach(function(d) {
poNameById[d['properties']['postalCode']] = d['properties']['PO_NAME'];
});
//Drawing Choropleth
svg.append("g")
.attr("class", "region")
.selectAll("path")
.data(map.features)
.enter().append("path")
.attr("d", path)
.style("fill", function(d) {
return color(rateById[d.properties.postalCode]);
})
.style("opacity", 0.8)
//Adding mouseevents
.on("mouseover", function(d) {
d3.select(this).transition().duration(300).style("opacity", 1);
divTooltip.transition().duration(300)
.style("opacity", 1)
divTooltip.text(poNameById[d.properties.postalCode] + " : " + rateById[d.properties.postalCode])
.style("left", (d3.event.pageX) + "px")
.style("top", (d3.event.pageY -30) + "px");
})
.on("mouseout", function() {
d3.select(this)
.transition().duration(300)
.style("opacity", 0.8);
divTooltip.transition().duration(300)
.style("opacity", 0);
})
}; // <-- End of Choropleth drawing
//Adding legend for our Choropleth
var legend = svg.selectAll("g.legend")
.data(ext_color_domain)
.enter().append("g")
.attr("class", "legend");
var ls_w = 20, ls_h = 20;
var x_position = 40;
var y_position = 80;
legend.append("rect")
.attr("x", x_position)
.attr("y", function(d, i){ return ((i*ls_h) - 2*ls_h) + y_position;})
.attr("width", ls_w)
.attr("height", ls_h)
.style("fill", function(d, i) { return color(d); })
.style("opacity", 0.8);
legend.append("text")
.attr("x", x_position + 30)
.attr("y", function(d, i){ return ((i*ls_h) - ls_h - 4) + y_position;})
.text(function(d, i){ return legend_labels[i]; });
</script>
</body>
</html>