如何实时更新QChartPyQt5?

问题描述

我必须使用QChart绘制传入的数据流。

要检查我已实现的代码,我获取了一个带有时间序列数据的csv文件,该文件会定期读取并附加到QLineSeries中。该系列已添加到QChart中。但是,在运行代码后,该图完全无法显示。

class Window(QMainWindow):
    def __init__(self,*args,**kwargs):
        super(Window,self).__init__()
        self.maxLen =  int(frame_rate*time_window) 
        self.data = []
        self.source = source

        chart = QChart()
        self.series = QLineSeries(self)
        chart.addSeries(self.series)
        chart.createDefaultAxes()
        chart.setAnimationOptions(QChart.SeriesAnimations)

        chartview = QChartView(chart)
        chartview.setRenderHint(QtGui.QPainter.Antialiasing)
        self.setCentralWidget(chartview)

        self.count = 0

    def update(self):

        new_data = source.iloc[self.count,4]

        if len(self.data) > self.maxLen:
            self.data= self.data[1:]

        self.data.append((self.count,new_data))

        self.series = list(self.data)

        App.processEvents()
        self.count += 1

if __name__ == '__main__':

    time_window = 5 #secs
    frame_rate = 20
    source = pd.read_csv('source.csv')

    App = QApplication(sys.argv)
    plot = Window(time_window,frame_rate,source)
    plot.show()
    graphUpdateSpeedMs = 1000/frame_rate
    timer = QtCore.QTimer()
    timer.timeout.connect(plot.update)
    timer.start(graphUpdateSpeedMs)
    App.instance().exec_()

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...