We love Moms!

We love Moms!

Mike Bostock’s (@mbostock) D3.js library makes use of SVG, HTML5 and CSS standards to create interactive data visualisations for the web. He has hundreds of examples of amazing visualisations created using this library.

I’ve been using the library to make some data visualisations of my own and as part of this I wanted to create a simple and reusable world map template. The idea is that I will be able to use this standard map as a base for future visualisations so it should be possible to customise it very easily.

This is a heading

You can play with the completed map and see the code on Codepen. Below I describe how it was coded and how the various functionalities were achieved.

The map should support “standard” zoom functionality as follows:

  • The user will be able to zoom in by double clicking on the map and zoom out by pressing shift whilst double clicking.
  • Similarly the user can zoom in or out by pinching on the trackpad.
  • The user can also pan the map by clicking and dragging.
  • In addition, as described above the map will zoom in on a particular country when the user clicks on that country.

We will also define some limits on the zoom/pan functionality:

  • The limits of “zoom out” are set by the size of the view. It will not be possible to zoom out so much that the map is smaller than the view.
  • Similarly the pan will be restricted so that the map can’t be moved beyond the edges of the view.
  • We will set an arbitrary maximum zoom.