As a side project, I've created a website that should highlight specific countries and therfore needed a customized map.
I've achieved that using Datamaps by Mark DiMarco. It's easy and very customizable to show the world, a continent or a single country including labels connections and much more, so let's get started!
1. Include the necessary files
Include the libraries D3.js (for the visualization) and Topojson (for the geographical data) and of course the most important: Datamaps.js
You can easily include D3.js and Topojson through the CDN Cloudflare. Similar to Datamaps hosted on Github, however it is recommended to download it. Here you can get Datamaps.js and choose the right map for you in the dist folder. I want to create a world map so I took the datamaps.world.min.js
file.
2. Create the actual map
We create a container and set the position to relative to embed the map within.
To include the Datamap itself we create a Datamap object as follows.
Note: I've added the responsive variable and set it to true since the default is false.
Responsive map
In order to have a responsive map, we shouldn't set the width and height of the container (however you could use relative values like 70%) and add an event listener that will be triggered when the window size changes.
3. Customize the map: Colorise countries
Now we want to customize our map further, in particular the colors of each country. First, we should be aware of how many groups we want to create meaning how many colors we want to use in the map. You then name each group with a variable (called fillKey) and set the color in hex code, RGB or RGBA.
In my case, I only used two groups so I have set the defaultFill Variable and created another variable called "visumfrei". This will be added within the datamaps object:
We now want to set the colors of each country by using the created color fill variables. Because I just added another color fill variable, I just had to set those countries. All the others will be set to the "defaulfFill" color. To set the color of each country we use the three digit country code and match it with the previously created fillKey, in my case "visumfrei". It will look like this:
Don't worry I got your back with the full list of all countries and their corresponding three digit country code:
Note: I came across the issue that three countries: "Kosovo, Northern Cyprus and Somaliland" can't be customized independently. Googling those countries I came up with the reason that those are not recognized as independent countries by many (well something new I learned on the go). To address those countries you can use the key "-99" instead of the normal country code key.
There are more customization options, connections, bubbles labels… You'll find all customization options of Datamaps here.
Result
Done, now you see the final map as we customized it!
This is the full code example used to create the map above: