How to create a barchart using QChartview
-
Re: How to plot Barchart using database queries
I have tried this code to plot a barchart but I get printer bars for each query apart instead of all printer bars together without spaces between them for the first region on the x-axis. I need help please. Thanks in advance.def regional_printer_bar(self): # Query for HP printers hq_hp_barchart = '''SELECT brand, COUNT(*) AS Brand_Count FROM printer_info WHERE district='HQ' GROUP BY brand''' hq_hp_sql = cur.execute(hq_hp_barchart).fetchall() af_barchart = '''SELECT brand, COUNT(*) AS Brand_Count FROM printer_info WHERE region='Af Region' GROUP BY brand''' af_hp_sql = cur.execute(af_barchart).fetchall() ash_barchart = '''SELECT brand, COUNT(*) AS Brand_Count FROM printer_info WHERE region='Ash Region' GROUP BY brand''' ash_hp_sql = cur.execute(ash_barchart).fetchall() # Create a QBarSet object for HP printers hp_set = QBarSet("HP") for printer in hq_hp_sql: hp_set << printer[1] for printer in af_hp_sql: hp_set << printer[1] for printer in ash_hp_sql: hp_set << printer[1] hp_series = QBarSeries() hp_series.append(hp_set) # Add the series to the chart chart = QChart() chart.addSeries(hp_series) #chart.addSeries(canon_series) #chart.addSeries(xerox_series) # Create the Y-axis and set its range axis_y = QValueAxis() axis_y.setRange(0, 100) # Set the range to the min and max count of brand chart.addAxis(axis_y, Qt.AlignLeft) # Create the X-axis and set its categories axis_x = QBarCategoryAxis() categories = ["HQ", "AR", "ASR"] axis_x.append(categories) chart.addAxis(axis_x, Qt.AlignBottom) # Set the chart options and add to the widget chart.setTitle("Brand Name - Regional Printer Total") chart.setAnimationOptions(QChart.SeriesAnimations) chart.legend().setVisible(True) chart.legend().setAlignment(Qt.AlignBottom) chartView = QChartView(chart) chartView.setRenderHint(QPainter.Antialiasing) self.ui.widget_320.setChart(chart)
-
Re: How to plot Barchart using database queries
I have tried this code to plot a barchart but I get printer bars for each query apart instead of all printer bars together without spaces between them for the first region on the x-axis. I need help please. Thanks in advance.def regional_printer_bar(self): # Query for HP printers hq_hp_barchart = '''SELECT brand, COUNT(*) AS Brand_Count FROM printer_info WHERE district='HQ' GROUP BY brand''' hq_hp_sql = cur.execute(hq_hp_barchart).fetchall() af_barchart = '''SELECT brand, COUNT(*) AS Brand_Count FROM printer_info WHERE region='Af Region' GROUP BY brand''' af_hp_sql = cur.execute(af_barchart).fetchall() ash_barchart = '''SELECT brand, COUNT(*) AS Brand_Count FROM printer_info WHERE region='Ash Region' GROUP BY brand''' ash_hp_sql = cur.execute(ash_barchart).fetchall() # Create a QBarSet object for HP printers hp_set = QBarSet("HP") for printer in hq_hp_sql: hp_set << printer[1] for printer in af_hp_sql: hp_set << printer[1] for printer in ash_hp_sql: hp_set << printer[1] hp_series = QBarSeries() hp_series.append(hp_set) # Add the series to the chart chart = QChart() chart.addSeries(hp_series) #chart.addSeries(canon_series) #chart.addSeries(xerox_series) # Create the Y-axis and set its range axis_y = QValueAxis() axis_y.setRange(0, 100) # Set the range to the min and max count of brand chart.addAxis(axis_y, Qt.AlignLeft) # Create the X-axis and set its categories axis_x = QBarCategoryAxis() categories = ["HQ", "AR", "ASR"] axis_x.append(categories) chart.addAxis(axis_x, Qt.AlignBottom) # Set the chart options and add to the widget chart.setTitle("Brand Name - Regional Printer Total") chart.setAnimationOptions(QChart.SeriesAnimations) chart.legend().setVisible(True) chart.legend().setAlignment(Qt.AlignBottom) chartView = QChartView(chart) chartView.setRenderHint(QPainter.Antialiasing) self.ui.widget_320.setChart(chart)
@LT-K101
I have never used QtCharts but take a look at how it's done in https://doc.qt.io/qt-6/qtcharts-barchart-example.html.You have one
QBarSet
with multiple items in it. That causes your "separate groups". You want multipleQBarSet
s (one for each printer) each with just one item/value (printer[1]
) in it. That will "group" them together.