基於PyQt5制作一個數據圖表生成器

我的需求:手動配置X軸、Y軸、圖表標題等參數自動通過Pyecharts模塊生成可視化的html數據圖表,並將瀏覽器圖表展示到UI界面上。

file

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

file

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

file

導入 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數據圖表生成的資料請關註WalkonNet其它相關文章!

推薦閱讀: