Candra set the challenge this week to use the new map layers to build a map display which, on click of a country, filtered the display to that country and additionally displayed a donut chart indicating the percentage of urban dwellers in that country.
If map layers are very new to you, then the webinar by Adam McCann referenced in the challenge, has some VERY useful pointers for this challenge (the workbook for that can be downloaded from here, as I found some things needed closer inspection).
I think this challenge is going to be best described by walking through the steps.
Building the 1st map layer
Double-Click on Country/Region to load a map, and change the mark type to (filled) Map. Add Region to the Colour shelf and assign the appropriate colours. Adjust the map background via the Map -> Map Layers menu and set the Style to dark, and remove all the selections against the Map Layers list
The intention is when a country is selected the map will ‘drill into’ /filter that country, and display additional information. We will drive this by a parameter, which will get set via a parameter action, but for now we’ll manually set the value.
Create a new string parameter, that is default to nothing/empty string/ ”, and then show this on your sheet.
We need the 1st layer of the map to display, when there is no value in the parameter. We need a calculated field to help drive this.
IF [pSelectedCountry]=” THEN [Country/Region] END
By default this will create a field of type ‘string’ but we need it to be a geographic data type. so change this as below.
Add this field to the Detail shelf of the map, and remove the Country/Region field that was automatically added when we first built the map.
Enter the name of a country, eg China, into the parameter. The map should essentially go blank (black screen).
Building the 2nd map layer
We need the country to display, if it’s entered into the parameter. For this, we need another calculated field
IF [pSelectedCountry]=[Country/Region] THEN [Country/Region] END
Once again change this to a geographic role data type of type County/Region.
Next click and drag this field onto the map, and drop it onto the Add Marks Layer option that displays. This will create a new Selected Country marks card, although nothing will obviously change on the map display itself.
Move the Selected Country pill to be on the Detail shelf instead, and add Region onto the Colour shelf. Change the mark type from circle to Map.
Now if you enter a county into the parameter, eg China, the display should ‘filter’ & ‘zoom in’ on China.
So what we have is the 1st layer only showing when no countries have been selected, and vice versa, the 2nd layer only showing when a country has been selected.
We need to now add further layers for the donut chart, which only want to show the country has been selected as well.
Building the 3rd map layer
A donut chart, in the past, is traditionally created by building a pie chart, then using a dual axis to add a circle, sized smaller that the pie chart, on top (see this Tableau KB for info). Rather than a dual axis, we’re going to use map layers – 1 layer for the pie chart, and then another layer for the central circle.
Keeping a country selected (so we can see what we’re building), drag Selected Country onto the map again to create another map layer. Change the mark type to Pie and increase the Size to as large as possible. Move Selected Country to Detail.
In the data set we have a field called Population Urban which stores the ‘percentage’ value of urban dwellers eg 0.17 is 17%. To create the angles for the pie chart, we need to know
Drag Measure Values onto the Angle shelf. This will automatically add Measure Names to the Filter shelf. Edit the filter to just select the Population Urban and Population Non-Urban measures. Drag the Measure Names field that was also automatically added to the Detail shelf, to the Colour shelf. Adjust colours accordingly, and set the border of the pie chart to white (under the Colour shelf options).
Verify that if you set the parameter to empty again, the whole world map displays, and you can’t see any pie charts.
Building the 4th map layer
Now we need to make the donut hole. Once again, ensure a country is selected, so your pie chart is visible, then drag Selected Country onto the map again, and drop to add another map layer.
This time, move the Selected Country field onto the Detail shelf, add Region to the Colour shelf, and adjust the size of the circle, so its smaller than the pie. Set the border of the circle to be white again too,
Add Population Urban onto the Label shelf, and format to a percentage with 0 dp. The best way to do this, is to format the Population Urban measure in the data pane (right click->default properties -> number format).
At this point you’ll notice the number is huge… we need to add Year to the Filter shelf, and select 2012.
Align the label to be middle centre, and adjust the font to be much bigger text. Add ‘urban dwellers’ underneath.
Once again, verify you get the expected behaviour as you change the values in the parameter from nothing to Russia or China etc.
The final step on this sheet is to add text to the Tooltips. Unlike when working with dual axis, you don’t have an All marks card, so you’ll need to add the required fields (Country/Region, Region, Population Total (formatted to Millions with 0dp), Population Urban to the Tooltip shelf on each of the relevant layers.
Setting the parameter interactively
Create a dashboard sheet, and add the map sheet you’ve built. Then create a dashboard action which sets the pSelectedCountry parameter, impacting the All Countries field, and that when the selection is cleared, the value is reset to ”.
My published viz is available here. Enjoy!
Happy vizzin’! Stay Safe!