block by rveciana f46df2272b289a9ce4e7

Automatic colouring World Map using Canvas

Full Screen

This example is the same as this Jason Davies GIST, but using Canvas instead of SVG.

How is the color selected:

The place was easy to find

context.fillStyle = color(d.color = d3.max(neighbors[i], function(n) { return countries[n].color; }) + 1 | 0);

I had to think for some time to understand how does this work. Let’s separate it:

color(
    d.color = 
            d3.max(
                    neighbors[i], 
                    function(n) { return countries[n].color; }
                    ) 
                    + 1 | 0
);

Going from the inside to the outside:

index.html