更新PYQT5中的图表数据

oo

我的上一个问题将QChartView插入ui

def draw_chart(self, obj, chart_view, replace=0):
    obj.setContentsMargins(0, 0, 0, 0)
    lay = QtWidgets.QHBoxLayout(obj)
    lay.setContentsMargins(0, 0, 0, 0)
    if replace:
        # lay.replaceWidget(chart_view)
        pass
    else:
        lay.addWidget(chart_view)

我这样调用此函数:

    cv1 = self.candle_chart(self.p)
    self.draw_chart(self.chart1, cv1, 0)

candle_chart返回一个QtChart.QChartView对象,self.chart1对象名称为Qwidget

第一次工作正常,但是当我想更新图表时却报错:

QLayout: Attempting to add QLayout "" to QWidget "chart1", which already has a layout

我正在尝试通过单击按钮更改图表数据

完整的代码:

class Test(base_3, form_3):
    def __init__(self):
        super(base_3, self).__init__()
        self.setupUi(self)

        self.p = [(1, 7380, 7520, 7380, 7510, 7324),
              (2, 7520, 7580, 7410, 7440, 7372),
              (3, 7440, 7650, 7310, 7520, 7434),
              (4, 7450, 7640, 7450, 7550, 7480),
              (5, 7510, 7590, 7460, 7490, 7502),
              (6, 7500, 7590, 7480, 7560, 7512),
              (7, 7560, 7830, 7540, 7800, 7584)]

        self.next.clicked.connect(self.more)

        self.lmt = 3
        self.st = 0

        cv1 = self.candle_chart(self.p[self.st:self.lmt])
        self.draw_chart(self.chart1, cv1, 0)

    def more(self):
        cv1 = self.candle_chart(self.p[self.st:self.lmt])
        self.draw_chart(self.chart1, cv1, 1)

        self.st += 1
        self.lmt += 1

    def draw_chart(self, label, chart_view, replace=0):
        label.setContentsMargins(0, 0, 0, 0)
        lay = QtWidgets.QHBoxLayout(label)
        lay.setContentsMargins(0, 0, 0, 0)
        if replace:
            # lay.replaceWidget(chart_view)
            pass
        else:
            lay.addWidget(chart_view)


    def candle_chart(self, data):
        series = QtChart.QCandlestickSeries()
        series.setDecreasingColor(QtCore.Qt.red)
        series.setIncreasingColor(QtCore.Qt.green)

        tm = []
        i = 0
        for d, o, h, l, c, v in data:
            # x = x[1]
            # series.append(QtChart.QCandlestickSet(x["open"], x["high"], x["low"], x["close"]))
            series.append(QtChart.QCandlestickSet(o, h, l, c))
            tm.append(str(i))
            i += 1

        chart = QtChart.QChart()
        chart.addSeries(series)
        chart.createDefaultAxes()
        chart.legend().hide()
        chart.axisX(series).setCategories(tm)
        chart_view = QtChart.QChartView(chart)
        return chart_view

用户界面代码:

<?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>1381</width>
    <height>680</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <widget class="QPushButton" name="next">
    <property name="geometry">
     <rect>
      <x>490</x>
      <y>540</y>
      <width>88</width>
      <height>33</height>
     </rect>
    </property>
    <property name="text">
     <string>Next</string>
    </property>
   </widget>
   <widget class="QWidget" name="chart1" native="true">
    <property name="enabled">
     <bool>true</bool>
    </property>
    <property name="geometry">
     <rect>
      <x>0</x>
      <y>0</y>
      <width>460</width>
      <height>250</height>
     </rect>
    </property>
   </widget>
   <widget class="QWidget" name="chart2" native="true">
    <property name="geometry">
     <rect>
      <x>460</x>
      <y>0</y>
      <width>460</width>
      <height>250</height>
     </rect>
    </property>
   </widget>
   <widget class="QWidget" name="chart3" native="true">
    <property name="geometry">
     <rect>
      <x>920</x>
      <y>0</y>
      <width>460</width>
      <height>250</height>
     </rect>
    </property>
   </widget>
   <widget class="QWidget" name="chart5" native="true">
    <property name="geometry">
     <rect>
      <x>450</x>
      <y>250</y>
      <width>450</width>
      <height>250</height>
     </rect>
    </property>
   </widget>
   <widget class="QWidget" name="chart6" native="true">
    <property name="geometry">
     <rect>
      <x>900</x>
      <y>250</y>
      <width>450</width>
      <height>250</height>
     </rect>
    </property>
   </widget>
   <widget class="QWidget" name="chart4" native="true">
    <property name="enabled">
     <bool>true</bool>
    </property>
    <property name="geometry">
     <rect>
      <x>0</x>
      <y>250</y>
      <width>450</width>
      <height>250</height>
     </rect>
    </property>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>1381</width>
     <height>25</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>
