简体   繁体   English

如何使用带有字符串记录的pygal制作条形表?

[英]How to make a bar table using pygal with string records.?

I am creating table using pygal , the example in the link works well, but I did the below change in it. 我正在使用pygal创建表, 链接中的示例效果很好,但是我做了以下更改。

line_chart = pygal.Bar()
line_chart.title = 'Browser usage evolution (in %)'
line_chart.x_labels = map(str, range(2002, 2013))
line_chart.add('Firefox', ["Hello", 'world', 'aaa', 'bbbb',   'ccc',   'cccc', 'dddd', 'eee', 'ffff', 'gggg', 'hhhh'])
line_chart.add('Chrome',  [None, None, None, None, None, None,    0,  3.9, 10.8, 23.8, 35.3])
line_chart.add('IE',      [85.8, 84.6, 84.7, 74.5,   66, 58.6, 54.7, 44.8, 36.2, 26.6, 20.1])
line_chart.add('Others',  [14.2, 15.4, 15.3,  8.9,    9, 10.4,  8.9,  5.8,  6.7,  6.8,  7.5])
line_chart.value_formatter = lambda x: '%.2f%%' % x if x is not None else '∅'
line_chart.render_table(style=True)

for the Firefox column I am adding string values in the list, it is giving me error like: 对于Firefox列,我在列表中添加了字符串值,这给了我类似以下错误:

TypeError: unsupported operand type(s) for -: 'str' and 'int' TypeError:-:“ str”和“ int”的不受支持的操作数类型

How can I include string records in the pygal table? 如何在pygal表中包含字符串记录?

To understand your issue you're going to need to understand this line: 要了解您的问题,您需要了解以下内容:

line_chart.value_formatter = lambda x: '%.2f%%' % x if x is not None else '∅'

what pygal is doing here is taking in a function to apply to each table value in order to get a string value out to be put into the actual table. pygal在这里所做的工作是接受一个函数,将其应用于每个表值,以便将字符串值输出到实际表中。 In this case the formatter takes in a number (int or float) and uses the older formatting syntax for formatting. 在这种情况下,格式化程序采用数字(int或float),并使用较旧的格式化语法进行格式化。 '%.2f%%' % x basically says "I'm going to put x here, modify the string representation such that it forces at least two decimal places, and then add a '%' symbol to the end". '%.2f%%' % x basically说“我将x放在这里,修改字符串表示形式,使其强制至少两个小数位,然后在结尾添加一个'%'符号”。 Additionally this lambda function checks if the value is not None before doing this, because formatting the string value in this way wouldn't work with a None value since it isn't a number, and outputs that null character. 另外,此lambda函数在执行此操作之前会检查该值是否不为None ,因为以这种方式格式化字符串值将无法使用None值,因为它不是数字,并输出该null字符。

In the original example, all table values where numerical values. 在原始示例中,所有表值都为数值。 Once you replaced the firefox column/row with string values the formatter couldn't work. 一旦将firefox列/行替换为字符串值,格式化程序将无法工作。 Your error comes from the function lambda x: '%.2f%%' % x if x is not None else '∅' not being able to be applied to string values. 您的错误来自函数lambda x: '%.2f%%' % x if x is not None else '∅'无法应用于字符串值。 In order to fix this you can either A: change the formatter to accept string values or B: change the values in firefox to use numbers instead of strings. 为了解决这个问题,您可以A:更改格式器以接受字符串值,或者B:将firefox中的值更改为使用数字而不是字符串。

You wanted String records, so to change this you can add a check for type (ie using type(value) == int ) or if you also want to use string integers, try a conversion first (ie int(value) or float(value) ). 您需要String记录,因此要更改此记录,可以添加类型检查(即,使用type(value) == int ),或者如果您还想使用字符串整数,请先尝试进行转换(即int(value)float(value) )。 See this post for more details on int checking for strings. 有关int检查字符串的更多详细信息,请参见这篇文章

Your formatter lambda function might for example become: 例如,您的格式化程序lambda函数可能变为:

lambda x: '%.2f%%' % x if type(x) is int or type(x) is float else '∅'

if you want to do something like the conversion checker, you are going to have to try: catch: it, so you'll need to make an actual formatter function instead of a lambda. 如果您想做类似转换检查器的操作,则必须尝试:catch:它,因此您需要制作一个实际的格式化程序函数而不是lambda函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM