我在 QTDesigner 中设计了一个 GUI,我想在其中包含一个使用 PyQT5 的 QChart 的圆环图。我已经实现了让图表本身可见并隐藏图例。但是,我不知道如何使图表适合定义的 Chartview 区域的整个区域。如图所示,它非常小。
以下是我的代码的相关片段:
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtChart import QChart, QChartView, QPieSeries
from PyQt5.QtGui import QColor
import csv
from operator import itemgetter
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1118, 702)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.tabWidget = QtWidgets.QTabWidget(self.centralwidget)
self.tabWidget.setGeometry(QtCore.QRect(0, 20, 1091, 611))
self.tabWidget.setObjectName("tabWidget")
# removed a lot of other graphical elements for readability
self.t_type_pressure = QtWidgets.QWidget() #create a tab which contains a group box
self.t_type_pressure.setObjectName("t_type_pressure")
self.groupBox = QtWidgets.QGroupBox(self.t_type_pressure)
self.groupBox.setGeometry(QtCore.QRect(20, 60, 511, 311))
self.groupBox.setObjectName("groupBox")
self.gv_all_types = QChartView(self.groupBox) #add a QChartView to the group box
self.gv_all_types.setGeometry(QtCore.QRect(0, 20, 100, 150)) #set the size of the QChartView container which holds the chart
self.gv_all_types.setObjectName("gv_all_types")
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
# removed the connections between buttons and functions, one of them called the connectCSVDatabase
def connectCSVDatabase(self):
# do connection to database here and get data
# ... get data from CSV file
# initialize all data containers in GUI
self.series_all_types = QPieSeries()
self.series_all_types.setHoleSize(0.45)
self.series_all_types.setPieSize(1)
self.series_all_types.append('A', 1).setColor(QColor(0,148,255))
self.series_all_types.append('B', 1).setColor(QColor(255,144,0))
self.series_all_types.append('C', 1).setColor(QColor(255,8,0))
self.series_all_types.hide()
self.chart_all_types = self.gv_all_types.chart()
self.chart_all_types.addSeries(self.series_all_types)
self.chart_all_types.setBackgroundRoundness(0)
self.chart_all_types.layout().setContentsMargins(1, 1, 1, 1)
self.update_shown_data()
def update_shown_data(self):
# clear the existing series and add the new ones
self.series_all_types.clear()
self.series_all_types.append('A', 10).setColor(QColor(0,148,255))
self.series_all_types.append('B', 20).setColor(QColor(255,144,0))
self.series_all_types.append('C', 30).setColor(QColor(255,8,0))
self.chart_all_types.addSeries(self.series_all_types)
使用上面的代码,我的容器在 GUI 中清晰可见,但图表本身非常小(参见包含的图片)。我希望有人可以帮助我!