<script src="d3.min.js"></script>
<script src="topojson.js"></script>
<script src="d3.slider.js"></script>
<link rel="stylesheet" type="text/css" href="d3.slider.css">
<title>Japanese Regional Population Change After WW2 (5 Years Interval)</title>
background-color: lightsteelblue;
font-family: Helvetica, Arial, sans-serif;
margin: 25px auto 25px auto;
padding: 50px 50px 50px 50px;
box-shadow: 0 0 20px #ccc;
<h1>Japanese Regional Population Change After WW2 (5 Years Interval)</h1>
<h4>Unit: Thousands people</h4>
<p>The graph shows the trend of Japanese population growth since WW2. As the reader moves the slider towards present days. It shows the population growth is moving into negative territory in most areas, except Tokyo and Osaka region. Also it shows a huge growth in some regions right after the war, and the urbanization of Tokyo since the end of war. For more information, please refer to README.md file.<p>
<div class="container"></div>
<div class="slider"></div>
<script type="text/javascript">
var keys; // variable for different key(prefecture)
var currentKey; // variable for current selection key (year)
var sliderKeys = []; // year variable for sliders
// Setup canvas/container
var container = d3.select('.container');
var svg = container.append('svg')
// Assign color to domain range
var color = d3.scale.linear()
.domain([-150, 0, 500, 1000, 1800])
.range(['#feebe2', '#fbb4b9', '#f768a1', '#c51b8a', '#7a0177']);
var tooltip = d3.select("body")
// Update map color function
var updateMap = function() {
.style('fill', function(d) {
return color(d.properties[currentKey]);
svg.selectAll('rect.legend')
.attr('y', function(d, i) { return 100 + 20 * i; })
.attr('fill', function(d) { return color(d); });
svg.selectAll('text.legend')
.attr('y', function(d, i) { return 100 + 20 * i + 12; })
.attr('text-anchor', 'end')
.text(function(d) { return d; });
d3.json('japan.topojson', function(error, collection) {
var japan = topojson.feature(collection, collection.objects.japan).features;
var projection = d3.geo.mercator()
.translate([w / 2, h / 2]);
var path = d3.geo.path().projection(projection);
.attr('prefecture', function(d) {
return d.properties.name;
.style('fill', function(d, i) {
.attr("stroke", "#404040")
.attr("stroke-width", 0.5)
.style('cursor', 'pointer')
.on('mouseover', function(d){ // Mouse over action
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){
.style("left", (d3.event.pageX + 10) + "px")
.style("top", (d3.event.pageY - 40) + "px");
}) // Return to previous color once mouse moved out of region
.on('mouseout', function(d, i){
tooltip.style("opacity", 0);
//Show the year's color which is selected
var self = d3.select(this);
.style('fill', function(d, i) {
return color(d.properties[currentKey]);
d3.csv('jp_pop.csv', function(error, rows) {
keys = Object.keys(rows[0]).filter(function(key){ return key !== 'prefecture'; });
// Prepare sliderKeys for slider
sliderKeys = svg.selectAll('.keys')
var total = sliderKeys.length,
difference = sliderKeys[1] - sliderKeys[0],
max = sliderKeys[total-1];
d3.select('.slider').call(
.on("slide", function(evt, value) {
.classed("selected", function(){return (d3.select(this).attr('value') === currentKey)});
// Read population data for that year
for (var i = 0; i < rows.length; i++) {
var municipality = japan.filter(function(obj) {
return (obj.properties['name'] === rows[i]['prefecture']);
keys.forEach(function(key){
municipality.properties[key] = rows[i][key];
// Set data to the first year when first load map