永乐

您不必替换小部件,最好重用,如下所示:

import sys

from PyQt5 import QtCore, QtWidgets, uic, QtChart

# load both ui file
uifile_1 = "UI/main.ui"
form_1, base_1 = uic.loadUiType(uifile_1)


class Example(base_1, form_1):
    def __init__(self):
        super(base_1, self).__init__()
        self.setupUi(self)

        self.series = QtChart.QCandlestickSeries()
        self.series.setDecreasingColor(QtCore.Qt.red)
        self.series.setIncreasingColor(QtCore.Qt.green)

        self.ma5 = QtChart.QLineSeries()  # 5-days average data line

        self.chart = QtChart.QChart()
        self.chart.addSeries(self.series)  # candle
        self.chart.addSeries(self.ma5)  # ma5 line
        self.chart.createDefaultAxes()
        self.chart.legend().hide()
        self.chart.axisX(self.ma5).setVisible(False)

        self.chart.setAnimationOptions(QtChart.QChart.SeriesAnimations)

        self.chartview = QtChart.QChartView(self.chart)

        self.chart_container.setContentsMargins(0, 0, 0, 0)
        lay = QtWidgets.QHBoxLayout(self.chart_container)
        lay.setContentsMargins(0, 0, 0, 0)
        lay.addWidget(self.chartview)

        self.update_data()

        timer = QtCore.QTimer(self, interval=1000, timeout=self.update_data)
        timer.start()

    def update_data(self):

        import random

        data = ((i, *random.sample(range(7000, 8000), 5)) for i in range(7))

        self.series.clear()
        self.ma5.clear()

        tm = []  # stores str type data
        x_min, x_max, y_min, y_max = (
            float("inf"),
            -float("inf"),
            float("inf"),
            -float("inf"),
        )
        # in a loop,  series and ma5 append corresponding data
        for num, o, h, l, c, m in data:
            self.series.append(QtChart.QCandlestickSet(o, h, l, c))
            self.ma5.append(QtCore.QPointF(num, m))
            tm.append(str(num))
            x_min = min(x_min, num)
            x_max = max(x_max, num)
            y_min = min(y_min, m, o, h, l, c)
            y_max = max(y_max, m, o, h, l, c)

        self.chart.axisY(self.series).setMin(y_min)
        self.chart.axisY(self.series).setMax(y_max)
        self.chart.axisX(self.series).setCategories(tm)

        self.chart.axisX(self.ma5).setMin(x_min)
        self.chart.axisX(self.ma5).setMax(x_max)


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    ex = Example()
    ex.show()
    sys.exit(app.exec_())

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

从pyqt5按钮更新matplotlib图表时出现问题

来自分类Dev

在pyqt5小部件中更新matplotlib

来自分类Dev

在for循环PyQT5的过程中更新QT元素

来自分类Dev

更新pyqt5中的选定列表视图项

来自分类Dev

在pyqt5小部件中更新matplotlib

来自分类Dev

如何在 PyQt5 中更新 QLabel?

来自分类Dev

信号中是否包含数据pyqt5?

来自分类Dev

在 Pyqt5 GUI 中显示来自 Python 文件的数据

来自分类Dev

如何实时更新PyQt5标签?

来自分类Dev

PyQt5 - 用 QThread 更新 Qtablewidget

来自分类Dev

PyQt5:设置QStandardItem的数据

来自分类Dev

标签文字未更新,只有最后一行更新了pyqt5中的标签

来自分类Dev

PyQt5 QSqlTableModel不更新对数据库的更改

来自分类Dev

来自MySQL数据库的数据未显示在PyQt5的QTableWidget中

来自分类Dev

如何从Qlistwidget中选择项目,并更新Pyqt5中的文本框?

来自分类Dev

利用PyQt5中的QtQuick控件

来自分类Dev

在PyQt5中未触发dropEvent

来自分类Dev

在PyQt5中串联多个QSplitter

来自分类Dev

PyQt5中的顺序编程复制

来自分类Dev

在PyQt5中实现画布

来自分类Dev

在PyQt5中打印pdf文档

来自分类Dev

在Windows 7中安装PyQt5

来自分类Dev

QTableWidget中的PyQt5 QComboBox

来自分类Dev

PyQt5中的哪个Gstreamer?

来自分类Dev

PyQt5 中的问号按钮

来自分类Dev

PyQt5 中的访问冲突错误

来自分类Dev

PyQt5 中的 QWebEngineView 和 QWidget

来自分类Dev

PyQT5 中的 GridLayout 和 VerticalLayout

来自分类Dev

如何在PyQt5中的Ui和类之间交换数据?