qpieslice 渐变效果_将我选择的颜色附加到QPieSeries的每个切片上

I want to make each slice of a pie chart a color of my choosing. I need to know the snytax of QPieSlice (if that is what I use) and how to attach a color to a particular slice of the pie. For example, I want the "Auto" slice to be '#00FF00', the "Employment" slice to be '#1A8CFF", etc.

Below are the pieces of my pie. I have tried various things like:

QPieSlice.setBrush(QPieSlice.setColor(QColor('#00FF00')))

but it does not work, and even if it did, I do not know how to attach it to a particular slice and where to put it.

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])

解决方案

When you use the append() method of QPieSeries, passing it the name and value it returns its associated QPieSlice so you must use that element

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 https://stackoverflow.com/questions/56727499")

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_())

But you can also build a QPieSlice using the name and value you can pass it using the other append() method:

# ...

series = QtChart.QPieSeries()

for name, (value, color) in data.items():

_slice = QtChart.QPieSlice(name, value)

_slice.setBrush(color)

series.append(_slice)

chart = QtChart.QChart()

# ...

And you can also build by creating a list of QPieSlice using the third append() method:

# ...

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()

# ...

Update:

In your case, use the second method:

_slice = series.append("Auto", self.expensesWindow.piechart[0])

_slice.setBrush(QColor('#00FF00'))

_slice = series.append("Employment", self.expensesWindow.piechart[1])

_slice.setBrush(QColor('#1A8CFF'))

# ...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值