Re: Getting a QPieChart to work
I want to make a pie chart and have looked at the documentation (it's in C, which I do not understand) and am having trouble. I have 8 slices of the pie and I want each a different color. Here is what I have so far, which produces at least one error (comes on the line: series.append("Auto", auto):
Code:
series = QPieSeries()
chart = QChart()
# Each slice's value
auto = self.expensesWindow.piechart[0]
employ = self.expensesWindow.piechart[1]
ins = self.expensesWindow.piechart[2]
hh = self.expensesWindow.piechart[3]
house = self.expensesWindow.piechart[4]
enter = self.expensesWindow.piechart[5]
util = self.expensesWindow.piechart[6]
other = self.expensesWindow.piechart[7]
#
# Set up the pie
series.append("Auto", auto)
series.append("Employ", employ)
series.append("Insurance", ins)
series.append("Household", hh)
series.append("House", house)
series.append("Entertainment", enter)
series.append("Utilities", util)
series.append("Other)", other)
#
chart.addSeries(series)
Regarding the color of each slice, I came up with this example for one of the slices (but likely not correct) and don't know where to put it:
Code:
Auto = QPieSlice("Auto")
Auto.
setBrush(QPieSlice.
setColor(QColor('#00FF00')))
To show the chart:
Code:
self.chartView = QChartView(chart)
self.ui.gridLayout.addWidget(self.chartView)
I need help getting the construction of the pie chart to work. thanks
I want to make a pie chart and have looked at the documentation (it's in C, which I do not understand) and am having trouble. I have 8 slices of the pie and I want each a different color. Here is what I have so far, which produces at least one error (comes on the line: series.append("Auto", auto):
Code:
series = QPieSeries()
chart = QChart()
# Each slice's value
auto = self.expensesWindow.piechart[0]
employ = self.expensesWindow.piechart[1]
ins = self.expensesWindow.piechart[2]
hh = self.expensesWindow.piechart[3]
house = self.expensesWindow.piechart[4]
enter = self.expensesWindow.piechart[5]
util = self.expensesWindow.piechart[6]
other = self.expensesWindow.piechart[7]
#
# Set up the pie
series.append("Auto", auto)
series.append("Employ", employ)
series.append("Insurance", ins)
series.append("Household", hh)
series.append("House", house)
series.append("Entertainment", enter)
series.append("Utilities", util)
series.append("Other)", other)
#
chart.addSeries(series)
Regarding the color of each slice, I came up with this example for one of the slices (but likely not correct) and don't know where to put it:
Code:
Auto = QPieSlice("Auto")
Auto.
setBrush(QPieSlice.
setColor(QColor('#00FF00')))
To show the chart:
Code:
self.chartView = QChartView(chart)
self.ui.gridLayout.addWidget(self.chartView)
I need help getting the construction of the pie chart to work. thanks
Added after 1 21 minutes:
Quote:
Originally Posted by
dennisvz
I want to make a pie chart and have looked at the documentation (it's in C, which I do not understand) and am having trouble. I have 8 slices of the pie and I want each a different color. Here is what I have so far, which produces at least one error (comes on the line: series.append("Auto", auto):
Code:
series = QPieSeries()
chart = QChart()
# Each slice's value
auto = self.expensesWindow.piechart[0]
employ = self.expensesWindow.piechart[1]
ins = self.expensesWindow.piechart[2]
hh = self.expensesWindow.piechart[3]
house = self.expensesWindow.piechart[4]
enter = self.expensesWindow.piechart[5]
util = self.expensesWindow.piechart[6]
other = self.expensesWindow.piechart[7]
#
# Set up the pie
series.append("Auto", auto)
series.append("Employ", employ)
series.append("Insurance", ins)
series.append("Household", hh)
series.append("House", house)
series.append("Entertainment", enter)
series.append("Utilities", util)
series.append("Other)", other)
#
chart.addSeries(series)
Regarding the color of each slice, I came up with this example for one of the slices (but likely not correct) and don't know where to put it:
Code:
Auto = QPieSlice("Auto")
Auto.
setBrush(QPieSlice.
setColor(QColor('#00FF00')))
To show the chart:
Code:
self.chartView = QChartView(chart)
self.ui.gridLayout.addWidget(self.chartView)
I need help getting the construction of the pie chart to work. thanks
UPDATE: I got the pie chart to work (the code was correct - there was an error in the data file), but I still need to know how to make each piece of the pie a color of my choosing.
SOLVED: Thanks for looking
Code:
_slice = series.append("Auto", self.expensesWindow.piechart[0])
_slice.
setBrush(QColor('#00FF00')) _slice = series.append("Employment", self.expensesWindow.piechart[1])
_slice.
setBrush(QColor('#1a8cff')) _slice = series.append("Insurance", self.expensesWindow.piechart[2])
_slice.
setBrush(QColor('#ff0000')) _slice = series.append("Household", self.expensesWindow.piechart[3])
_slice.
setBrush(QColor('#cccccc')) _slice = series.append("Housing", self.expensesWindow.piechart[4])
_slice.
setBrush(QColor('#00ffff')) _slice = series.append("Entertainment", self.expensesWindow.piechart[5])
_slice.
setBrush(QColor('#ff00ff')) _slice = series.append("Utilities", self.expensesWindow.piechart[6])
_slice.
setBrush(QColor('#ff7f50')) _slice = series.append("Other", self.expensesWindow.piechart[7])
_slice.
setBrush(QColor('#ffff00'))
Re: Getting a QPieChart to work
The QPieSeries::append() call returns a QPieSlice object.
So if you want you can set the color right there
Code:
series.append("Auto", auto).setColor("#00ff00");
Cheers,
_
Re: Getting a QPieChart to work
Thanks anda -- I like your streamlined version
How would I:
1. Set the font size on the labels that point to the slices (labels already showing on outside of pie with a line to the slice)
2. Put the % each slice represents next to the label on the outside of the pie
Re: Getting a QPieChart to work
Font is just another setting of the slide
Code:
slice = series.append("Auto", auto);
slice.setColor(...)
slice.setLabelFont(...)
For the percentage you might have to calculate that yourself and then call setLabel() on the slice.
Cheers,
_