How can I plot a pie chart from a database?
-
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.
-
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")
-
@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.
-
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.
-
@LT-K101
Ifstore_name
andstore_count
are empty, how many items does this append to theseries
, how many times does it go round the loop? Verify how many elements you added to the series if you expect it to show something?@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)
-
@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? -
@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 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?