[英]Using Python, how can I update data programmatically for a Pygal chart?
Using Python, how can I update data programmatically for a Pygal chart? 使用Python,如何以编程方式为Pygal图表更新数据?
The code below works fine as a static Stacked-Bar chart, but I can't figure out how to accommodate changing values. 下面的代码可以很好地用作静态Stacked-Bar图表,但是我无法弄清楚如何容纳变化的值。
Considerations/issues: 注意事项/问题:
import pygal
line_chart = pygal.HorizontalStackedBar()
line_chart.title = 'Application Health'
line_chart.x_labels = ( "cluster05", "cluster04", "cluster03", "cluster02", "cluster01")
line_chart.add('Critical', [2, 5, 4, 1, None])
line_chart.add('Warning',[1, 7, 2, None, 2])
line_chart.add('OK', [25, 30, 19, 20, 25])
line_chart.render_to_file('test_StackedBar.svg')
The line type is the following class. 线型是以下类。
>>> type(line_chart.add('OK', [25, 30, 19, 20, 25]))
<class 'pygal.graph.horizontalstackedbar.HorizontalStackedBar'>
>>>
newData = "21, 55, 35, 82, 47, 70, 60"
line_chart.add('OK',[newData])
TypeError: unsupported operand type(s) for +: 'int' and 'str'
newData = "21, 55, 35, 82, 47, 70, 60"
y = list(newData)
line_chart.add('OK',[y])
line_chart.render_to_file('test_StackedBar.svg')
TypeError: unsupported operand type(s) for +: 'int' and 'list'
You have a TypeError
. 您有一个TypeError
。 it expects a list of numbers. 它需要一个数字列表。
It's easy to think of variables as substitutions: y = [1,2,3]
means anywhere there is a y
, just put [1,2,3]
instead. 将变量视为替换很容易: y = [1,2,3]
表示在任何存在y
地方,只需放[1,2,3]
。 So when you do [y]
, it happily replaces it with [[1,2,3]]
因此,当您执行[y]
,它会愉快地替换为[[1,2,3]]
You're giving it a list containing a single string: ["21, 55, 35, 82, 47, 70, 60"]
, then passing that inside a list: [["21, 55, 35, 82, 47, 70, 60"]]
, which is not what it expects. 您要给它提供一个包含单个字符串的列表: ["21, 55, 35, 82, 47, 70, 60"]
,然后将其传递到列表中: [["21, 55, 35, 82, 47, 70, 60"]]
,这不是预期的结果。
It wants a list of integers: 它需要一个整数列表:
y = [21, 55, 35, 82, 47, 70, 60]
line_chart.add('OK',y)
To update the chart, you'll likely have to rebuild it (I'm not familiar with pygal, so it may support editing in place, but rebuilding will always work) 要更新图表,您可能必须对其进行重建(我对pygal不熟悉,因此它可能支持就地编辑,但是重建始终可以进行)
def update_chart(crit_values, warning_values, ok_values):
line_chart = pygal.HorizontalStackedBar()
line_chart.title = 'Application Health'
line_chart.x_labels = ( "cluster05", "cluster04", "cluster03", "cluster02", "cluster01")
line_chart.add('Critical', crit_values)
line_chart.add('Warning', warning_values)
line_chart.add('OK', ok_values)
line_chart.render_to_file('test_StackedBar.svg')
Will allow you to rebuild it and pass it new lists for each type, by doing: 将允许您通过执行以下操作来重建它并为每种类型传递新的列表:
update_chart([2, 5, 4, 1, None], [1, 7, 2, None, 2], [25, 30, 19, 20, 25])
or update_chart(x, y, z)
where x,y,z are integer lists like I showed above. update_chart([2, 5, 4, 1, None], [1, 7, 2, None, 2], [25, 30, 19, 20, 25])
update_chart(x, y, z)
update_chart([2, 5, 4, 1, None], [1, 7, 2, None, 2], [25, 30, 19, 20, 25])
或update_chart(x, y, z)
其中x, y,z是我上面显示的整数列表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.