Results 1 to 4 of 4

Thread: Getting a QPieChart to work

  1. #1
    Join Date
    Jan 2017
    Posts
    54
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Getting a QPieChart to work

    I want to make a pie chart and have looked at the documentation (it's in C, which I do not understand) and am having trouble. I have 8 slices of the pie and I want each a different color. Here is what I have so far, which produces at least one error (comes on the line: series.append("Auto", auto):
    Qt Code:
    1. series = QPieSeries()
    2. chart = QChart()
    3.  
    4. # Each slice's value
    5. auto = self.expensesWindow.piechart[0]
    6. employ = self.expensesWindow.piechart[1]
    7. ins = self.expensesWindow.piechart[2]
    8. hh = self.expensesWindow.piechart[3]
    9. house = self.expensesWindow.piechart[4]
    10. enter = self.expensesWindow.piechart[5]
    11. util = self.expensesWindow.piechart[6]
    12. other = self.expensesWindow.piechart[7]
    13. #
    14. # Set up the pie
    15. series.append("Auto", auto)
    16. series.append("Employ", employ)
    17. series.append("Insurance", ins)
    18. series.append("Household", hh)
    19. series.append("House", house)
    20. series.append("Entertainment", enter)
    21. series.append("Utilities", util)
    22. series.append("Other)", other)
    23. #
    24. chart.addSeries(series)
    To copy to clipboard, switch view to plain text mode 

    Regarding the color of each slice, I came up with this example for one of the slices (but likely not correct) and don't know where to put it:
    Qt Code:
    1. Auto = QPieSlice("Auto")
    2. Auto.setBrush(QPieSlice.setColor(QColor('#00FF00')))
    To copy to clipboard, switch view to plain text mode 

    To show the chart:
    Qt Code:
    1. self.chartView = QChartView(chart)
    2. self.ui.gridLayout.addWidget(self.chartView)
    To copy to clipboard, switch view to plain text mode 

    I need help getting the construction of the pie chart to work. thanks

    I want to make a pie chart and have looked at the documentation (it's in C, which I do not understand) and am having trouble. I have 8 slices of the pie and I want each a different color. Here is what I have so far, which produces at least one error (comes on the line: series.append("Auto", auto):
    Qt Code:
    1. series = QPieSeries()
    2. chart = QChart()
    3.  
    4. # Each slice's value
    5. auto = self.expensesWindow.piechart[0]
    6. employ = self.expensesWindow.piechart[1]
    7. ins = self.expensesWindow.piechart[2]
    8. hh = self.expensesWindow.piechart[3]
    9. house = self.expensesWindow.piechart[4]
    10. enter = self.expensesWindow.piechart[5]
    11. util = self.expensesWindow.piechart[6]
    12. other = self.expensesWindow.piechart[7]
    13. #
    14. # Set up the pie
    15. series.append("Auto", auto)
    16. series.append("Employ", employ)
    17. series.append("Insurance", ins)
    18. series.append("Household", hh)
    19. series.append("House", house)
    20. series.append("Entertainment", enter)
    21. series.append("Utilities", util)
    22. series.append("Other)", other)
    23. #
    24. chart.addSeries(series)
    To copy to clipboard, switch view to plain text mode 

    Regarding the color of each slice, I came up with this example for one of the slices (but likely not correct) and don't know where to put it:
    Qt Code:
    1. Auto = QPieSlice("Auto")
    2. Auto.setBrush(QPieSlice.setColor(QColor('#00FF00')))
    To copy to clipboard, switch view to plain text mode 

    To show the chart:
    Qt Code:
    1. self.chartView = QChartView(chart)
    2. self.ui.gridLayout.addWidget(self.chartView)
    To copy to clipboard, switch view to plain text mode 

    I need help getting the construction of the pie chart to work. thanks


    Added after 1 21 minutes:


    Quote Originally Posted by dennisvz View Post
    I want to make a pie chart and have looked at the documentation (it's in C, which I do not understand) and am having trouble. I have 8 slices of the pie and I want each a different color. Here is what I have so far, which produces at least one error (comes on the line: series.append("Auto", auto):
    Qt Code:
    1. series = QPieSeries()
    2. chart = QChart()
    3.  
    4. # Each slice's value
    5. auto = self.expensesWindow.piechart[0]
    6. employ = self.expensesWindow.piechart[1]
    7. ins = self.expensesWindow.piechart[2]
    8. hh = self.expensesWindow.piechart[3]
    9. house = self.expensesWindow.piechart[4]
    10. enter = self.expensesWindow.piechart[5]
    11. util = self.expensesWindow.piechart[6]
    12. other = self.expensesWindow.piechart[7]
    13. #
    14. # Set up the pie
    15. series.append("Auto", auto)
    16. series.append("Employ", employ)
    17. series.append("Insurance", ins)
    18. series.append("Household", hh)
    19. series.append("House", house)
    20. series.append("Entertainment", enter)
    21. series.append("Utilities", util)
    22. series.append("Other)", other)
    23. #
    24. chart.addSeries(series)
    To copy to clipboard, switch view to plain text mode 

    Regarding the color of each slice, I came up with this example for one of the slices (but likely not correct) and don't know where to put it:
    Qt Code:
    1. Auto = QPieSlice("Auto")
    2. Auto.setBrush(QPieSlice.setColor(QColor('#00FF00')))
    To copy to clipboard, switch view to plain text mode 

    To show the chart:
    Qt Code:
    1. self.chartView = QChartView(chart)
    2. self.ui.gridLayout.addWidget(self.chartView)
    To copy to clipboard, switch view to plain text mode 

    I need help getting the construction of the pie chart to work. thanks
    UPDATE: I got the pie chart to work (the code was correct - there was an error in the data file), but I still need to know how to make each piece of the pie a color of my choosing.
    SOLVED: Thanks for looking
    Qt Code:
    1. _slice = series.append("Auto", self.expensesWindow.piechart[0])
    2. _slice.setBrush(QColor('#00FF00'))
    3. _slice = series.append("Employment", self.expensesWindow.piechart[1])
    4. _slice.setBrush(QColor('#1a8cff'))
    5. _slice = series.append("Insurance", self.expensesWindow.piechart[2])
    6. _slice.setBrush(QColor('#ff0000'))
    7. _slice = series.append("Household", self.expensesWindow.piechart[3])
    8. _slice.setBrush(QColor('#cccccc'))
    9. _slice = series.append("Housing", self.expensesWindow.piechart[4])
    10. _slice.setBrush(QColor('#00ffff'))
    11. _slice = series.append("Entertainment", self.expensesWindow.piechart[5])
    12. _slice.setBrush(QColor('#ff00ff'))
    13. _slice = series.append("Utilities", self.expensesWindow.piechart[6])
    14. _slice.setBrush(QColor('#ff7f50'))
    15. _slice = series.append("Other", self.expensesWindow.piechart[7])
    16. _slice.setBrush(QColor('#ffff00'))
    To copy to clipboard, switch view to plain text mode 
    Last edited by dennisvz; 24th June 2019 at 00:25.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Getting a QPieChart to work

    The QPieSeries::append() call returns a QPieSlice object.

    So if you want you can set the color right there

    Qt Code:
    1. series.append("Auto", auto).setColor("#00ff00");
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  3. #3
    Join Date
    Jan 2017
    Posts
    54
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Getting a QPieChart to work

    Thanks anda -- I like your streamlined version

    How would I:

    1. Set the font size on the labels that point to the slices (labels already showing on outside of pie with a line to the slice)
    2. Put the % each slice represents next to the label on the outside of the pie

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Getting a QPieChart to work

    Font is just another setting of the slide

    Qt Code:
    1. slice = series.append("Auto", auto);
    2. slice.setColor(...)
    3. slice.setLabelFont(...)
    To copy to clipboard, switch view to plain text mode 

    For the percentage you might have to calculate that yourself and then call setLabel() on the slice.

    Cheers,
    _

Similar Threads

  1. Replies: 0
    Last Post: 10th April 2015, 14:05
  2. Replies: 3
    Last Post: 9th October 2012, 02:12
  3. Replies: 0
    Last Post: 12th July 2011, 08:20
  4. Replies: 2
    Last Post: 13th December 2009, 20:27
  5. Replies: 3
    Last Post: 15th January 2008, 12:11

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.