Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. How can I plot a pie chart from a database?
Forum Updated to NodeBB v4.3 + New Features

How can I plot a pie chart from a database?

Scheduled Pinned Locked Moved Unsolved Qt for Python
17 Posts 4 Posters 1.4k Views 1 Watching
  • 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.
  • SGaistS SGaist

    You are creating as many charts as you have entries. Your loop should only add data to the QPieSeries object.

    L Offline
    L Offline
    LT-K101
    wrote on last edited by
    #5

    @SGaist Please could you help me correct what you saying in the code I provided because I'm new to pyqt5 and python. Thank you.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #6

      Only ˋseries.append` should be in the loop. And following what you asked, there should be only one line.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      L 1 Reply Last reply
      0
      • SGaistS SGaist

        Only ˋseries.append` should be in the loop. And following what you asked, there should be only one line.

        L Offline
        L Offline
        LT-K101
        wrote on last edited by LT-K101
        #7

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

          @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")
          
          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #8

          @LT-K101
          If store_name and store_count are empty, how many items does this append to the series, 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?

          L 1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #9

            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 ?

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            1
            • L Offline
              L Offline
              LT-K101
              wrote on last edited by LT-K101
              #10

              @JonB and @SGaist Thanks a lot problem solved. Please is it possible to display a geographical area map instead of pie chart using same data from database?

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #11

                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.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                L 1 Reply Last reply
                0
                • SGaistS SGaist

                  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.

                  L Offline
                  L Offline
                  LT-K101
                  wrote on last edited by
                  #12

                  @SGaist Thanks I will try my hands on it and let you know

                  1 Reply Last reply
                  0
                  • JonBJ JonB

                    @LT-K101
                    If store_name and store_count are empty, how many items does this append to the series, 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?

                    L Offline
                    L Offline
                    LT-K101
                    wrote on last edited by LT-K101
                    #13

                    @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)
                    
                    jsulmJ JonBJ 2 Replies Last reply
                    0
                    • L LT-K101

                      @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)
                      
                      jsulmJ Online
                      jsulmJ Online
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #14

                      @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?

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      0
                      • L LT-K101

                        @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)
                        
                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by
                        #15

                        @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 the for loop and see if that works and what it outputs that you are appending? Is it correct?

                        1 Reply Last reply
                        0
                        • L Offline
                          L Offline
                          LT-K101
                          wrote on last edited by
                          #16

                          @JonB and @jsulm Thanks for the heads up. I did the following and it worked.

                          
                                  item_name = (result)
                          
                                  series = QHorizontalBarSeries()
                          
                                  for item,num in item_name:
                          
                                      item_name1 = QBarSet(item)
                          
                                      item_name1 << num
                          
                                      series.append(item_name1)
                          
                          1 Reply Last reply
                          1
                          • L Offline
                            L Offline
                            LT-K101
                            wrote on last edited by
                            #17

                            This is the result i obtained. Please is there a way to space the bars out with this barchart type?

                            Screenshot (65).png

                            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