如何在 Pygal 中调整工具提示?
How do I adjust tooltip in Pygal?
我试图制作一个条形图,以可视化 GitHub 上最受关注的项目。我在工具提示中添加了 'label'
和 'xlink'
,但是,'label'
内容不太适合某些项目,而且某些链接没有显示在某些项目的工具提示中。见下文,
下面是Python使用Pygal模块的代码,运行自己看.svg文件。
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')
如何解决这个问题,通过调整工具提示的字体大小或工具提示 window 大小?
我认为没有一种干净的方法可以做到这一点;我认为您需要直接修改 .svg 文件。有一个 open issue on the Pygal project asking this question. Someone seems to have found a solution 使用 \n
和 force_uri_protocol='http'
,但这对我不起作用。在我的机器上 \n
只是转换为单个 space.
我能想出的最好办法是 运行将描述添加到一定数量的字符,像这样:
temp = {
'value': k['stargazers_count'],
'label': k['description'][:80] + "..." ,
'xlink': k['html_url'],
}
如果你喜欢这个解决方案,你可以添加一些逻辑,这样短标签就不会在末尾有省略号。我认为这是我下次 运行 解决此问题时将使用的解决方案。
我试图制作一个条形图,以可视化 GitHub 上最受关注的项目。我在工具提示中添加了 'label'
和 'xlink'
,但是,'label'
内容不太适合某些项目,而且某些链接没有显示在某些项目的工具提示中。见下文,
下面是Python使用Pygal模块的代码,运行自己看.svg文件。
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')
如何解决这个问题,通过调整工具提示的字体大小或工具提示 window 大小?
我认为没有一种干净的方法可以做到这一点;我认为您需要直接修改 .svg 文件。有一个 open issue on the Pygal project asking this question. Someone seems to have found a solution 使用 \n
和 force_uri_protocol='http'
,但这对我不起作用。在我的机器上 \n
只是转换为单个 space.
我能想出的最好办法是 运行将描述添加到一定数量的字符,像这样:
temp = {
'value': k['stargazers_count'],
'label': k['description'][:80] + "..." ,
'xlink': k['html_url'],
}
如果你喜欢这个解决方案,你可以添加一些逻辑,这样短标签就不会在末尾有省略号。我认为这是我下次 运行 解决此问题时将使用的解决方案。