Skip to content

Instantly share code, notes, and snippets.

@shotahorii
Created November 16, 2013 06:40
Show Gist options
  • Save shotahorii/7496746 to your computer and use it in GitHub Desktop.
Save shotahorii/7496746 to your computer and use it in GitHub Desktop.
patternables.com

Original data: 人口推計(2012年) 都道府県, 男女別人口及び人口性比-総人口, 日本人人口(平成24年10月1日現在)
Source: e-Stat
References: Donut Multiples

prefecture 0-14 Years 15-64 Years 65 Years and Over
Hokkaido 640 3398 1422
Aomori 164 822 364
Iwate 162 777 364
Miyagi 301 1491 534
Akita 118 619 326
Yamagata 145 681 326
Fukushima 252 1198 511
Ibaraki 388 1855 701
Tochigi 263 1266 463
Gunma 267 1229 496
Saitama 940 4687 1585
Chiba 791 3966 1437
Tokyo 1494 8924 2812
Kanagawa 1178 5942 1948
Niigata 292 1415 639
Toyama 138 646 299
Ishikawa 156 716 291
Fukui 109 482 208
Yamanashi 110 524 218
Nagano 288 1260 585
Gifu 283 1258 520
Shizuoka 501 2302 932
Aichi 1056 4780 1591
Mie 248 1128 465
Shiga 210 899 306
Kyoto 330 1646 649
Osaka 1152 5605 2099
Hyogo 750 3466 1355
Nara 179 856 355
Wakayama 124 583 281
Tottori 77 347 158
Shimane 90 405 212
Okayama 261 1167 507
Hiroshima 383 1746 719
Yamaguchi 180 833 418
Tokushima 94 464 217
Kagawa 131 591 268
Ehime 181 841 393
Kochi 89 436 226
Fukuoka 687 3212 1186
Saga 121 509 214
Nagasaki 188 839 380
Kumamoto 247 1081 478
Oita 153 705 327
Miyazaki 156 670 301
Kagoshima 230 1002 457
Okinawa 248 911 250
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.legend {
font-size: 12px;
}
.title {
font-size: 14px;
}
.label {
font-size: 10px;
}
.arc {
stroke: #fff;
}
</style>
<body>
<script src="http://d3js.org/d3.v3.js"></script>
<script>
var radius = 80,
padding = 10;
var arc = d3.svg.arc()
.outerRadius(radius)
.innerRadius(radius-35);
var pie = d3.layout.pie()
.value(function(d){return d.pop;});
var colour = d3.scale.ordinal()
.range(["skyblue","orange","limegreen"]);
//read data.
d3.csv("data.csv", function(error, rows){
var formatPercent = d3.format(".0%");
colour.domain(d3.keys(rows[0]).filter(function(key){return key != "prefecture";}));
rows.forEach(function(d){
d.pref = d['prefecture'],
d.total = +d['0-14 Years'] + +d['15-64 Years'] + +d['65 Years and Over'];
d.ages = colour.domain().map(function(generation){return {gen: generation, pop: +d[generation]/d.total};})
});
var legend = d3.select("body")
.append("svg")
.attr({
class: "legend",
width: (radius+padding)*2,
height: (radius+padding)*2,
})
.selectAll("g")
.data(colour.domain())
.enter()
.append("g")
.attr({
transform: function(d, i){return "translate(20," + (i+1)*20 + ")";}
});
legend.append("rect")
.attr({
width: 15,
height: 15
})
.style("fill", colour);
legend.append("text")
.attr({
x: 20,
y: 12
})
.text(function(d){return d;});
var svg = d3.select("body")
.selectAll(".pie")
.data(rows)
.enter()
.append("svg")
.attr({
class: "pie",
width: (radius+padding)*2,
height: (radius+padding)*2
})
.append("g")
.attr({
transform: "translate(" + radius + "," + radius + ")"
});
svg.selectAll(".arc")
.data(function(d){return pie(d.ages);})
.enter()
.append("path")
.attr({
class: "arc",
d: arc
})
.style("fill", function(d){return colour(d.data.gen);});
svg.selectAll(".label")
.data(function(d){return pie(d.ages);})
.enter()
.append("text")
.attr({
class: "label",
transform: function(d){return "translate("+arc.centroid(d)+")";}
})
.style("text-anchor", "middle")
.text(function(d){return formatPercent(d.data.pop);});
svg.append("text")
.attr({
class: "title",
y: 5
})
.style("text-anchor", "middle")
.text(function(d){return d.pref;});
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment