-
Notifications
You must be signed in to change notification settings - Fork 0
/
population-density.html
252 lines (194 loc) · 6.47 KB
/
population-density.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
<!DOCTYPE html>
<html>
<head>
<title>Population density in France</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
<style>
html, body, #map {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
.stations, .stations svg {
position: absolute;
}
.stations svg {
width: 60px;
height: 20px;
padding-right: 100px;
font: 10px sans-serif;
}
.stations circle {
fill: brown;
stroke: black;
stroke-width: 1.5px;
}
</style>
</head>
<body>
<input id=dateInput type='range' min=0> <span id=dateSpan></span>
<div id="map"></div>
<script src="https://d3js.org/d3.v3.min.js"></script>
<script src="https://d3js.org/topojson.v1.min.js"></script>
<script>
// may 2015
var width = 400;
var height = 350;
/*var map = new google.maps.Map(d3.select("#map").node(), {
zoom: 8,
center: new google.maps.LatLng(37.76487, -122.41948),
mapTypeId: google.maps.MapTypeId.TERRAIN
});*/
const d3Json = url => new Promise(res => d3.json(url, (req, json) => res(json)));
var path = d3.geo.path();
var projection = d3.geo.conicConformal() // Lambert-93
.center([2.454071, 47.279229])
.scale(2000)
.translate([width / 2, height / 2]);
path.projection(projection);
load().then(({geojson, density, dates}) => {
dateInput.max = dates.length-1;
dateInput.value = dates.length-1;
var values = Object.keys(density).map(i => density[i][5]);
const color = d3.scale.log()
.domain([d3.min(values), d3.mean(values), d3.max(values)])
.range(["blue", "orange", "red"]);
/*console.log(geojson.features.map(function(i){return i.properties.CODE_DEPT}).sort())
console.log(Object.keys(density).sort())
*/
const features = d3.select('#map').append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("id", "departements")
.selectAll("path")
.data(geojson.features);
features.enter()
.append("path")
.attr('class', 'departement')
.attr('fill', function(d) {
return color((density[d.properties.CODE_DEPT]||Array.from({length:dates.length},()=>0))[+dateInput.value])
})
.attr("d", path)
dateSpan.textContent = dates[+dateInput.value];
dateInput.onchange = function(e){
d3.selectAll(".departement")
.attr('fill', function(d) {
return color((density[d.properties.CODE_DEPT]||Array.from({length:dates.length},()=>0))[+dateInput.value])
});
dateSpan.textContent = dates[+dateInput.value]
}
});
function load() {
if (localStorage.caubPopulationDensity) {
var cache = JSON.parse(localStorage.caubPopulationDensity);
if (Date.now() < cache.time + 7*86.4e6) {
return Promise.resolve(cache.data);
}
}
//TODO use queue http://bl.ocks.org/mbostock/4060606
// other todo: http://zbryikt.github.io/visualize/dorling/
return Promise.all([
d3Json('https://rawgit.com/kerneis/france-choropleth/master/data/geofla/departement.json'),
fetch('https://cors-anywhere.herokuapp.com/https://fr.wikipedia.org/wiki/Liste_des_d%C3%A9partements_fran%C3%A7ais_class%C3%A9s_par_population_et_superficie').then(r => r.text())
])
.then(([geojson, text]) => {
const doc=(new DOMParser).parseFromString(text, "text/xml");
const rows = Array.from(doc.querySelectorAll('table.wikitable.sortable.alternance tr')).slice(1);
const header = rows[0];
const density = {};
const dates = Array.from(header.children, th => parseInt(th.textContent));
for (let i=1; i<rows.length; i++) {
const tr = rows[i];
const depId = tr.children[1].textContent;
const area = parseInt(tr.children[tr.childElementCount - 2].querySelector('span').textContent.replace(/\s+/g,''));
density[depId] = Array.from(tr.children).slice(3, -2).reduce((a, td) =>
a.concat(
Array.from({length:td.getAttribute('colspan')||1}, () => td.textContent ?
parseInt(td.querySelector('span').textContent.replace(/\s+/g,'')) / area :
0
)
), []
);
// note td.colSpan is undefined wit DOMParser
}
localStorage.caubPopulationDensity = JSON.stringify({time: Date.now(), data: {geojson, density, dates}});
return {geojson, density, dates};
});
}
/*
var overlay = new google.maps.OverlayView();
// Add the container when the overlay is added to the map.
overlay.onAdd = function() {
var layer = d3.select(this.getPanes().overlayLayer).append("div")
.attr("class", "stations");
overlay.draw = function() {
var features = deps
.selectAll("path")
.data(geojson.features);
var colorScale = d3.scale.category20c();
features.enter()
.append("path")
.attr('class', 'departement')
.attr('fill', function(d) {
return colorScale(+d.properties.CODE_DEPT);
})
.attr("d", path)
var projection = this.getProjection(),
padding = 10;
var marker = layer.selectAll("svg")
.data(d3.entries(data))
.each(transform) // update existing markers
.enter().append("svg:svg")
.each(transform)
.attr("class", "marker");
// Add a circle.
marker.append("svg:circle")
.attr("r", 4.5)
.attr("cx", padding)
.attr("cy", padding);
// Add a label.
marker.append("svg:text")
.attr("x", padding + 7)
.attr("y", padding)
.attr("dy", ".31em")
.text(function(d) { return d.key; });
function transform(d) {
d = new google.maps.LatLng(d.value[1], d.value[0]);
d = projection.fromLatLngToDivPixel(d);
return d3.select(this)
.style("left", (d.x - padding) + "px")
.style("top", (d.y - padding) + "px");
}
};
};
// Bind our overlay to the map
overlay.setMap(map);*/
/*fetch('https://rawgit.com/kerneis/france-choropleth/master/data/geofla/departement.json').then(r => r.json()).then(function(r){
console.log(r)
})
//https://rawgit.com/alignedleft/d3-book/master/chapter_12/us-states.json
*/
/*yql(`select * from html where url='http://fr.wikipedia.org/wiki/Liste_des_d%C3%A9partements_fran%C3%A7ais_class%C3%A9s_par_population_et_superficie'`).then(function(r){
var doc=(new DOMParser).parseFromString(r, "text/xml");
population2 = [].slice.apply(doc.querySelectorAll('table.wikitable.sortable.alternance tbody tr')).slice(1).map(function(e){
return {
id: +e.childNodes[9].innerHTML,
name: e.childNodes[1].childNodes[0].innerHTML,
pop1931: e.childNodes[2].childNodes[0].innerHTML,
pop1999: e.childNodes[3].childNodes[0].innerHTML,
pop2008: e.childNodes[4].childNodes[0].innerHTML,
pop2010: e.childNodes[5].childNodes[0].innerHTML,
pop2011: e.childNodes[6].childNodes[0].innerHTML,
area: e.childNodes[7].childNodes[0].innerHTML,
}
})
population = {};
populations2.forEach(function(o){ population[o.id]=o.pop2011 })
})
*/
</script>
</body>
</html>