我们可以更新此代码以显示使用QTimer
的更新图吗?我已经在series.append
事件中尝试过QTimer.timeout
语句,但是它创建了一个新的图形线。谁能告诉我为什么会这样吗?以及如何使其成为可滚动的图表?希望此图表在新条目存储在 MySQL 中时进行更新。
预先感谢
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtChart import *
#https://doc.qt.io/qt-5/qtcharts-datetimeaxis-example.html
if __name__ == '__main__':
import sys
a = QApplication(sys.argv)
x = ['2018-07-01 13:06:38','2018-07-01 12:46:38','2018-07-01 12:36:38','2018-07-01 12:26:38','2018-07-01 12:16:38','2018-07-01 12:06:38','2018-07-01 11:56:38','2018-07-01 11:46:38','2018-07-01 11:36:38','2018-07-01 11:26:38','2018-07-01 10:56:38','2018-07-02 00:46:38','2018-07-01 10:36:38']
y = [23.5,20.8,28.0,28.1,27.8,27.3,27.2,25.7,24.7,25.0,24.9]
#Chart Type
series = QLineseries()
for t,val in zip(x,y):
series.append(qdateTime.fromString(t,"yyyy-MM-dd hh:mm:ss").toMSecsSinceEpoch(),val)
# Create Chart and set General Chart setting
chart = QChart()
chart.addSeries(series)
chart.setTitle("Temperature records in celcius")
chart.setanimationOptions(QChart.SeriesAnimations)
# X Axis Settings
axisX = qdateTimeAxis()
axisX.setTickCount(10)
axisX.setformat( "yyyy-MM-dd hh:mm:ss") #https://doc.qt.io/qt-5/qdatetime.html#toString-2
axisX.setTitleText("Day")
chart.addAxis(axisX,Qt.AlignBottom)
series.attachAxis(axisX)
# Y Axis Settings
axisY = QValueAxis()
axisY.setLabelFormat("%i")
axisY.setTitleText("Temperature C")
chart.addAxis(axisY,Qt.AlignLeft)
series.attachAxis(axisY)
# Create a QChartView object with QChart as a parameter. This way we don't need to create the QGraphicsView scene ourselves. We also set the Antialiasing on to have the rendered lines look nicer.
chartView = QChartView(chart)
chartView.setRenderHint(QPainter.Antialiasing)
def update():
series.append(qdateTime.fromString('2018-07-02 00:00:38',75.0)
chartView.setRenderHint(QPainter.Antialiasing)
chart.axisY(series).setRange(0,100)
chart.legend().setVisible(True)
chart.legend().setalignment(Qt.AlignBottom)
window = QMainWindow()
sa = QScrollArea()
sa.setGeometry(QRect(0,1280,480))
sa.setWidget(chartView)
window.setCentralWidget(chartView)
window.resize(1280,480)
window.show()
timer = QTimer()
timer.timeout.connect(update)
timer.start(5000)
print(series)
a.exec_()