如何将数据从QTableWidget绘制到QChart

时间:2019-07-04 17:23:35

标签: python qtablewidget pyside2 qtcharts

我正在研究数据分析软件,该软件从远程数据库中获取数据并将其放入 QTableWidget 中。我怎样才能有效地从表中获取这些数据并将其放入 QChart

我已经看到,如果我一直在使用 QTableView ,可以使用模型来完成,但是据我了解,使用 QTableView 会更加复杂在我的情况下。

from PySide2.QtWidgets import *
from PySide2.QtGui import *
from PySide2.QtCore import *
from PySide2.QtCharts import *
import sys
import random


class DateTimeDelegate(QStyledItemDelegate):
    def initStyleOption(self, option, index):
        super(DateTimeDelegate, self).initStyleOption(option, index)
        value = index.data()
        option.text = 
QDateTime.fromMSecsSinceEpoch(value).toString("dd.MM.yyyy")


class MainWindow(QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()

        self.setGeometry(0, 0, 1280, 400)
        self.chart_table()

        self.populate()
    def chart_table(self):
        self.table = QTableWidget(0, 2)
        delegate = DateTimeDelegate(self.table)
        self.table.setItemDelegateForColumn(0, delegate)
        chart = QtCharts.QChart()
        self.chartView = QtCharts.QChartView(chart)
        self.chartView.setFixedSize(600, 430)

        splitter = QSplitter(self)
        splitter.addWidget(self.table)
        splitter.addWidget(self.chartView)

        self.setCentralWidget(splitter)

        series = QtCharts.QLineSeries(name='Odoslané')
        mapper = QtCharts.QVXYModelMapper(xColumn=0, yColumn=2)
        mapper.setModel(self.table.model())
        mapper.setSeries(series)
        chart.addSeries(mapper.series())

        self.axis_X = QtCharts.QDateTimeAxis()
        self.axis_X.setFormat("MMM yyyy")
        self.axis_Y = QtCharts.QValueAxis()

        chart.setAxisX(self.axis_X, series)
        chart.setAxisY(self.axis_Y, series)
        self.axis_Y.setRange(0, 0)
        self.axis_Y.setLabelFormat('%.0f')
        self.axis_X.setRange(QDate(2017, 10, 1), QDate.currentDate())

        chart.setTitle('Chart')

    def addRow(self, dt, value):
        self.table.insertRow(0)

        for col, v in enumerate((dt.toMSecsSinceEpoch(), value)):
            it = QTableWidgetItem()
            it.setData(Qt.DisplayRole, dt.toMSecsSinceEpoch())
            self.table.setItem(0, 0, it)
        t_m, t_M = self.axis_X.min(), self.axis_X.max()
        t_m = min(t_m, dt)
        t_M = max(t_M, dt)

        m, M = self.axis_Y.min(), self.axis_Y.max()
        m = min(m, value)
        M = max(M, value)

在这种方法中,当我从数据库中获取数据时,我会模拟用数据填充表。

def populate(self):
    for i in range(4):
        count=random.randint(1,40)
        value_str = QDate.currentDate().addDays(count).toString('dd.MM.yyyy')

        dt = QDateTime.fromString(value_str, "dd.MM.yyyy")

        sent = QTableWidgetItem(str(count))
        value = int(sent.text())

        self.addRow(dt, value)
        self.table.setItem(0, 1, sent)

和应用运行功能-

def main():
    app = QApplication(sys.argv)
    gui = MainWindow()
    gui.show()
    sys.exit(app.exec_())

main()

1 个答案:

答案 0 :(得分:0)

在QChartView中显示QTableWidget数据的最简单方法是使用QVXYModelMapper,它将QTableWidget的模型与QLineSerie相关联。但是为此,存储在QTableWidget中的数据不应为字符串,而应为整数,因此您不应使用toString()将QDateTime转换为字符串,而应使用toMSecsSinceEpoch()转换为整数,并在QTableWidget中将其显示为日期时间。应该使用委托。

在下面的示例中,addRow方法允许向行添加(QDateTime,value),这将重新计算每个轴的范围。

import random
from functools import partial
from PySide2 import QtCore, QtGui, QtWidgets
from PySide2.QtCharts import QtCharts


class DateTimeDelegate(QtWidgets.QStyledItemDelegate):
    def initStyleOption(self, option, index):
        super(DateTimeDelegate, self).initStyleOption(option, index)
        value = index.data()
        option.text = QtCore.QDateTime.fromMSecsSinceEpoch(value).toString("dd.MM.yyyy")


class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)

        self.m_tablewidget = QtWidgets.QTableWidget(0, 2)
        delegate = DateTimeDelegate(self.m_tablewidget)
        self.m_tablewidget.setItemDelegateForColumn(0, delegate)
        self.m_chartview = QtCharts.QChartView()
        self.m_chartview.chart().setTheme(QtCharts.QChart.ChartThemeQt)
        self.m_chartview.setMinimumWidth(400)

        self.m_series = QtCharts.QLineSeries(name="Time-Value")
        self.m_mapper = QtCharts.QVXYModelMapper(self, xColumn=0, yColumn=1)
        self.m_mapper.setModel(self.m_tablewidget.model())
        self.m_mapper.setSeries(self.m_series)

        self.m_chartview.chart().addSeries(self.m_mapper.series())

        splitter = QtWidgets.QSplitter(self)
        splitter.addWidget(self.m_tablewidget)
        splitter.addWidget(self.m_chartview)
        self.setCentralWidget(splitter)

        self.m_time_axis = QtCharts.QDateTimeAxis()
        self.m_time_axis.setFormat("dd.MM.yyyy")
        self.m_value_axis = QtCharts.QValueAxis()

        self.m_chartview.chart().setAxisX(self.m_time_axis, self.m_series)
        self.m_chartview.chart().setAxisY(self.m_value_axis, self.m_series)

        self.m_value_axis.setRange(0, 0)
        self.m_time_axis.setRange(
            QtCore.QDateTime.currentDateTime(),
            QtCore.QDateTime.currentDateTime().addDays(1),
        )

    def addRow(self, dt, value):
        row = self.m_tablewidget.rowCount()
        self.m_tablewidget.insertRow(row)
        for col, v in enumerate((dt.toMSecsSinceEpoch(), value)):
            it = QtWidgets.QTableWidgetItem()
            it.setData(QtCore.Qt.DisplayRole, v)
            self.m_tablewidget.setItem(row, col, it)
        t_m, t_M = self.m_time_axis.min(), self.m_time_axis.max()
        t_m = min(t_m, dt)
        t_M = max(t_M, dt)

        m, M = self.m_value_axis.min(), self.m_value_axis.max()
        m = min(m, value)
        M = max(M, value)

        self.m_time_axis.setRange(t_m, t_M)
        self.m_value_axis.setRange(m, M)


