This tutorial is a great intro to creating choropleths with d3, but it\'s data is US-centric. Where do I get the corresponding data for a world map?
I\'m sure it\'s in t
NOTE: This answer was written for version 2 of d3. Version 3 is out now, and it has awesome new features that create better geometry segmentation and solve the fill issue mentioned below. Also, the interface for setting up projections _may_ have changed in V3 (not sure bc I haven't tried).
There is a json file available for the entire world, and you can render it equivalently to the us-states.json coropleth (using an Albers equal area projection) – assuming you understand (and are ok with) the fact that an Albers-projected world map looks like this, which is not quite how most people recognize a world map.
First you need the json data for the entire world, which is the same data used by the world mercator projection example.
Then you need to render the world json data using a customized Albers projection:
Mercator Projection
origin()
, parallels()
, translate()
, and scale()
values can be tweaked to get different results.
There's a problem with Antartica, which – due to the nature of this projection – gets "flipped" and is not a closed shape, so its fill covers the whole world. You either have to remove it from the json, or don't apply fill to it.
Also for some reason Brazil (and couple of others) didn't get rendered properly when I tried this. Dunno why. You'd have to examine the svg and the data to figure out why.