简体   繁体   English

使用Python,如何以编程方式为Pygal图表更新数据?

[英]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: 注意事项/问题:

  • My values, that I would like to update, come from a REST call as strings every five minutes. 我想更新的值是每五分钟来自一次REST调用的字符串。
  • I haven't figured out how to update data for the Pygal chart, because inserting strings into the lists cause an error (shown below). 我还没有弄清楚如何为Pygal图表更新数据,因为将字符串插入列表会导致错误(如下所示)。
  • I don't know how to insert a list of new integers into the "class" below. 我不知道如何在下面的“类”中插入新整数列表。
  • I'm showing only five data points for this example, but I might need to have as many as 100 data points eventually. 在此示例中,我仅显示五个数据点,但最终可能需要多达100个数据点。
  • Setting variables might work for a static number of elements but my elements will increase and decrease sporadically. 设置变量可能适用于静态数量的元素,但是我的元素会偶尔增加和减少。

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.

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