You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

162 lines
5.6 KiB
Python

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import QSize, Qt
from PyQt5.Qt import *
from PyQt5.QtGui import QPixmap, QIcon
from PyQt5.QtWidgets import QHBoxLayout, QAbstractItemView, QTableView, QVBoxLayout, QSplitter, \
QApplication, QGroupBox, QLabel, QGridLayout, QLineEdit, QComboBox, QTextEdit, QCheckBox,QVBoxLayout,QListView, QMainWindow
from PyQt5.QtWidgets import QListWidget, QStackedWidget, QFrame
from PyQt5.QtWidgets import QListWidgetItem, QSizePolicy
from PyQt5.QtWidgets import QWidget, QSpacerItem, QHeaderView
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtWebEngineWidgets import QWebEnginePage
from utils.DBModels.ProtocolModel import InfluxMem
from utils import Globals
from model.HistoryDBModel.HistoryDBManage import HistoryDBManage
import datetime
import pyecharts.options as opts
from pyecharts.charts import Line
import numpy as np
from Static import static
class HistoryTrend(object):
def __init__(self, view):
self.line = Line(init_opts= opts.InitOpts(width = str(view.size().width() - 20) + 'px', height = str(view.size().height() - 30) + 'px'))
self.setOpts()
def addXAxis(self, xAxis):
self.line.add_xaxis(xaxis_data=xAxis)
def addYAxis(self, lineName, yAxis):
self.line.add_yaxis(
series_name=lineName,
y_axis=yAxis,
markpoint_opts=opts.MarkPointOpts(
data=[
opts.MarkPointItem(type_="max", name="最大值"),
opts.MarkPointItem(type_="min", name="最小值"),
]
),
markline_opts=opts.MarkLineOpts(
data=[opts.MarkLineItem(type_="average", name="平均值")]
),
)
def setOpts(self):
self.line.set_global_opts(
title_opts=opts.TitleOpts(title="趋势图"),
tooltip_opts=opts.TooltipOpts(trigger="axis"),
toolbox_opts=opts.ToolboxOpts(is_show=True),
xaxis_opts=opts.AxisOpts(type_="category", boundary_gap=False),
datazoom_opts=[
opts.DataZoomOpts(xaxis_index=0, range_start=0, range_end=100),
],
)
@property
def html(self):
return self.line.render_embed()
class TrendWidgets(QWidget):
def __init__(self, parent=None):
super(TrendWidgets, self).__init__(parent)
# self.setAttribute(Qt.WA_StyledBackground, True) # 移除无效属性
self.gridLayout = QtWidgets.QGridLayout(self)
self.gridLayout.setObjectName("gridLayout")
# 变量列表
self.varListWidget = QListWidget()
self.varListWidget.setObjectName("varListWidget")
self.varListWidget.itemDoubleClicked.connect(self.showVarTrend)
self.gridLayout.addWidget(self.varListWidget, 0, 0, 15, 4)
# 趋势图WebView
self.trendWebView = QWebEngineView()
self.trendWebView.setObjectName("trendWebView")
self.gridLayout.addWidget(self.trendWebView, 0, 4, 15, 22)
self.gridLayout.setSpacing(10)
self.gridLayout.setContentsMargins(20, 20, 20, 20)
Globals.setValue('HistoryWidget', self)
def exchangeProject(self):
self.historyDB = Globals.getValue('historyDBManage')
self.refreshVarList()
def deleteMem(self):
pass # 历史mem相关功能已废弃
def itemCheckstate(self):
pass # 历史mem相关功能已废弃
@QtCore.pyqtSlot(str)
def on_lineEdit_textChanged(self, text):
pass # 历史mem相关功能已废弃
def listContext(self, position):
pass # 历史mem相关功能已废弃
def delVarRecard(self):
pass # 历史mem相关功能已废弃
def getMems(self):
pass # 历史mem相关功能已废弃
def refreshList(self, index):
pass # 历史mem相关功能已废弃
def createHtml(self, varNames):
pass # 历史mem相关功能已废弃
def refreshVarList(self):
# 显示进度条
self.varListWidget.clear()
varNames = []
try:
sql = f'SELECT DISTINCT("varName") FROM "{self.historyDB.table}"'
df = self.historyDB.client.query(sql, mode="pandas")
import pandas as pd
if isinstance(df, pd.DataFrame) and 'varName' in df.columns:
varNames = df['varName'].tolist()
except Exception as e:
print(f"获取变量名失败: {e}")
varNames = []
for name in varNames:
item = QListWidgetItem(str(name))
self.varListWidget.addItem(item)
def showVarTrend(self, item):
varName = item.text()
# 查询该变量历史数据
data, timeList = self.historyDB.queryVarHistory(varName)
if not data or not timeList:
self.trendWebView.setHtml('<h3>无历史数据</h3>')
return
# 构建趋势图
line = Line(init_opts=opts.InitOpts(width='900px', height='500px'))
line.add_xaxis(timeList)
line.add_yaxis(varName, data)
line.set_global_opts(
title_opts=opts.TitleOpts(title=f"{varName} 历史趋势"),
tooltip_opts=opts.TooltipOpts(trigger="axis"),
xaxis_opts=opts.AxisOpts(type_="category", boundary_gap=False),
datazoom_opts=[opts.DataZoomOpts(xaxis_index=0, range_start=0, range_end=100)],
)
html = line.render_embed()
page = QWebEnginePage(self.trendWebView)
page.setHtml('<meta charset="utf-8">\n<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">\n' + html)
self.trendWebView.setPage(page)
self.trendWebView.reload()