Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I adjust tooltip in Pygal?

Tags:

python

pygal

I tried to produce a bar chart that visualizes the most-starred projects on GitHub. I added 'label' and 'xlink' inside tooltips, however, 'label' contents are not very well fit in for some items, also, some links are not shown up in certain projects' tooltip. See below,

enter image description here

Below is the Python code using Pygal module, run it and see the .svg file yourself.

import requests, pygal


url = 'https://api.github.com/search/repositories?q=language:python&sort=star'
r = requests.get(url)
repo_list = r.json()['items']

names, stars = [], []
for k in repo_list:
    names.append(k['name'])
    temp = {
            'value': k['stargazers_count'], 
            'label': k['description'],
            'xlink': k['html_url'],
           }
    stars.append(temp)

my_config = pygal.Config() 
my_config.x_label_rotation = 45

chart = pygal.Bar(my_config)
chart.title = 'GitHub, Python Most Starred Projects'
chart.x_labels = names
chart.add('', stars)
chart.render_to_file('MyFile.svg', force_uri_protocol = 'http')

How do I solve this problem, either by adjusting font size of tooltip or tooltip window size?

like image 389
Nicholas Avatar asked Oct 18 '25 10:10

Nicholas


1 Answers

I don't think there's a clean way to do this; I think you'd need to modify the .svg file directly. There's an open issue on the Pygal project asking this question. Someone seems to have found a solution using \n and the force_uri_protocol='http', but that doesn't work for me. On my machine \n just gets converted to a single space.

The best I could come up with is truncating the description to a certain number of characters, something like this:

temp = {
        'value': k['stargazers_count'], 
        'label': k['description'][:80] + "..." ,
        'xlink': k['html_url'],
       }

If you like this solution you can add some logic so short labels don't have the ellipsis at the end. I think this is the solution I'll use next time I run into this issue.

like image 159
japhyr Avatar answered Oct 20 '25 01:10

japhyr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!