Custom Zoom Control for (Multiple) Maps
Aug 27, 2018
Last week I wrote about how to create bounding boxes in Tableau. In my most recent visualization I used them to create a custom zoom control for multiple maps. Click the dashboard to play with the interactive version on Tableau Public.
With the ‘zoom in/out’-parameter on the left side you can vary the size of the bounding boxes and because of the automatic adjusting that tableau does with its maps it looks like you are zooming in and out. If I would fix the maps (see again last week’s post on bounding boxes) a change of the parameter would only change the size of the boxes in the view.
In this post I want to share the steps I went through to build this visualization.
I had the idea to look at centers of global capital cities, mainly because it is so easy in Tableau to identify the geographical centers:
As described in last week’s post it needs five points to draw a bounding box. Therefore, I quintupled the data and identified each partition as my point order. That’s all data preparation you need (and even this step could have been done in Tableau).
I could then construct my bounding box around my city centers with ∆ lon as width, ∆ lat as height and some basic math:
But then it got tricky. An early version of my viz looked something like this:
All boxes are sized differently. What is going on here?
I’m not a spatial expert and it took me some time to figure it out. As described in 3. I wanted to add or subtract a lat/lon to my center lat/lon. Unfortunately, a distance expressed in lat/lon is depending on where you are located on the globe (therefore the different sizes of the boxes, the cities are spread around the world).
Source: Wikipedia
This is due to the distance between two longitudes. While the distance between two latitudes is (more or less) constant (111.12 km), the distance between to longitudes has its maximum amplitude at the equator (where it is also 111.12 km) and is zero at the poles.
The distance between two longitudes can be calculated like this:
Distance = 1 nautical mile * 60 * cos(Latitude)
Distance at the equator = 1,852 m * 60 * cos(0) = 111,120 m (or 111.12 km) with cos(0)=1
The distance at a latitude of 45 degrees would for example be
1,852 m * 60 * cos(45) = 78,573.7 m
That means: a distance expressed in longitude has different lengths around the globe, depending on the latitude. To kill this effect, I just had to normalize the ∆ lon with the cosine of the latitude which I calculated in Tableau with the following formula:
So far so good.
In an early version I was now able to create two parameters for height and width (expressed in lat and lon) and to draw the boxes, but then I felt it would be cool to have only one parameter expressing the size of the box (s) in square kilometer. Therefore I had to calculate height (x) and width (y) within Tableau.
To do so it needed the following two equations:
(1) s = x * y
(2) f = y/x with f=format of the box (e. g. 16:9, 4:3 or 1:1)
I could then calculate height and width in meter based on my parameters Square Kilometers, format and lat/lon per 100 m:
resulting in these two calculated fields:
The last step before creating the view was to calculate the longitudes and latitudes for each partition of my data.
For the latitudes:
For the longitudes:
To create the viz I went through similar steps like described in my previous post.
For the second axis I chose circles from the marks card to place my label with the following formula:
That’s it (+ a little bit of formatting)!
Hope you enjoyed this post and you’ll find own use cases for this technique!
3 COMMENTS
Marlin says:
Thanks Klaus. Happy to hear that my thinking is correct. I’ll give it a go, although I’m still very new to Tableau. Will let you know if I got it done.
Marlin says:
Hi Klaus, great idea! What Tableau has been missing is the ability to synchronise 2 maps, with the same zoom level and centre of zoom. It looks like your post solved the first problem. Am I correct to assume that, if the centre of the source map is determined and applied to the other maps, we can achieve the 2nd requirements, hence, a complete synchronisation of 2 maps on a dashboard?
If my assumption is correct, would you be able to point me to the right direction to achieve that?
Thanks
Marlin
Klaus Schulte says:
Hi Marlin, simple answer: YES! 🙂 If you only want the zoom functionality, you wouldn’t need to draw the box with five points, two points would be enough (for example top left and bottom right), so you would only need two copies of the data.
Not sure about pointing you to the right direction to achieve this though. The blog already explains every step to achieve it!