1. 使用pyqt5搭建框架
先看主程序,我在ui界面中预留了一个frame,用来加载 pyecharts 生成的图形。
import os
import sys
from PyQt5.QtWidgets import *
# from PyQt5.QtGui import *
from PyQt5.QtCore import *
from TSFrame import Ui_MainWindow
from TestingScript_Function import deal_scriptfile
from PyQt5.QtWebEngineWidgets import QWebEngineView
class Example(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(Example, self).__init__(parent)
self.setupUi(self)
self.cwd = os.getcwd()
self.pushButton.clicked.connect(self.choose_file)
self.pushButton_2.clicked.connect(self.exec_script)
self.hboxLayout = QHBoxLayout(self.frame)
'''新建QWebEngineView框架,用来加载网页文件'''
self.myHtml = QWebEngineView()
self.hboxLayout.addWidget(self.myHtml)
def choose_file(self):
filename_choosed, filetype = QFileDialog.getOpenFileName(
self,"选择数据文件",self.cwd,"All Files (*);;Text Files (*.txt)")
self.plainTextEdit_2.setPlainText(filename_choosed)
self.cwd = os.path.split(filename_choosed)[0]
def exec_script(self):
filepath = fr'{self.plainTextEdit_2.toPlainText()}'
if filepath == '':
self.reply = QMessageBox(QMessageBox.Question, "拜托", "数据文件选了吗?")
self.reply.addButton('确定', QMessageBox.YesRole)
self.reply.show()
else:
'''执行函数生成echarts的html'''
out = deal_scriptfile(filepath)
html_file = out[1]
self.myHtml.load(QUrl.fromLocalFile(os.path.join(os.getcwd(), html_file)))
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
ex.show()
sys.exit(app.exec_())
2. 使用pycharts创建可视化图形的html文件
下面是配置echarts图形的函数
配置项:https://pyecharts.org/#/zh-cn/intro
示例:https://gallery.pyecharts.org/#/Line/README
import os
import pandas as pd
import pyecharts
import pyecharts.options as opts
from pyecharts.charts import Line
from pyecharts.globals import CurrentConfig
# print(CurrentConfig.ONLINE_HOST)
CurrentConfig.ONLINE_HOST = '' # 将echarts.min.js放在同一目录下
def deal_scriptfile(filepath):
'''
分析数据
......
'''
html_file = pyecharts_html(OutDict)
return html_file
def pyecharts_html(OutDict):
'''OutDict是处理数据整合的字典,'''
line = Line(init_opts=opts.InitOpts(width='1200px', height='550px'))
line.add_xaxis(xaxis_data=OutDict['Time'])
for key in OutDict.keys():
if key != 'Time':
line.add_yaxis(
series_name=key,
y_axis=OutDict[key],
is_symbol_show=False,
yaxis_index=0
)
line.set_global_opts(
title_opts=opts.TitleOpts(title="主标题", subtitle="副标题"),
tooltip_opts=opts.TooltipOpts(trigger="axis", axis_pointer_type='cross'),
toolbox_opts=opts.ToolboxOpts(is_show=True, pos_left='14%' ,feature=opts.ToolBoxFeatureOpts(
save_as_image=opts.ToolBoxFeatureSaveAsImageOpts(is_show=False),
data_view=opts.ToolBoxFeatureDataViewOpts(is_show=False),
data_zoom=opts.ToolBoxFeatureDataZoomOpts(is_show=False),
magic_type=opts.ToolBoxFeatureMagicTypeOpts(is_show=False))),
xaxis_opts=opts.AxisOpts(type_="value", boundary_gap=False),
legend_opts=opts.LegendOpts(orient='vertical', pos_right=0, pos_top=60),
datazoom_opts=[
opts.DataZoomOpts(xaxis_index=0, start_value=0, end_value=100, range_start=0, range_end=100),
opts.DataZoomOpts(type_="inside", yaxis_index=0, start_value=0, end_value=100, range_start=0, range_end=100),
]
)
html_file = 'render.html'
line.render(html_file)
return html_file
3. echarts.js 配置
默认不配置 CurrentConfig.ONLINE_HOST 参数时,html 引用的是远程的js文件,如果不能访问该网站,那么echarts也加载不出来,所以需要将echarts.min.js 文件下载到本地,并将CurrentConfig.ONLINE_HOST 配置成js文件所在的文件夹。
js下载地址:https://assets.pyecharts.org/assets/v5/echarts.min.js