将我选择的颜色附加到 QPieSeries 的每个切片
Attach colors of my choosing to each slice of QPieSeries
我想让饼图的每一部分都成为我选择的颜色。我需要知道 QPieSlice 的 snytax(如果这是我使用的)以及如何将颜色附加到馅饼的特定切片。例如,我希望 "Auto" 切片为“#00FF00”,"Employment" 切片为“#1A8CFF”,等等
下面是我的馅饼。我尝试过各种方法,例如:
QPieSlice.setBrush(QPieSlice.setColor(QColor('#00FF00')))
但它不起作用,即使它起作用,我也不知道如何将它附加到特定的切片以及将它放在哪里。
series.append("Auto", self.expensesWindow.piechart[0])
series.append("Employment", self.expensesWindow.piechart[1])
series.append("Insurance", self.expensesWindow.piechart[2])
series.append("Household", self.expensesWindow.piechart[3])
series.append("Housing", self.expensesWindow.piechart[4])
series.append("Entertainment", self.expensesWindow.piechart[5])
series.append("Utilities", self.expensesWindow.piechart[6])
series.append("Other", self.expensesWindow.piechart[7])
当您使用 append()
method of QPieSeries
, passing it the name and value it returns its associated QPieSlice
时,您必须使用该元素
from PyQt5 import QtCore, QtGui, QtWidgets, QtChart
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
data = {
"Auto": (10, QtGui.QColor("#00FF00")),
"Employment": (20, QtGui.QColor("#1A8CFF")),
"Insurance": (30, QtGui.QColor("salmon")),
"Household": (40, QtGui.QColor(255, 0, 255)),
"Housing": (40, QtGui.QColor("blue")),
"Entertainment": (30, QtGui.QColor(0, 255, 255)),
"Utilities": (20, QtGui.QColor("#aabbcc")),
"Other": (10, QtGui.QColor("gray")),
}
series = QtChart.QPieSeries()
for name, (value, color) in data.items():
_slice = series.append(name, value)
_slice.setBrush(color)
chart = QtChart.QChart()
chart.addSeries(series)
chart.setTitle("Example for ")
chart.legend().setAlignment(QtCore.Qt.AlignBottom)
chart.legend().setFont(QtGui.QFont("Arial", 7))
chartview = QtChart.QChartView(chart)
chartview.setRenderHint(QtGui.QPainter.Antialiasing)
w = QtWidgets.QMainWindow()
w.setCentralWidget(chartview)
w.resize(640, 480)
w.show()
sys.exit(app.exec_())
但是你也可以构建一个QPieSlice
using the name and value you can pass it using the other append()方法:
# ...
series = QtChart.QPieSeries()
for name, (value, color) in data.items():
_slice = QtChart.QPieSlice(name, value)
_slice.setBrush(color)
series.append(_slice)
chart = QtChart.QChart()
# ...
而且你也可以通过QPieSlice
using the third append()
方法创建列表来构建:
# ...
series = QtChart.QPieSeries()
slices = []
for name, (value, color) in data.items():
_slice = QtChart.QPieSlice(name, value)
_slice.setBrush(color)
slices.append(_slice)
series.append(slices)
chart = QtChart.QChart()
# ...
更新:
在你的情况下,使用第二种方法:
_slice = series.append("Auto", self.expensesWindow.piechart[0])
_slice.setBrush(QColor('#00FF00'))
_slice = series.append("Employment", self.expensesWindow.piechart[1])
_slice.setBrush(QColor('#1A8CFF'))
# ...
我想让饼图的每一部分都成为我选择的颜色。我需要知道 QPieSlice 的 snytax(如果这是我使用的)以及如何将颜色附加到馅饼的特定切片。例如,我希望 "Auto" 切片为“#00FF00”,"Employment" 切片为“#1A8CFF”,等等
下面是我的馅饼。我尝试过各种方法,例如:
QPieSlice.setBrush(QPieSlice.setColor(QColor('#00FF00')))
但它不起作用,即使它起作用,我也不知道如何将它附加到特定的切片以及将它放在哪里。
series.append("Auto", self.expensesWindow.piechart[0])
series.append("Employment", self.expensesWindow.piechart[1])
series.append("Insurance", self.expensesWindow.piechart[2])
series.append("Household", self.expensesWindow.piechart[3])
series.append("Housing", self.expensesWindow.piechart[4])
series.append("Entertainment", self.expensesWindow.piechart[5])
series.append("Utilities", self.expensesWindow.piechart[6])
series.append("Other", self.expensesWindow.piechart[7])
当您使用 append()
method of QPieSeries
, passing it the name and value it returns its associated QPieSlice
时,您必须使用该元素
from PyQt5 import QtCore, QtGui, QtWidgets, QtChart
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
data = {
"Auto": (10, QtGui.QColor("#00FF00")),
"Employment": (20, QtGui.QColor("#1A8CFF")),
"Insurance": (30, QtGui.QColor("salmon")),
"Household": (40, QtGui.QColor(255, 0, 255)),
"Housing": (40, QtGui.QColor("blue")),
"Entertainment": (30, QtGui.QColor(0, 255, 255)),
"Utilities": (20, QtGui.QColor("#aabbcc")),
"Other": (10, QtGui.QColor("gray")),
}
series = QtChart.QPieSeries()
for name, (value, color) in data.items():
_slice = series.append(name, value)
_slice.setBrush(color)
chart = QtChart.QChart()
chart.addSeries(series)
chart.setTitle("Example for ")
chart.legend().setAlignment(QtCore.Qt.AlignBottom)
chart.legend().setFont(QtGui.QFont("Arial", 7))
chartview = QtChart.QChartView(chart)
chartview.setRenderHint(QtGui.QPainter.Antialiasing)
w = QtWidgets.QMainWindow()
w.setCentralWidget(chartview)
w.resize(640, 480)
w.show()
sys.exit(app.exec_())
但是你也可以构建一个QPieSlice
using the name and value you can pass it using the other append()方法:
# ...
series = QtChart.QPieSeries()
for name, (value, color) in data.items():
_slice = QtChart.QPieSlice(name, value)
_slice.setBrush(color)
series.append(_slice)
chart = QtChart.QChart()
# ...
而且你也可以通过QPieSlice
using the third append()
方法创建列表来构建:
# ...
series = QtChart.QPieSeries()
slices = []
for name, (value, color) in data.items():
_slice = QtChart.QPieSlice(name, value)
_slice.setBrush(color)
slices.append(_slice)
series.append(slices)
chart = QtChart.QChart()
# ...
更新:
在你的情况下,使用第二种方法:
_slice = series.append("Auto", self.expensesWindow.piechart[0])
_slice.setBrush(QColor('#00FF00'))
_slice = series.append("Employment", self.expensesWindow.piechart[1])
_slice.setBrush(QColor('#1A8CFF'))
# ...