Skip to content

Instantly share code, notes, and snippets.

@toja
Last active September 19, 2017 21:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toja/49a09c867c32d5c9f201204f41e0c488 to your computer and use it in GitHub Desktop.
Save toja/49a09c867c32d5c9f201204f41e0c488 to your computer and use it in GitHub Desktop.
World population - Dorling
license: gpl-3.0
height: 500
border: no
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
<!DOCTYPE html>
<meta charset="utf-8">
<script src="//d3js.org/d3.v4.min.js"></script>
<script src="//d3js.org/d3-geo-projection.v1.min.js"></script>
<script src="//d3js.org/topojson.v2.min.js"></script>
<style>
.land {
fill: #323436;
stroke: none;
opacity: 0.5;
}
.border {
fill: none;
stroke: #727476;
stroke-width: .5px;
stroke-linejoin: round;
stroke-linecap: round;
}
.graticule {
fill: none;
stroke: #323436;
stroke-width: .5px;
}
.stroke {
fill: none;
stroke: #121416;
stroke-width: .5px;
}
.fill {
fill: #121416;
}
.bubble {
stroke: none;
stroke-width: .5px;
}
.q0-9 { fill:rgba(255,247,243, 0.1); }
.q1-9 { fill:rgba(253,224,221, 0.2); }
.q2-9 { fill:rgba(252,197,192, 0.3); }
.q3-9 { fill:rgba(250,159,181, 0.35); }
.q4-9 { fill:rgba(247,104,161, 0.4); }
.q5-9 { fill:rgba(221,52,151, 0.45); }
.q6-9 { fill:rgba(174,1,126, 0.5); }
.q7-9 { fill:rgba(122,1,119, 0.55); }
.q8-9 { fill:rgba(73,0,106, 0.6); }
</style>
<body>
<script>
var width = 960,
height = 500;
var radius = d3.scaleSqrt()
.domain([0, 1.45e9])
.range([0, 32]);
var proj = d3.geoCylindricalStereographic()
.parallel(35) // DIN A4
.precision(.1);
var path = d3.geoPath()
.projection (proj);
var quantile = d3.scaleQuantile()
.range(d3.range(9).map(function(i) { return "q" + i + "-9"; }));
var dichteById = d3.map();
// start map
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
var defs = svg.append("defs");
defs.append("path")
.datum({type: "Sphere"})
.attr("id", "sphere")
.attr("d", path)
defs.append("clipPath")
.attr("id", "clip")
.append("use")
.attr("xlink:href", "#sphere");
svg.append("use")
.attr("class", "stroke")
.attr("xlink:href", "#sphere");
svg.append("use")
.attr("class", "fill")
.attr("xlink:href", "#sphere");
var features = svg.append("g")
.attr("class", "land");
var bubbles = svg.append("g");
function renderWorld(world) {
// Graticule
features.append("path")
.datum(d3.geoGraticule())
.attr("class", "graticule")
.attr("clip-path", "url(#clip)")
.attr("d", path);
// world land
features.selectAll("countries")
.data(topojson.feature(world, world.objects.countries).features)
.enter().append("path")
.attr("clip-path", "url(#clip)")
.attr("d", path);
// country borders
features.append("path")
.datum(topojson.mesh(world, world.objects.countries, function(a, b) { return a !== b; }))
.attr("class", "border")
.attr("clip-path", "url(#clip)")
.attr("d", path);
}
function renderBubbles(nodes) {
var node = bubbles.selectAll("circle")
.data(nodes)
.enter().append("circle")
.attr("class", function(d) { return quantile(dichteById.get(d.n)); })
.attr("r", function(d) {return d.r;} );
node.append("title").text(function(d) {return d.n;});
var simulation = d3.forceSimulation(nodes)
.velocityDecay(0.8)
.force("x", d3.forceX().x(function(d) { return d.x; }))
.force("y", d3.forceY().y(function(d) { return d.y; }))
.force("collide", d3.forceCollide().radius(function(d) { return d.r }).strength(0.5))
.on("tick", function() {
node
.attr("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; })
});
}
d3.json("50m.json", function (error, world) {
if (error) throw error;
renderWorld(world);
});
d3.json("points.geojson", function (error, points) {
if (error) throw error;
// set domain for quantiles based on data
for (var i = 0; i < points.features.length; ++i) {
dichteById.set(points.features[i].properties.name, +points.features[i].properties.dichte);
}
quantile.domain(dichteById.values());
// create a set of nodes for dorling cartogram
var nodes = points.features.map(function(d) {
var point = proj(d.geometry.coordinates);
return {
x: point[0], y: point[1],
//x0: point[0], y0: point[1],
r: radius(d.properties.pop_est),
n: d.properties.name
}
});
// render cartogram
renderBubbles(nodes);
});
</script>
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment