首頁 > 軟體

基於PyQt5製作一個資料圖表生成器

2022-02-26 13:00:55

我的需求:手動設定X軸、Y軸、圖表標題等引數自動通過Pyecharts模組生成視覺化的html資料圖表,並將瀏覽器圖表展示到UI介面上。

製作出圖表後的效果展示如下:

另外,生成後的圖表結果會使用 html 的形式儲存下來。

匯入 UI 介面相關的 PyQt5 第三方模組庫。

from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *

若是使用PyQt5的版本是5.10.1以上,則需要單獨安裝一下PyQtWebEngine,說是QtWebEngineWidgets模組被新版本移除了。執行一下下面安裝單獨安裝一下PyQtWebEngine就OK了。

pip install PyQtWebEngine

匯入 Web 瀏覽器引擎的支援。

from PyQt5.QtWebEngineWidgets import QWebEngineView

應用其他的相關模組匯入。

import sys  # 應用操作庫
import os

from datetime import datetime
from pyecharts.charts import *  # 圖表設定展示庫
from pyecharts import options  # 圖示引數設定庫

主要的程式碼塊實現過程如下:

class DataVisual(QWidget):
    def __init__(self):
        super(DataVisual, self).__init__()
        self.cwd = os.getcwd()  # 獲取當前路徑
        self.init_ui()

    def init_ui(self):
        self.setWindowTitle('資料圖表生成器')
        self.setWindowIcon(QIcon('資料.ico'))

        self.brower = QWebEngineView()
        self.brower.setMinimumWidth(800)
        self.brower.setMaximumWidth(1400)
        self.brower.setUrl(QUrl(''))

        form = QFormLayout()

        '''折線圖/柱狀圖控制元件開始'''
        self.selected_label = QLabel()
        self.selected_label.setText('設定圖表型別:')
        self.selected_cobox = QComboBox()
        self.selected_cobox.addItems(['Bar(柱狀圖/條形圖)', 'Line(折線/面積圖)', 'Pie(餅圖)'])
        self.selected_cobox.currentIndexChanged.connect(self.selected_cobox_change)

        self.title_label = QLabel()
        self.title_label.setText('設定標題:')
        self.title_text = QLineEdit()
        self.title_text.setPlaceholderText('請輸入圖表標題(最大長度10)')
        self.title_text.setMaxLength(10)

        self.subtitle_label = QLabel()
        self.subtitle_label.setText('設定副標題:')
        self.subtitle_text = QLineEdit()
        self.subtitle_text.setPlaceholderText('請輸入圖表副標題(最大長度20)')
        self.title_text.setMaxLength(20)

        self.data_label_x = QLabel()
        self.data_label_x.setText('設定X軸資料:')
        self.data_text_x = QLineEdit()
        self.data_text_x.setPlaceholderText("湖北, 四川, 重慶, 河北, 雲南")
        self.data_text_x.setMaxLength(1000)

        self.data_label_y1_name = QLabel()
        self.data_label_y1_name.setText('設定Y1軸標籤:')
        self.data_text_y1_name = QLineEdit()
        self.data_text_y1_name.setPlaceholderText("請輸入Y1軸標籤")

        self.data_label_y1 = QLabel()
        self.data_label_y1.setText('設定Y1軸資料:')
        self.data_text_y1 = QLineEdit()
        self.data_text_y1.setPlaceholderText("34500, 3000, 3218, 2890, 50023")
        self.data_text_y1.setMaxLength(1000)

        self.data_label_y2_name = QLabel()
        self.data_label_y2_name.setText('設定Y2軸標籤:')
        self.data_text_y2_name = QLineEdit()
        self.data_text_y2_name.setPlaceholderText("請輸入Y2軸標籤")

        self.data_label_y2 = QLabel()
        self.data_label_y2.setText('設定Y2軸資料:')
        self.data_text_y2 = QLineEdit()
        self.data_text_y2.setPlaceholderText("1200, 100, 300, 130, 1004")
        self.data_text_y2.setMaxLength(1000)
        '''折線圖/柱狀圖控制元件結束'''

        '''餅圖控制元件開始'''
        self.pie_table_label = QLabel()
        self.pie_table_label.setText('設定標題:')
        self.pie_table_label.setVisible(False)
        self.pie_table_text = QLineEdit()
        self.pie_table_text.setPlaceholderText('請輸入標題')
        self.pie_table_text.setVisible(False)

        self.pie_data_label = QLabel()
        self.pie_data_label.setText('設定餅圖資料:')
        self.pie_data_label.setVisible(False)
        self.pie_data_text = QLineEdit()
        self.pie_data_text.setPlaceholderText("華為:35,三星:59")
        self.pie_data_text.setVisible(False)

        self.pie_radius = QLabel()
        self.pie_radius.setText('設定資料顯示比例:')
        self.pie_radius.setVisible(False)
        self.pie_radius_text = QLineEdit()
        self.pie_radius_text.setPlaceholderText('65%')
        self.pie_radius_text.setVisible(False)

        self.pie_radius_cle = QLabel()
        self.pie_radius_cle.setText('設定圓環空心比例:')
        self.pie_radius_cle.setVisible(False)
        self.pie_radius_text_cle = QLineEdit()
        self.pie_radius_text_cle.setPlaceholderText('60%')
        self.pie_radius_text_cle.setVisible(False)

        '''餅圖控制元件結束'''

        '''公共控制元件開始'''
        self.save_dir_text = QLineEdit()
        self.save_dir_text.setPlaceholderText('請選擇圖表儲存地址')
        self.save_dir_text.setReadOnly(True)
        self.save_dir_btn = QPushButton()
        self.save_dir_btn.setText('儲存路徑')
        self.save_dir_btn.clicked.connect(self.save_dir_btn_click)

        self.version = QLabel()
        self.version.setText('<font color="green">公眾號:[Python 集中營]</font>')
        self.generate_btn = QPushButton()
        self.generate_btn.setText('生成圖表')
        self.generate_btn.clicked.connect(self.generate_btn_click)

        '''公共控制元件結束'''

        '''折線圖/柱狀圖佈局開始'''
        form.addRow(self.selected_label, self.selected_cobox)
        form.addRow(self.title_label, self.title_text)
        form.addRow(self.subtitle_label, self.subtitle_text)
        form.addRow(self.data_label_x, self.data_text_x)
        form.addRow(self.data_label_y1_name,self.data_text_y1_name)
        form.addRow(self.data_label_y1, self.data_text_y1)
        form.addRow(self.data_label_y2_name,self.data_text_y2_name)
        form.addRow(self.data_label_y2, self.data_text_y2)
        '''折線圖/柱狀圖佈局結束'''

        '''柱狀圖'''
        form.addRow(self.pie_table_label, self.pie_table_text)
        form.addRow(self.pie_radius, self.pie_radius_text)
        form.addRow(self.pie_radius_cle, self.pie_radius_text_cle)
        form.addRow(self.pie_data_label, self.pie_data_text)
        '''柱狀圖'''

        '''公共佈局'''
        form.addRow(self.save_dir_text, self.save_dir_btn)
        form.addRow(self.version, self.generate_btn)
        '''公共佈局'''

        hbox = QHBoxLayout()
        hbox.addWidget(self.brower)
        hbox.addSpacing(10)
        hbox.addLayout(form)
        self.setLayout(hbox)

    def save_dir_btn_click(self):
        directory = QFileDialog.getExistingDirectory(self, "選取資料夾", self.cwd)
        self.save_dir_text.setText(directory)

    def selected_cobox_change(self):
        selected = self.selected_cobox.currentText().strip()
        print(selected)
        if selected == 'Line(折線/面積圖)' 
                or selected == 'Bar(柱狀圖/條形圖)':

            '''顯示控制元件'''
            self.title_label.setVisible(True)
            self.title_text.setVisible(True)

            self.subtitle_label.setVisible(True)
            self.subtitle_text.setVisible(True)

            self.data_label_x.setVisible(True)
            self.data_text_x.setVisible(True)

            self.data_label_y1.setVisible(True)
            self.data_text_y1.setVisible(True)

            self.data_label_y2.setVisible(True)
            self.data_text_y2.setVisible(True)

            self.data_label_y1_name.setVisible(True)
            self.data_text_y1_name.setVisible(True)

            self.data_label_y2_name.setVisible(True)
            self.data_text_y2_name.setVisible(True)

            '''隱藏控制元件'''
            self.pie_table_label.setVisible(False)
            self.pie_table_text.setVisible(False)

            self.pie_data_label.setVisible(False)
            self.pie_data_text.setVisible(False)

            self.pie_radius.setVisible(False)
            self.pie_radius_text.setVisible(False)

            self.pie_radius_cle.setVisible(False)
            self.pie_radius_text_cle.setVisible(False)
        elif selected == 'Pie(餅圖)':

            '''隱藏控制元件'''
            self.title_label.setVisible(False)
            self.title_text.setVisible(False)

            self.subtitle_label.setVisible(False)
            self.subtitle_text.setVisible(False)

            self.data_label_x.setVisible(False)
            self.data_text_x.setVisible(False)

            self.data_label_y1.setVisible(False)
            self.data_text_y1.setVisible(False)

            self.data_label_y2.setVisible(False)
            self.data_text_y2.setVisible(False)

            self.data_label_y1_name.setVisible(False)
            self.data_text_y1_name.setVisible(False)

            self.data_label_y2_name.setVisible(False)
            self.data_text_y2_name.setVisible(False)

            '''顯示控制元件'''
            self.pie_table_label.setVisible(True)
            self.pie_table_text.setVisible(True)

            self.pie_data_label.setVisible(True)
            self.pie_data_text.setVisible(True)

            self.pie_radius.setVisible(True)
            self.pie_radius_text.setVisible(True)

            self.pie_radius_cle.setVisible(True)
            self.pie_radius_text_cle.setVisible(True)

    def generate_btn_click(self):

        selected = self.selected_cobox.currentText().strip()
        if selected == 'Pie(餅圖)':
            pie_table_text = self.pie_table_text.text().strip()
            print(pie_table_text)
            pie_data_text_list = self.pie_data_text.text().strip().split(",")
            print(pie_data_text_list)
            list_data = []

            for str_ in pie_data_text_list:
                d_list = str_.split(":")
                tuple_ = (d_list[0],) + (int(d_list[1]),)
                list_data.append(tuple_)

            print(list_data)

            pie_radius_text = self.pie_radius_text.text()
            print(pie_radius_text)
            pie_radius_text_cle = self.pie_radius_text_cle.text()
            print(pie_radius_text_cle)

            pie = Pie()

            pie.add(
                series_name=pie_table_text,
                data_pair=list_data,
                radius=[pie_radius_text_cle, pie_radius_text],
                rosetype='radius'
            )

            pie.set_series_opts(label_opts=options.LabelOpts(formatter='{b}:{d}%'))

            pie.set_global_opts(title_opts=options.TitleOpts(title=pie_table_text))

            html_file_path = self.save_dir_text.text().strip() + '/' + datetime.now().strftime("%Y%m%d%H%M%S") + '.html'

            print(html_file_path)

            pie.render(html_file_path)

            self.brower.setUrl(QUrl(html_file_path))

        elif selected == 'Bar(柱狀圖/條形圖)':
            title = self.title_text.text().strip()
            subtitle = self.subtitle_text.text().strip()
            data_x = list(self.data_text_x.text().strip().split(","))
            data_y1 = list(self.data_text_y1.text().strip().split(","))
            data_y2 = list(self.data_text_y2.text().strip().split(','))
            bar = Bar()
            bar.add_xaxis(data_x)
            bar.add_yaxis(self.data_text_y1_name.text().strip(), data_y1)
            bar.add_yaxis(self.data_text_y2_name.text().strip(), data_y2)

            bar.set_global_opts(
                title_opts=options.TitleOpts(title=title, subtitle=subtitle),
                toolbox_opts=options.ToolboxOpts())

            bar.set_series_opts(
                label_opts=options.LabelOpts(is_show=False),
                markpoint_opts=options.MarkPointOpts(data=[
                    options.MarkPointItem(type_='min', name='最小值'),
                    options.MarkPointItem(type_='max', name='最大值')
                ]))
            html_file_path = self.save_dir_text.text().strip() + '/' + datetime.now().strftime("%Y%m%d%H%M%S") + '.html'
            print(html_file_path)
            bar.render(html_file_path)
            self.brower.setUrl(QUrl(html_file_path))
        elif selected == 'Line(折線/面積圖)':
            title = self.title_text.text().strip()
            subtitle = self.subtitle_text.text().strip()
            data_x = list(self.data_text_x.text().strip().split(","))
            data_y1 = list(self.data_text_y1.text().strip().split(","))
            data_y2 = list(self.data_text_y2.text().strip().split(','))
            line = Line()
            # 3. 關聯資料
            line.add_xaxis(data_x)

            line.add_yaxis(self.data_text_y1_name.text().strip(), data_y1,
                           is_smooth=True)

            line.add_yaxis(self.data_text_y2_name.text().strip(), data_y2, markpoint_opts=options.MarkPointOpts(
                data=[options.MarkPointItem(type_='min', name='最小值')]
            ))

            line.set_series_opts(markline_opts=options.MarkLineOpts(
                data=[options.MarkPointItem(type_='average', name='平均值'),
                      options.MarkPointItem(type_='max', name='最大值')]
            ))

            line.set_global_opts(title_opts=options.TitleOpts(title=title, subtitle=subtitle))

            html_file_path = self.save_dir_text.text().strip() + '/' + datetime.now().strftime("%Y%m%d%H%M%S") + '.html'
            print(html_file_path)
            line.render(html_file_path)
            self.brower.setUrl(QUrl(html_file_path))

 以上就是基於PyQt5製作一個資料圖表生成器的詳細內容,更多關於PyQt5資料圖表生成的資料請關注it145.com其它相關文章!


IT145.com E-mail:sddin#qq.com