counter = 0


def onTimeout(w):
    # Emulate the data
    global counter
    dt = QtCore.QDateTime.currentDateTime().addDays(counter)
    value = random.uniform(-100, 100)
    w.addRow(dt, value)

    counter += 1


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)

    w = MainWindow()
    w.resize(640, 480)
    w.show()

    wrapper = partial(onTimeout, w)
    timer = QtCore.QTimer(timeout=wrapper, interval=1000)
    timer.start()

    sys.exit(app.exec_())

enter image description here


更新

您不必在populate方法中创建任何QTableWidget。我已经更正了您的逻辑,以便将其添加到QTableWidget的顶部,还更正了范围的计算。

import sys
import random
from PySide2.QtCore import *
from PySide2.QtWidgets import *
from PySide2.QtCharts import QtCharts


class DateTimeDelegate(QStyledItemDelegate):
    def initStyleOption(self, option, index):
        super(DateTimeDelegate, self).initStyleOption(option, index)
        value = index.data()
        option.text = QDateTime.fromMSecsSinceEpoch(value).toString("dd.MM.yyyy")


class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)

        self.setGeometry(0, 0, 1280, 400)
        self.chart_table()

        self.populate()

    def chart_table(self):
        self.table = QTableWidget(0, 2)
        delegate = DateTimeDelegate(self.table)
        self.table.setItemDelegateForColumn(0, delegate)
        chart = QtCharts.QChart()
        self.chartView = QtCharts.QChartView(chart)
        self.chartView.setFixedSize(600, 430)

        splitter = QSplitter(self)
        splitter.addWidget(self.table)
        splitter.addWidget(self.chartView)
        self.setCentralWidget(splitter)

        series = QtCharts.QLineSeries(name="Odoslané")
        mapper = QtCharts.QVXYModelMapper(self, xColumn=0, yColumn=1)
        mapper.setModel(self.table.model())
        mapper.setSeries(series)
        chart.addSeries(mapper.series())

        self.axis_X = QtCharts.QDateTimeAxis()
        self.axis_X.setFormat("MMM yyyy")
        self.axis_Y = QtCharts.QValueAxis()

        chart.setAxisX(self.axis_X, series)
        chart.setAxisY(self.axis_Y, series)
        self.axis_Y.setRange(0, 0)
        self.axis_Y.setLabelFormat("%.0f")
        chart.setTitle("Chart")

    def addRow(self, dt, value):
        self.table.insertRow(0)
        for col, v in enumerate((dt.toMSecsSinceEpoch(), value)):
            it = QTableWidgetItem()
            it.setData(Qt.DisplayRole, v)
            self.table.setItem(0, col, it)

        if self.table.rowCount() == 1:
            self.axis_X.setRange(dt, dt.addDays(1))
            self.axis_Y.setRange(v, v)

        else:
            t_m, t_M = self.axis_X.min(), self.axis_X.max()
            t_m = min(t_m, dt)
            t_M = max(t_M, dt)

            m, M = self.axis_Y.min(), self.axis_Y.max()
            m = min(m, value)
            M = max(M, value)

            self.axis_X.setRange(t_m, t_M)
            self.axis_Y.setRange(m, M)

    def populate(self):
        for i in range(100):
            # simulate filling table with data as I get them from database.
            value = random.uniform(1, 40)
            fake_dt_str = QDate.currentDate().addDays(i).toString("dd.MM.yyyy")
            fake_value_str = str(random.uniform(0, 2))

            # Convert simulated data
            dt = QDateTime.fromString(fake_dt_str, "dd.MM.yyyy")
            value = float(fake_value_str)
            self.addRow(dt, value)

def main():
    app = QApplication(sys.argv)
    gui = MainWindow()
    gui.show()
    sys.exit(app.exec_())


main()