Pygal条形图显示“无数据”

时间:2019-01-12 17:50:25

标签: python-3.x pygal

我正在尝试在pygal中创建条形图,该条形图将api用于黑客新闻并根据评论绘制最活跃的新闻。我在下面发布了我的代码,但是我无法弄清楚为什么我的图表一直说“没有数据” ????有什么建议么?谢谢!

import requests
import pygal

from pygal.style import LightColorizedStyle as LCS, LightenStyle as LS

from operator import itemgetter

# Make an API call, and store the response.
url = 'https://hacker-news.firebaseio.com/v0/topstories.json'
r = requests.get(url)
print("Status code:", r.status_code)

# Process information about each submission.
submission_ids = r.json()
submission_dicts = []
for submission_id in submission_ids[:30]:
    # Make a separate API call for each submission.
    url = ('https://hacker-news.firebaseio.com/v0/item/' +
           str(submission_id) + '.json')
    submission_r = requests.get(url)
    print(submission_r.status_code)
    response_dict = submission_r.json()

    submission_dict = {
        'comments': int(response_dict.get('descendants', 0)),
        'title': response_dict['title'],
        'link': 'http://news.ycombinator.com/item?id=' + str(submission_id),
    }
    submission_dicts.append(submission_dict)

# Visualization
my_style = LS('#336699', base_style=LCS)

my_config = pygal.Config()
my_config.show_legend = False
my_config.title_font_size = 24
my_config.label_font_size = 14
my_config.major_label_font_size = 18
my_config.show_y_guides = False
my_config.width = 1000

chart = pygal.Bar(my_config, style=my_style)
chart.title = 'Most Active News on Hacker News'

chart.add('', submission_dicts)
chart.render_to_file('hn_submissons_repos.svg')

1 个答案:

答案 0 :(得分:0)

传递给add函数的数组中的值必须是数字或包含键value(或两者的混合)的字典。最简单的解决方案是更改创建submission_dict时使用的键:

submission_dict = {
    'value': int(response_dict.get('descendants', 0)),
    'label': response_dict['title'],
    'xlink': 'http://news.ycombinator.com/item?id=' + str(submission_id),
}

请注意,link已成为xlink,这是Value Configuration section of the pygal docs中定义的可选参数之一。