Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to create a barchart using QChartview
Forum Updated to NodeBB v4.3 + New Features

How to create a barchart using QChartview

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 237 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    LT-K101
    wrote on last edited by
    #1

    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)
    
    JonBJ 1 Reply Last reply
    0
    • L LT-K101

      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)
      
      JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by JonB
      #2

      @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 multiple QBarSets (one for each printer) each with just one item/value (printer[1]) in it. That will "group" them together.

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved