Skip to content

Instantly share code, notes, and snippets.

@yudataguy
Created April 12, 2018 16:55
Show Gist options
  • Save yudataguy/502e4a6d5688955064b98039113be8c5 to your computer and use it in GitHub Desktop.
Save yudataguy/502e4a6d5688955064b98039113be8c5 to your computer and use it in GitHub Desktop.
Japan population growth
<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<script src="//d3js.org/d3.v3.min.js"></script>
<script src="//d3js.org/topojson.v1.min.js"></script>
<script src="https://unpkg.com/[email protected]/build/d3-simple-slider.js"></script>
<title>Japanese Population Trend After WW2</title>
<style>
.keys.selected {
font-weight: bold;
color: red;
font-size: 14;
}
#tooltip {
position: absolute;
top: 0;
left: 0;
background-color: #f0f0f0;
}
</style>
</head>
<body>
<div class="container"></div>
<script type="text/javascript">
var japan;
var keys; // variable for different key(prefecture)
var currentKey; // variable for current selection key (year)
var w = 960;
var h = 760;
var container = d3.select('.container');
var svg = container.append('svg')
.attr('width', w)
.attr('height', h)
.append("g");
var g = svg.append("g");
// Tooltip style
var tooltip = d3.select("body")
.append("div")
.attr("id", "tooltip")
.style("opacity", 0);
// Function for data from different year
var clickKey = function() {
currentKey = d3.select(this).attr('value');
d3.selectAll('.keys')
.classed("selected", function(){return (d3.select(this).attr('value') === currentKey)});
updateMap();
}
// Create slider for different year
var slider = function() {
currentKey = d3.select(this).attr('value');
d3.sliderHorizontal()
.min(d3.min(keys))
.max(d3.max(keys))
.width(800)
.tickFormat(d3.timeFormat('%Y'))
.tickValues(keys)
.on('onchange', function(){return (d3.select(this).attr('value') === currentKey)});
updateMap();
}
// Assign color to domain range
var color = d3.scale.linear()
.domain([-150, 300, 750, 1200, 1800])
.range(['#feebe2', '#fbb4b9', '#f768a1', '#c51b8a', '#7a0177']);
// Update map color
var updateMap = function() {
g.selectAll('path')
.transition()
.duration(1000)
.style('fill', function(d) {
return color(d.properties[currentKey]);
});
}
// Legend color
svg.selectAll('rect.legend')
.data(color.domain())
.enter()
.append('rect')
.attr('class', 'legend')
.attr('x', 65)
.attr('y', function(d, i) { return 100 + 20 * i; })
.attr('width', 20)
.attr('height', 20)
.attr('fill', function(d) { return color(d); });
// Legend text
svg.selectAll('text.legend')
.data(color.domain())
.enter()
.append('text')
.attr('x', 60)
.attr('y', function(d, i) { return 100 + 20 * i + 12; })
.attr('font-size', 9)
.attr('text-anchor', 'end')
.text(function(d) { return d; });
// Read map file
d3.json('japan.topojson', function(error, collection) {
var japan = topojson.feature(collection, collection.objects.japan).features;
// Setup map
var projection = d3.geo.mercator()
.scale(1500)
.center([137, 34.5])
.translate([w / 2, h / 2]);
var path = d3.geo.path().projection(projection);
g.selectAll('path')
.data(japan)
.enter()
.append('path')
.attr('d', path)
.attr('prefecture', function(d) {
return d.properties.name;
})
.style('fill', function(d, i) {
return 'white';
})
.attr("stroke", "#404040")
.attr("stroke-width", 0.5)
.style('cursor', 'pointer')
.on('mouseover', function(d){
tooltip.style("opacity", 1)
.html(d.properties['name']+'<br/>'+d.properties[currentKey])
.style("left", (d3.event.pageX + 10) + "px")
.style("top", (d3.event.pageY - 40) + "px");
var self = d3.select(this);
self.style('fill', 'red');
})
.on('mousemove', function(d){
tooltip
.style("left", (d3.event.pageX + 10) + "px")
.style("top", (d3.event.pageY - 40) + "px");
})
.on('mouseout', function(d, i){
tooltip.style("opacity", 0);
var self = d3.select(this);
self.transition()
.duration(300)
.style('fill', function(d, i) {
return color(d.properties[currentKey]);
});
});
// Read csv data
d3.csv('jp_pop.csv', function(error, rows) {
// Gather data from csv
keys = Object.keys(rows[0]).filter(function(key){ return key !== 'prefecture'; });
svg.selectAll('.keys')
.data(keys)
.enter()
.append('text')
.attr('class', 'keys')
.attr('x', w - 100)
.attr('y', function(d,i){return 10 + i * h / keys.length; })
.attr('value', function(d){return d;})
.attr('font-size', 10)
.style('cursor', 'pointer')
.text(function(d){return d;})
.on('click', clickKey)
// Add excess population data
for (var i = 0; i < rows.length; i++) {
var municipality = japan.filter(function(obj) {
return (obj.properties['name'] === rows[i]['prefecture']);
})[0];
if (municipality) {
//
keys.forEach(function(key){
municipality.properties[key] = rows[i][key];
});
}
}
// Set data to the first year when first load map
currentKey = keys[0];
updateMap();
});
});
</script>
</body>
</html>
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
prefecture 1955 1960 1965 1970 1975 1980 1985 1990 1995 2000 2005 2010 2015
Hokkaido 477 266 133 12 154 238 103 -35 48 -9 -55 -122 -122
Aomori 100 44 -10 11 41 55 0 -41 -1 -6 -39 -64 -64
Iwate 80 22 -38 -40 15 36 12 -17 3 -4 -31 -55 -50
Miyagi 64 16 10 66 136 127 94 73 80 36 -5 -12 -14
Akita 40 -13 -56 -39 -9 25 -3 -27 -13 -25 -43 -60 -63
Yamagata -3 -33 -58 -37 -6 32 10 -4 -1 -13 -28 -47 -46
Fukushima 33 -44 -67 -38 25 64 45 24 30 -7 -36 -62 -115
Ibaraki 25 -17 9 88 198 216 167 120 111 30 -11 -5 -52
Tochigi -2 -34 8 58 118 94 74 69 49 21 12 -9 -33
Gumma 13 -36 28 53 97 93 72 45 38 21 -1 -16 -35
Saitama 117 168 584 851 955 599 444 541 354 179 116 141 66
Chiba 66 101 396 665 782 586 413 407 243 128 130 160 8
Tokyo 1759 1647 1185 539 266 -56 211 27 -82 290 513 582 355
Kanagawa 431 524 988 1041 926 526 508 548 266 244 302 256 79
Niigata 12 -31 -43 -38 31 59 27 -3 13 -12 -45 -57 -69
Toyama 12 12 -8 5 41 32 15 2 3 -2 -9 -19 -26
Ishikawa 9 7 7 22 68 49 33 13 15 1 -7 -4 -16
Fukui 2 -1 -2 -7 30 20 24 6 3 2 -7 -16 -19
Yamanashi -4 -25 -19 -1 21 21 29 20 29 6 -3 -22 -28
Nagano -40 -39 -24 -1 61 66 53 20 37 21 -19 -44 -52
Gifu 39 54 62 59 109 92 69 38 33 8 -1 -26 -48
Shizuoka 179 106 157 177 219 138 128 96 67 29 25 -27 -64
Aichi 378 437 593 587 538 298 233 236 177 175 212 156 73
Mie 25 -1 29 29 83 61 60 46 48 16 10 -12 -39
Shiga -7 -11 10 37 96 94 76 66 65 56 37 31 2
Kyoto 102 58 110 147 175 102 60 15 28 14 4 -12 -26
Osaka 761 887 1152 963 659 194 195 67 62 8 12 48 -26
Hyogo 311 285 404 358 324 153 133 127 -3 149 40 -3 -51
Nara 13 4 45 104 147 132 96 70 56 12 -22 -20 -36
Wakayama 25 -5 25 16 29 15 0 -13 6 -10 -34 -34 -38
Tottori 14 -15 -19 -11 12 23 12 0 -1 -2 -6 -18 -15
Shimane 16 -40 -67 -48 -5 16 10 -14 -10 -9 -20 -25 -23
Okayama 29 -20 -25 62 107 57 46 9 25 0 6 -12 -23
Hiroshima 67 35 97 155 210 93 80 31 32 -3 -2 -16 -16
Yamaguchi 69 -8 -58 -33 44 32 15 -29 -17 -28 -35 -42 -46
Tokushima -1 -31 -32 -24 14 20 10 -3 0 -8 -14 -25 -29
Kagawa -2 -25 -18 7 53 39 23 0 4 -4 -11 -16 -19
Ehime 19 -40 -55 -28 47 42 23 -15 -8 -14 -25 -37 -45
Kochi 9 -28 -42 -26 21 23 9 -15 -8 -3 -18 -32 -36
Fukuoka 330 147 -42 62 266 260 166 92 122 83 34 22 31
Saga 29 -31 -71 -34 0 28 14 -2 6 -7 -11 -16 -17
Nagasaki 103 12 -119 -71 2 19 3 -31 -18 -28 -38 -52 -49
Kumamoto 68 -40 -85 -71 15 75 48 2 20 -1 -17 -25 -30
Oita 24 -37 -53 -31 34 39 21 -13 -6 -10 -11 -13 -30
Miyazaki 48 -4 -54 -30 34 67 24 -7 7 -6 -17 -18 -31
Kagoshima 240 -81 -109 -125 -5 61 34 -21 -4 -8 -33 -47 -57
Okinawa -114 82 51 11 98 64 72 43 51 45 44 31 41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment