-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAirBnb(Calgary_V3).html
256 lines (205 loc) · 8.22 KB
/
AirBnb(Calgary_V3).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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Example 1 - Saskatoon Map</title>
<script type="text/javascript" src="d3.v5.min.js"></script>
</head>
<body>
</body>
<style>
path {
stroke: lightgrey;
}
</style>
<script type="text/javascript"> //V3 = Implemented zoom-in/out functionality
//Width and height
var w = 800;
var h = 500;
//Define map projection
var projection = d3.geoMercator()
.center([-114.07, 51.0486])
.translate([w / 2, h / 2])
.scale([50000]);
//Define path generator
var path = d3.geoPath()
.projection(projection);
//Create SVG element
var svg = d3.select("body")
.append("svg")
.attr("width", w)
.attr("height", h)
.style('border', '2px solid black') //to see where the border is zooming
.call(d3.zoom().on("zoom", function () { //Function call
svg.attr("transform", d3.event.transform)
}))
.append("g");
//Load in GeoJSON data
d3.json("calgary.json").then(function(json) {
// Bind data and create one path per GeoJSON feature
svg.selectAll("path")
.data(json.features)
.enter()
.append("path")
.attr("d", path)
.attr('fill', 'white')
.append('title')
.text(function(d) {
return d.properties.name;
})
var color;
var maxvalue;
var minvalue;
//dictionary variables
var dictionary = {};
var rating_dictionary = {};
var reviews_dictionary = {};
var location1= [];
var KeysA;
var dataset;
/*END: READ ONLY*/
//Load in the data
d3.csv("tomslee.csv").then(function (data){
dataset = data;
contributionDictionary();
console.log(dataset)
maxvalue=Math.max.apply(null, Object.values(dictionary));
minvalue=Math.min.apply(null, Object.values(dictionary));
color = d3.scaleQuantize()
//.range(["light blue","blue","pink","red"]);
.range(["grey","pink","red"]);
color.domain([minvalue,maxvalue])
// d3.csv("Country_List.csv").then(function (data1){
// dataset = data1;
generateVisualization();
});
function contributionDictionary(){
dataset.forEach(function(d){
//console.log(d)
d.price=+d.price;
d.overall_satisfaction=+d.overall_satisfaction;
d.reviews=+d.reviews;
d.bedrooms=+d.bedrooms;
d.latitude=+d.latitude;
d.longitude=+d.longitude;
if(d.borough in dictionary){
if(d.bedrooms==0||d.bedrooms==1){
dictionary[String(d.borough)].push(d.price);
}
else{
dictionary[String(d.borough)].push(d.price/d.bedrooms);
}
//creating rating dictionary
if(isNaN(parseFloat(d.overall_satisfaction))) return;
rating_dictionary[String(d.borough)].push(d.overall_satisfaction)
//creating reviews dictionary
reviews_dictionary[String(d.borough)] += d.reviews;
}
else{
//location1[String(d.borough)]=[d.latitude,d.longitude];
location1.push({latitude:d.latitude,longitude:d.longitude,borough:d.borough})
dictionary[String(d.borough)]=[];
rating_dictionary[String(d.borough)]=[]; //rating dictionary
reviews_dictionary[String(d.borough)] = 0;
if(d.bedrooms==0||d.bedrooms==1){
dictionary[String(d.borough)].push(d.price);
}
else{
dictionary[String(d.borough)].push(d.price/d.bedrooms);
}
rating_dictionary[String(d.borough)].push(d.overall_satisfaction);
reviews_dictionary[String(d.borough)] += d.reviews;
}
});
var i;
var keys1=Object.keys(dictionary); //keys1 is array of keys
for (i in keys1){ //i is index eg. keys1[0] = centre
dictionary[keys1[i]]=d3.mean(dictionary[keys1[i]]);
//console.log(dictionary[keys1[i]])
}
//console.log(rating_dictionary["CENTRE"])
var i;
var keys2=Object.keys(rating_dictionary);
for (i in keys2){
rating_dictionary[keys2[i]]=d3.mean(rating_dictionary[keys2[i]]);
}
//console.log(rating_dictionary)
};
function generateVisualization(){
//Create SVG element
//var svg = d3.select("body").append("svg").attr("width", w).attr("height", h);
//Draw circles
svg.selectAll("circle").data(location1).enter().append("circle") //change .data(dataset) to plot each listings
.attr("cx",function(d,i){
//return d.Longitude;
// console.log(projection([d.longitude, d.latitude]))
return projection([d.longitude, d.latitude])[0];
//return long(d.longitude);
}
)
.attr("cy",function(d,i){
return projection([d.longitude, d.latitude])[1];
//return lat(d.latitude);
}
)
.attr("r",10)
.attr("fill",function(d,i){
//console.log(d.borough);
//if(d.borough in KeysA){
//keysA.remove(d.borough)
return color(dictionary[d.borough]);
})
.on("mouseover",handleMouseOver)
.on("mouseout",handleMouseOut)
/////////////TEST////
//Mouse over event
function handleMouseOver(d, i) { // Add interactivity
// Specify where to put label of text
//mouseover-text - Average Price
svg.append("text").attr('id', "mouseover-text")
.attr('x',d3.select(this).attr('cx')-30)
.attr('y',d3.select(this).attr('cy')-30)
<!-- .attr("stroke","black") -->
<!-- .attr("stroke",function(location1, i){ //change the text color according to borough color -->
<!-- //console.log(d.borough); -->
<!-- //if(d.borough in KeysA){ -->
<!-- //keysA.remove(d.borough) -->
<!-- return color(dictionary[d.borough]); -->
<!-- }) -->
.text(" Average Price:"+Math.round(dictionary[d.borough]))
//mouseover2-text - Ratings
svg.append("text").attr('id', "mouseover2-text")
.attr('x',d3.select(this).attr('cx')-30)
.attr('y',d3.select(this).attr('cy')-45)
.text(" Average Rating:"+ d3.format(".1f")(rating_dictionary[d.borough]))
//mouseover3-text - reviews
svg.append("text").attr('id', "mouseover3-text")
.attr('x',d3.select(this).attr('cx')-30)
.attr('y',d3.select(this).attr('cy')-60)
.text(" Number of reviews:"+reviews_dictionary[d.borough]);
}
function handleMouseOut(d, i) {
// Use D3 to select element, change color back to normal
// Select text by id and then remove
d3.select("#mouseover-text").remove() // Remove text location
d3.select("#mouseover2-text").remove()
d3.select("#mouseover3-text").remove();
}
console.log(location1)
svg.selectAll("text").data(location1).enter().append("text")
.attr("x",function(d,i){
return projection([d.longitude, d.latitude])[0];
})
.attr("y",function(d,i){
return projection([d.longitude, d.latitude])[1];
})
.attr("font-size","15px")
.text(function(d,i){
if(color(dictionary[d.borough])=="grey"||color(dictionary[d.borough])=="red"||color(dictionary[d.borough])=="pink"){
return d.borough
}
})
};
});
</script>
</html>