如何向图表(QtChart)添加数据?

How to add data to the chart (QtChart)?

一个任务:

  1. 在小部件 Frame(PyQt5) 上用 GUI(PyQT5) 绘制图形(这很重要).
  2. 实时图表每秒更新 1 次。 X轴为时间轴。

发生了什么:

  1. 在小部件上绘制图表(仅轴和网格)

什么不起作用:

  1. 图表中未添加新点
  2. 时间轴,尽管设置了,但不显示时间。

代码:

from random import uniform
import sys
from PyQt5 import uic, QtChart, QtCore
from PyQt5.QtCore import QTimer
from PyQt5.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QWidget
from PyQt5.QtChart import QChart, QChartView, QLineSeries

class Window(QMainWindow):
    def __init__(self):
        super().__init__()
        uic.loadUi('QtChart.ui', self)

        # Create chart
        chart = self.create_linechart()

        # Create PyQt widget for painting
        chart_widget = self.chart
        my_layout = QVBoxLayout(chart_widget)
        self.chart.setLayout(my_layout)
        my_layout.addWidget(chart)

        # Setting the axis to display dates
        self.axis_x = QtChart.QDateTimeAxis()
        self.axis_x.setTickCount(100)
        self.axis_x.setFormat("h:mm")
        self.axis_x.setTitleText("Date")

        # Timer for update data 1 time per second
        self.timer_update_events = QTimer()
        self.timer_update_events.timeout.connect(self.update_data)
        self.timer_update_events.start(1000)

        self.show()

    def update_data(self):
        time_now = QtCore.QDateTime.currentDateTime()
        y = uniform(0, 1)
        self.series.append(time_now.toMSecsSinceEpoch(), y)

    def create_linechart(self):
        self.series = QLineSeries()
        chart = QChart()
        chart.addSeries(self.series)
        chart.createDefaultAxes()
        chartview = QChartView(chart)
        return chartview

if __name__ == "__main__":
    App = QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(App.exec_())

如何添加新的点并在X轴上设置时间?

加载的QtChart.ui接口文件(其代码...创建*.txt文件并重命名QtChart.ui):

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>1007</width>
    <height>789</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <widget class="QFrame" name="chart">
    <property name="geometry">
     <rect>
      <x>50</x>
      <y>30</y>
      <width>901</width>
      <height>681</height>
     </rect>
    </property>
    <property name="styleSheet">
     <string notr="true">background-color: rgb(255, 255, 255);</string>
    </property>
    <property name="frameShape">
     <enum>QFrame::StyledPanel</enum>
    </property>
    <property name="frameShadow">
     <enum>QFrame::Raised</enum>
    </property>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>1007</width>
     <height>21</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>

创建 QDateTimeAxis 并不意味着它会被添加到图表中而不是附加到系列中。另一方面,坐标轴不会自动计算可见范围。

class Window(QMainWindow):
    def __init__(self):
        super().__init__()
        uic.loadUi("QtChart.ui", self)

        # Create chart
        self.chartview = self.create_chart()
        self.series = self.create_linechart()

        self.chartview.chart().addSeries(self.series)

        # Setting the axis to display dates
        self.axis_x = QtChart.QDateTimeAxis()
        self.axis_x.setFormat("h:mm")
        self.axis_x.setTitleText("Date")
        self.chartview.chart().addAxis(self.axis_x, QtCore.Qt.AlignBottom)
        self.series.attachAxis(self.axis_x)

        self.axis_y = QtChart.QValueAxis()
        self.chartview.chart().addAxis(self.axis_y, QtCore.Qt.AlignLeft)
        self.series.attachAxis(self.axis_y)

        # Create PyQt widget for painting
        my_layout = QVBoxLayout(self.chart)
        my_layout.addWidget(self.chartview)

        # Timer for update data 1 time per second
        self.timer_update_events = QTimer()
        self.timer_update_events.timeout.connect(self.update_data)
        self.timer_update_events.start(1000)

        QTimer.singleShot(0, self.update_data)

    def update_data(self):
        time_now = QtCore.QDateTime.currentDateTime()
        if self.series.count() == 0:
            self.axis_x.setMin(time_now)
        y = uniform(0, 1)
        self.series.append(time_now.toMSecsSinceEpoch(), y)
        self.axis_x.setMax(time_now)

    def create_chart(self):
        chart = QChart()
        chartview = QChartView(chart)
        return chartview

    def create_linechart(self):
        series = QtChart.QLineSeries()
        return series