How can I plot a pie chart from a database?
-
I want to plot a pie chart using the values obtained from my query but I'm finding it difficult to do so. The example below has values already assigned to it. Please how can I use the results obtained in the table in the image below to plot similar pie chart ?
Sample Code
#create pieseries series = QPieSeries() #append some data to the series series.append("Apple", 80) series.append("Banana", 70) series.append("Pear", 50) series.append("Melon", 80) #create QChart object chart = QChart() chart.addSeries(series) chart.setAnimationOptions(QChart.SeriesAnimations) chart.setTitle("Fruits Pie Chart") chart.setTheme(QChart.ChartThemeDark)
Database result
-
Hi,
The simple method is to loop through your table and take the value from the first column as text for the series item and the value from the second column converted to an int for the value of said item.
-
@SGaist Below is what I came out with but it does not seem working , could you please check to see what I'm doing wrong?
store_name = [] store_count = [] for store_name, store_count in zip(store_name,store_count): print(store_name,store_count) series = QPieSeries() series.append("Store", str(store_name[0])) series.append("Count",int(store_count[1])) chart = QChart() chart.legend().hide() chart.addSeries(series) chart.createDefaultAxes() chart.setAnimationOptions(QChart.SeriesAnimations) chart.setTitle("Pie Chart Example") chart.legend().setVisible(True) chart.legend().setAlignment(Qt.AlignBottom) chartview = QChartView(chart) chartview.setRenderHint(QPainter.Antialiasing) self.ui.widget.setChart(chart)
-
You are creating as many charts as you have entries. Your loop should only add data to the QPieSeries object.
-
Only Ë‹series.append` should be in the loop. And following what you asked, there should be only one line.
-
@SGaist I have place the series.append in the loop as directed but chart does not show. The title " Pie Chart Example" is the only thing displaying. Please below is what I did.
store_name = [] store_count = [] series = QPieSeries() for store_name, store_count in zip(store_name,store_count): series.append("Store", str(store_name[0])) series.append("Count",int(store_count[1])) chart = QChart() chart.legend().hide() chart.addSeries(series) chart.createDefaultAxes() chart.setAnimationOptions(QChart.SeriesAnimations) chart.setTitle("Pie Chart Example")
-
Your series entries are supposed to have the store name as first parameter and the count as second. That's not what you are doing. Also, are you sure that both parameters are from the right types ?
-
Possible, likely yes but you will have to be able to associate your stores with their geographical positions which if you don't have already, you would have to add to your database or retrieve through some other means.
-
@JonB Please I'm trying to use same table and approach to plot a barchart but i get the following error could you please help me out with what I'm doing wrong.
series.append(store_name[0], store_count[1])
TypeError: arguments did not match any overloaded call:
append(self, QBarSet): argument 1 has unexpected type 'str'
append(self, Iterable[QBarSet]): argument 1 has unexpected type 'str'series = QPercentBarSeries() for store_name, store_count in zip(store_name, store_count): series.append(store_name[0], store_count[1]) chart = QChart() chart.addSeries(series) chart.setTitle("Item Type Example") chart.setAnimationOptions(QChart.SeriesAnimations) categories = ["Store A", "Store B", "Store C", "Store D"] axis = QBarCategoryAxis() axis.append(categories) chart.createDefaultAxes() chart.setAxisX(axis, series) chart.legend().setVisible(True) chart.legend().setAlignment(Qt.AlignBottom) chartView = QChartView(chart) chartView.setRenderHint(QPainter.Antialiasing) self.ui.widget_2.setChart(chart)
-
@LT-K101 The error message tells you what is wrong, you just need to check documentation: https://doc.qt.io/qt-5/qabstractbarseries.html#append
There are two append methods: one takes a QBarSet * as parameter, second one takes QList<QBarSet *>. You are passing two parameters: how should this work? -
@LT-K101 said in How can I plot a pie chart from a database?:
for store_name, store_count in zip(store_name, store_count): series.append(store_name[0], store_count[1])
In addition to @jsulm. This still seems like nonsense to me. I don't know what it does, and I have no idea what you do it for/intend it to achieve.
Why don't you at least insert
print store_name[0], store_count[1]
above the
series.append()
in thefor
loop and see if that works and what it outputs that you are appending? Is it correct?