-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
198 lines (162 loc) · 6.31 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
<!DOCTYPE html>
<html>
<head>
<script src="https://d3js.org/d3.v5.min.js"></script>
<script src="https://unpkg.com/topojson-client@3"></script>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h3 align="center">Average Median Rent Prices of NYC Neighborhoods 2021</h3>
<div id="map-container"></div>
</body>
<script>
var width = 900,
height = 600;
var lowColor = '#fffbaf',
highColor = '#ab2aed';
var svg = d3.select("#map-container").append("svg")
.attr("width", width)
.attr("height", height);
var projection = d3.geoMercator()
.scale(50000)
.center([-73.94, 40.70]); // long, lat of NYC
var path = d3.geoPath()
.projection(projection);
d3.csv("rent.csv").then(function(data) {
console.log(data)
// create chloropleth scale
var dataArray = [];
for (var i = 0; i < data.length; i++) {
var dataRentArr = []
for (var month = 1; month < 12; month++) {
var dataRent = 0.0
if (month >= 10) {
dataRent = parseFloat(data[i][`2021-${month}`]);
} else {
dataRent = parseFloat(data[i][`2021-0${month}`])
}
if (Number.isNaN(dataRent)) {
continue
}
dataRentArr.push(dataRent)
}
// Calculate average rent
if (dataRentArr.length != 0) {
var rentSum = dataRentArr.reduce((curSum, curVal) => curSum + curVal)
var avgRent = Math.round(rentSum / dataRentArr.length)
dataArray.push({avgRent, name: data[i]['areaName']})
}
}
var minVal = d3.min(dataArray.map(x => x.avgRent))
var maxVal = d3.max(dataArray.map(x => x.avgRent))
var ramp = d3.scaleLinear().domain([minVal * 0.7,maxVal * 0.7]).range([lowColor,highColor])
d3.json("neighbs.json").then(function(json) {
// convert topojson to json
json = topojson.feature(json, json.objects.neighbs).features
// loop through each neighborhood in the csv file
for (var i = 0; i < dataArray.length; i++) {
// grab neighborhood name
var dataNeighborhood = dataArray[i].name;
// find the corresponding neighborhood inside the json
for (var j = 0; j < json.length; j++) {
var jsonNeighborhood = json[j].properties.name
if (dataNeighborhood == jsonNeighborhood) {
// copy data value into json
json[j].properties['rent'] = dataArray[i].avgRent;
// stop looking through the json
break;
}
}
}
// create a tooltip
var tooltip = d3.select("#map-container")
.append("div")
.style("opacity", 0)
.attr("class", "tooltip")
// Three function that change the tooltip when user hover / move / leave a cell
var mouseover = function(d) {
tooltip
.style("opacity", 1)
d3.select(this)
.attr("stroke-width", 1)
.style("opacity", 1)
}
var mousemove = function(d) {
tooltip
.html(`${d.properties.name}<br/>Avg 2021 Rent: $${d.properties.rent}.00`)
.style("left", (d3.mouse(this)[0]+10) + "px")
.style("top", (d3.mouse(this)[1]+25) + "px")
}
var mouseleave = function(d) {
tooltip
.style("opacity", 0)
d3.select(this)
.attr("stroke-width", 0.5)
.style("opacity", 0.8)
}
// draw map
svg
.selectAll("path")
.data(json)
.enter()
.append("path")
.attr("d", path)
.attr("fill", "#808080")
.attr("stroke", "#000000")
.attr("stroke-width", 0.5)
.style("fill", function(d) { return ramp(d.properties.rent) })
.style("opacity", 0.8)
.on("mouseover", mouseover)
.on("mousemove", mousemove)
.on("mouseleave", mouseleave);
// add a legend
var w = 140, h = 450;
var key = d3.select("#map-container")
.append("svg")
.attr("width", w)
.attr("height", h)
.attr("class", "legend");
var legend = key.append("defs")
.append("svg:linearGradient")
.attr("id", "gradient")
.attr("x1", "100%")
.attr("y1", "0%")
.attr("x2", "100%")
.attr("y2", "100%")
.attr("spreadMethod", "pad");
legend.append("stop")
.attr("offset", "0%")
.attr("stop-color", highColor)
.attr("stop-opacity", 1);
legend.append("stop")
.attr("offset", "100%")
.attr("stop-color", lowColor)
.attr("stop-opacity", 1);
key.append("rect")
.attr("width", w - 100)
.attr("height", h - 100)
.style("fill", "url(#gradient)")
.attr("transform", "translate(0,10)");
var y = d3.scaleLinear()
.range([h - 100, 0])
.domain([minVal, maxVal]);
var yAxis = d3.axisRight(y);
key.append("g")
.attr("class", "y axis")
.attr("transform", "translate(41,10)")
.call(yAxis)
key.append("rect")
.attr("width", 10)
.attr("height", 10)
.attr("stroke", "#000000")
.attr("stroke-width", 0.5)
.attr("transform", `translate(0,${h - 50})`)
.style("fill", "#808080")
key.append("text")
.text("no data")
.attr("transform", `translate(20,${h - 43})`)
.style("font-size", "12px")
});
});
</script>
</html>