-
Notifications
You must be signed in to change notification settings - Fork 0
/
moneymap.html
406 lines (352 loc) · 16.3 KB
/
moneymap.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
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
<!DOCTYPE html>
<html lang="en">
<head>
<title>Council Tax Stock of Properties in Wales</title>
<meta charset="UTF-8" />
<meta name="author" content="Joseph Scott Davies" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="./css/map.css" />
<style>
body {
background-color: rosybrown;
}
.chart rect{
fill: #EE4035;
}
.chart text {
fill: #EE4035;
font: 10px sans-serif;
text-anchor: end;
}
nav {
float: left;
clear: both;
width: 300px;
}
svg{
margin: 10px;
float: left;
z-index: 1000;
}
p {
margin-left: 1em;
}
</style>
</head>
<body>
<!-- explanatory stuff -->
<header>
<h1>Council Tax Stock of Properties in Wales</h1>
<h2>by Local Authority</h2>
</header>
<!-- visualisation container -->
<div id="vis">
<div id="histo">
<div class="selectors">
<!-- selectors go here -->
</div>
</div><!-- #histo -->
<div id="money-map">
</div><!-- #map -->
</div><!-- #vis -->
<!-- javascript -->
<script src="//cdnjs.cloudflare.com/ajax/libs/d3/3.5.16/d3.min.js"></script>
<script src="./js/d3.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/queue-async/1.0.7/queue.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/topojson/1.6.20/topojson.min.js"></script>
<!-- visualisation js -->
<script>
var width;
var height;
var svg;
var g;
var key;
var projection;
var path;
var selector1;
var selector2;
var active = d3.select(null);
var medianColors = d3.scaleOrdinal()
.domain([0,8])
.range(["#EE4035", "#F3A530" ,"#56B949", "#30499B"]);
var letter = d3.scaleOrdinal()
.domain([0,1,2,3,4,5,6,7,8])
.range(['A','B','C','D','E','F','G','H','I']);
var letterColor = d3.scaleLinear()
.domain([0,1,2,3,4])
.range(["#FA5A5A", "#FFCB4F","#00BE70", "#295EA4" ]);
var wales_data;
var charge_data;
var rent_data;
var wage_data;
var rentRatios = [];
var wageRatios = [];
function getMedianHouseholdColor(d){
// variable I want returned to produce fill color
var medColor;
//get the relevant numbers for this area from CSV data and arrange into an array
var councilObject = (wales_data.filter(function(obj){ return obj.ECODE == d['id']; }))
var anArray = buildArray(councilObject[0]);
// variables for calculating
var target = 0;
var median = 0;
// the work
// get the total for area
for (i=0; i < anArray.length; i++){
median += anArray[i]
};
// find the midway point
median = Math.round(median/2);
// add bands until we reach the midway point
for (i=0; i < anArray.length; i++){
if (target < median){
target += anArray[i]
}else{
medColor = letterColor(i-1)
break;
}
}
// return newly arrived at color
return medColor;
};
function getMedianHouseholdBand(d){
// variable I want returned to produce fill color
var band;
//get the relevant numbers for this area from CSV data and arrange into an array
var councilObject = (wales_data.filter(function(obj){ return obj.ECODE == d['id']; }))
var anArray = buildArray(councilObject[0]);
// variables for calculating
var target = 0;
var median = 0;
// the work
// get the total for area
for (i=0; i < anArray.length; i++){
median += anArray[i]
};
// find the midway point
median = Math.round(median/2);
// add bands until we reach the midway point
for (i=0; i < anArray.length; i++){
if (target < median){
target += anArray[i]
}else{
band = letter(i-1)
break;
}
}
// return newly arrived at color
return band;
};
function draw(lad_bounds) {
projection
.scale(1)
.translate([0,0]);
var b = path.bounds(topojson.feature(lad_bounds, lad_bounds.objects['lad']));
var s = .95/Math.max((b[1][0] - b[0][0])/width, (b[1][1] - b[0][1])/height);
var t = [(width - s * (b[1][0] + b[0][0]))/2, (height - s*(b[1][1] + b[0][1]))/2];
projection
.scale(s)
.translate(t);
var areasMoney = gMoney.selectAll(".area")
.data(topojson.feature(lad_bounds, lad_bounds.objects['lad']).features);
areasMoney
.enter()
.append('path')
.attr('class', 'moneyarea')
.attr('data-key', function(d) { return d['id']; })
.attr('data-color', '#f7f7f7')
.attr('data-band', function(d) { return getMedianHouseholdBand(d); })
.attr('data-charge', function(d){
var band = getMedianHouseholdBand(d)
var id = d['id']
var arrayObject = charge_data.filter(function(obj){
return obj.ECODE == id
})
return arrayObject[0][band+" "];
})
.attr('data-rent', function(d){
var band = getMedianHouseholdBand(d)
var id = d['id']
var arrayObject = rent_data.filter(function(obj){
return obj.ECODE == id
})
var rentRatio = this.dataset.charge / arrayObject[0]["1 Bedroom"];
rentRatios.push(rentRatio);
rentRatios.sort();
return arrayObject[0]["1 Bedroom"];
})
.attr('data-wage', function(d){
var band = getMedianHouseholdBand(d)
var id = d['id']
var arrayObject = wage_data.filter(function(obj){
return obj.ECODE == id
})
var wageRatio = this.dataset.charge / arrayObject[0]['2016 (13)'];
wageRatios.push(wageRatio);
wageRatios.sort();
return arrayObject[0]['2016 (13)'];
})
.attr('d', path)
.style('stroke', '#f7f7f7');
colorMoneyMap('Rent');
}
function clicking(d){
if (active.node() === this){
null;
}else{
var id = this.id
console.log(this.id);
var walesObject = (wales_data.filter(function(obj){
return obj.ECODE == "W92000004";
}))
var arrayObject = (wales_data.filter(function(obj){
return obj.ECODE == id;
}))
var list1 = buildArray(walesObject[0]);
var list2 = buildArray(arrayObject[0]);
active.style("fill", medianColors(2));
active = d3.select(this);
active.style("fill", medianColors(0));
drawChart(list2);
}
}
function colorMoneyMap(input){
console.log(rentRatios);
d3.selectAll(".moneyarea")
.style("fill", function(d){
document.getElementById("selector2").style.visibility = "visible"
var id = this.dataset.key;
let secondValue = document.getElementById("selector2").value;
if (input == "Rent"){
var arrayObject = rent_data.filter(function(obj){
return obj.ECODE == id;
});
var rentRatio = this.dataset.charge / arrayObject[0][secondValue];
var ratioScale = d3.scaleLinear()
.domain([d3.min(wageRatios), d3.max(wageRatios)])
.range(["#FFF", "#EE4035"]);
return ratioScale(rentRatio);
} else {
document.getElementById("selector2").style.visibility = "hidden"
var ratio = this.dataset.charge / this.dataset.wage;
var ratioScale = d3.scaleLinear()
.domain([d3.min(wageRatios), d3.max(wageRatios)])
.range(["#FFF", "#56B949"]);
return ratioScale(ratio);
}
});
}
function buildArray(object){
var list = [parseInt(object.BAND_A.replace(/,/g,""), 10), parseInt(object.BAND_B.replace(/,/g,""), 10), parseInt(object.BAND_C.replace(/,/g,""), 10), parseInt(object.BAND_D.replace(/,/g,""), 10), parseInt(object.BAND_E.replace(/,/g,""), 10), parseInt(object.BAND_F.replace(/,/g,""), 10), parseInt(object.BAND_G.replace(/,/g,""), 10), parseInt(object.BAND_H.replace(/,/g,""), 10),parseInt(object.BAND_I.replace(/,/g,""),10)];
for (i=0; i < 8; i++){
if(isNaN(list[i])){
list[i] = 0
}
}
if (isNaN(list[8])) {
list.pop()
}
return list
}
function init() {
width = 666
height = 807
moneyMap = d3.select("#money-map")
.append("svg")
.attr("width", width)
.attr("height", height);
gMoney = moneyMap.append("g");
projection = d3.geo.albers()
.rotate([0,0]);
path = d3.geo.path()
.projection(projection);
selector1 = d3.select(".selectors")
.append("select")
.attr("class", "selector")
.attr("id","selector1")
.on("change", function(){
colorMoneyMap(this.value);
});
var options = selector1.selectAll("option")
.data(["Rent", "Wages"])
.enter().append("option")
.html(function(d){
return d;
});
selector2 = d3.select(".selectors")
.append("select")
.attr("class", "selector")
.attr("id","selector2")
.on("change", function(){
colorMoneyMap(document.getElementById('selector1').value);
});
var options = selector2.selectAll("option")
.data(["1 Bedroom", "2 Bedrooms", "3 Bedrooms", "4 Bedrooms"])
.enter().append("option")
.html(function(d){
return d;
});
queue()
.defer(d3.json, "./geoJSON/topo_lad.json")
.defer(d3.csv, "./data/ctsop2016.csv")
.defer(d3.csv, "./data/ctcharges.csv")
.defer(d3.csv, "./data/medianearnings.csv")
.defer(d3.csv, "./data/medianrents.csv")
.await(function(error, boundaries, ctsop2016, ctcharges, wages, rents){
wales_data = ctsop2016.filter(function(obj){
if (/^W/.test(obj.ECODE)){
return obj
}
});
charge_data = ctcharges.filter(function(obj){
if (/^W/.test(obj.ECODE)){
return obj
}
});
wage_data = wages;
rent_data = rents;
draw(boundaries);
})
}
function drawChart(list){
var chartWidth = 300,
chartHeight = 300,
barHeight = chartHeight/9;
var x = d3.scaleLinear()
.domain([0,d3.max(list)])
.range([0, ((chartHeight/100)*90)]);
var letters = d3.scaleOrdinal()
.domain([0,1,2,3,4,5,6,7,8])
.range(["A","B","C","D","E","F","G","H", "I"]);
var chart = d3.select(".chart")
.style("height", chartHeight)
.style("width", chartWidth);
var bar = chart.selectAll("rect")
.data(list);
var text = chart.selectAll("text")
.data(list);
bar.exit().transition().remove();
text.exit().transition().remove();
bar.transition()
.attr("transform", function(d,i){ return "translate(" + (i * barHeight) + "," + (chartHeight - x(d)) + ")"; })
.attr("height", function(d){ return x(d); })
.attr("width", barHeight-1);
text.transition()
.attr("y", function(d){ return (chartHeight - x(d) - 10); })
.attr("x", function(d,i){return i * barHeight + barHeight / 2 })
.attr("dy", ".35em")
.text(function(d,i){return letters(i);});
bar.enter().append("rect")
.attr("transform", function(d,i){ return "translate(" + (i * barHeight) + "," + (chartHeight - x(d)) + ")"; })
.attr("height", function(d){ return x(d); })
.attr("width", barHeight-1);
text.enter().append("text")
.attr("y", function(d){ return (chartHeight - x(d) - 10); })
.attr("x", function(d,i){return i * barHeight + barHeight / 2 })
.attr("dy", ".35em")
.text(function(d,i){return letters(i);});
}
init();
</script>
</body>
</html>