Reputation: 1
My problem here is that I want to display a little bit of text when you click on each country, such as the Capital City of each one. For now, I have just included placeholder numbers. I tried using the str("x") command, but no success was found. What do I do here?
Here is a screenshot of what I currently have, and in the solution I want, I want "Washington DC" to be displayed in place of 66. Here is the image result I am getting
# import pygal library
import pygal
mm = pygal.maps.world.World()
# create a world map
worldmap = pygal.maps.world.World()
# set the title of the map
worldmap.title = 'Countries'
# adding the countries
worldmap.add('Random Data', {
'aq' : 69,
'cd' : 30,
'de' : 40,
'eg' : 50,
'ga' : 45,
'hk' : 23,
'in' : 0,
'jp' : 65,
'nz' : 41,
'kz' : 32,
'us' : 66
})
# save into the file
worldmap.render_to_file('worldmapresult.svg')
open('worldmapresult.svg')
print("Success")
Upvotes: 0
Views: 184
Reputation: 6799
You can use tooltip_...
from pygal! Here are the documentations for everything tooltip-related.
Add a font size and border radius for the tooltips:
worldmap.tooltip_font_size = 14
worldmap.tooltip_border_radius = 10
Now add the appropriate labels:
worldmap.value_formatter = lambda x: {
'aq': '', # does it have a capital?
'cd': 'Kinshasa',
'de': 'Berlin',
'eg': 'Cairo',
'ga': 'Libreville',
'hk': 'Hong Kong',
'in': 'New Delhi',
'jp': 'Tokyo',
'nz': 'Wellington',
'kz': 'Astana',
'us': 'Washington DC'
}[x]
Here is the full code:
# import pygal library
import pygal
# create a world map
worldmap = pygal.maps.world.World()
# set the title of the map
worldmap.title = 'Countries'
worldmap.tooltip_font_size = 14
worldmap.tooltip_border_radius = 10
worldmap.value_formatter = lambda x: {
'aq': '',
'cd': 'Kinshasa',
'de': 'Berlin',
'eg': 'Cairo',
'ga': 'Libreville',
'hk': 'Hong Kong',
'in': 'New Delhi',
'jp': 'Tokyo',
'nz': 'Wellington',
'kz': 'Astana',
'us': 'Washington DC'
}[x]
# adding the countries
worldmap.add('Random Data', {
'aq' : 69,
'cd' : 30,
'de' : 40,
'eg' : 50,
'ga' : 45,
'hk' : 23,
'in' : 0,
'jp' : 65,
'nz' : 41,
'kz' : 32,
'us' : 66
})
# save into the file
worldmap.render_to_file('worldmapresult.svg')
open('worldmapresult.svg')
print("Success")
Upvotes: 0