Last active
October 22, 2015 12:19
-
-
Save niallmackenzie/45a0539e9adc6630078e to your computer and use it in GitHub Desktop.
Population Bubbles Test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<meta charset="utf-8"> | |
<style> | |
.municipality { | |
fill: #ccc; | |
} | |
.bubble { | |
fill: #7BB300; | |
fill-opacity: .5; | |
stroke: #fff; | |
stroke-width: .5px; | |
} | |
.bubble :hover { | |
stroke: #000; | |
} | |
.legend circle { | |
fill: none; | |
stroke: #ccc; | |
} | |
.legend text { | |
fill: #777; | |
font: 10px sans-serif; | |
text-anchor: middle; | |
} | |
.mesh { | |
fill: none; | |
stroke: #fff; | |
stroke-width: .5px; | |
stroke-linejoin: round; | |
} | |
.county-boundary { | |
fill: none; | |
stroke: #fff; | |
stroke-width: 2.5px; | |
pointer-events: none; | |
} | |
</style> | |
<body> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/topojson/1.6.19/topojson.min.js"></script> | |
<script> | |
var width = 960, | |
height = 500; | |
var projection = d3.geo.mercator() | |
var formatNumber = d3.format(",.0f"); | |
var radius = d3.scale.sqrt() | |
.domain([0, 5e5]) | |
.range([0, 50]); | |
// Create a path generator | |
var path = d3.geo.path() | |
.projection(projection); | |
var svg = d3.select("body").append("svg") | |
.attr("width", width) | |
.attr("height", height); | |
var legend = svg.append("g") | |
.attr("class", "legend") | |
.attr("transform", "translate(" + (width - 100) + "," + (height - 20) + ")") | |
.selectAll("g") | |
.data([1e4, 1e5, 5e5]) | |
.enter().append("g"); | |
legend.append("circle") | |
.attr("cy", function(d) { return -radius(d); }) | |
.attr("r", radius); | |
legend.append("text") | |
.attr("y", function(d) { return -2 * radius(d); }) | |
.attr("dy", "1.3em") | |
.text(d3.format(".1s")); | |
d3.json("swePop.json", function(error, swePop) { | |
if (error) throw error; | |
var komms = topojson.feature(swePop, swePop.objects.swePop), | |
jkKom = komms.features.filter(function(d) { return d.properties.KnKod == '0665'; })[0]; | |
// Create a unit projection | |
projection | |
.scale(1) | |
.translate([0, 0]); | |
// Compute the bounds of a feature of interest, then derive scale & translate | |
var b = path.bounds(jkKom), | |
// The first (boundary percetage) value is a great cheat to control the zoom | |
s = .1 / Math.max((b[1][0] - b[0][0]) / width, (b[1][1] - b[0][1]) / height), | |
t = [(width - s * (b[1][0] + b[0][0])) / 2, (height - s * (b[1][1] + b[0][1])) / 2]; | |
// Update the projection to use computed scale & translate | |
projection | |
.scale(s) | |
.translate(t); | |
svg.append("g") | |
.attr("class", "municipality") | |
.selectAll("path") | |
.data(topojson.feature(swePop, swePop.objects.swePop).features) | |
.enter().append("path") | |
.attr("d", path) | |
.append("title"); | |
//.text(function(d) { return d.properties.KnNamn; }); | |
svg.append("path") | |
.datum(topojson.mesh(swePop, swePop.objects.swePop, function(a, b) { return a !== b; })) | |
.attr("class", "mesh") | |
.attr("d", path); | |
svg.append("path") | |
.datum(topojson.mesh(swePop, swePop.objects.swePop, function(a, b) { return a.properties.LnKod !== b.properties.LnKod; })) | |
.attr("class", "county-boundary") | |
.attr("d", path); | |
svg.append("g") | |
.attr("class", "bubble") | |
.selectAll("circle") | |
.data(topojson.feature(swePop, swePop.objects.swePop).features | |
.sort(function(a, b) { return b.properties.swePop - a.properties.swePop; })) | |
.enter().append("circle") | |
.attr("transform", function(d) { return "translate(" + path.centroid(d) + ")"; }) | |
.attr("r", function(d) { return radius(d.properties.population); }) | |
.append("title") | |
.text(function(d) { | |
return d.properties.KnNamn | |
+ "\nPopulation " + formatNumber(d.properties.population); | |
}); | |
}); | |
d3.select(self.frameElement).style("height", height + "px"); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment