Installing the ChartDirector for Python on Ubuntu Linux and Demo to Create 3D Pie Charts

Post Reply
User avatar
Eli
Senior Expert Member
Reactions: 183
Posts: 5334
Joined: 9 years ago
Location: Tanzania
Has thanked: 75 times
Been thanked: 88 times
Contact:

#1

Below are step-by-step instructions for Installing the ChartDirector for Python 3.6.x on Ubuntu Linux. First, download the appropriate version from here (currently this one) and then follow the instructions below:

  1. $tar -xf chartdir_python_linux_64.tar.gz
  2. $cd ChartDirector/
  3. $ipython3 -c "import sys; print(sys.path)"
  4. // Or
  5. $ipython3
  6. >>import sys
  7. >> print(sys)
  8. ['/home/elimboto/.local/bin', '/usr/lib/python36.zip', '/usr/lib/python3.6', '/usr/lib/python3.6/lib-dynload', '', '/home/elimboto/.local/lib/python3.6/site-packages', '/usr/local/lib/python3.6/dist-packages', '/usr/lib/python3/dist-packages', '/home/elimboto/.local/lib/python3.6/site-packages/IPython/extensions', '/home/elimboto/.ipython']
  9.  :~/Desktop/ChartDirector$ cd /usr/local/lib/python3.6/dist-packages
  10. $sudo mkdir chartdirector
  11. $ cd chartdirector/
  12. $pwd
  13. /usr/local/lib/python3.6/dist-packages/chartdirector
  14. $ cd //Move to the directory where the ChartDirectory is downloaded, in this case  Desktop
  15. $:~/Desktop$ cd ChartDirector/lib/
  16. $ sudo cp -a ./* /usr/local/lib/python3.6/dist-packages/chartdirector/
  17. $ cd /usr/local/lib/python3.6/dist-packages/

0
TSSFL -- A Creative Journey Towards Infinite Possibilities!
User avatar
Eli
Senior Expert Member
Reactions: 183
Posts: 5334
Joined: 9 years ago
Location: Tanzania
Has thanked: 75 times
Been thanked: 88 times
Contact:

#2

And then one can run the following codes.

Below are a number of sample codes (taken from here with some modifications) to create pie charts using ChartDirector 7.0 (Python Edition).

3D Pie Chart

  1. #!/usr/bin/python
  2. # https://www.advsofteng.com/doc/cdpydoc/threedpie.htm
  3. #https://www.796t.com/article.php?id=139934
  4.  
  5. # The ChartDirector for Python module is assumed to be in "../lib"
  6. import sys, os
  7.  
  8. #sys.path.insert(0,"/usr/local/lib/python3.6/dist-packages")
  9. sys.path.insert(0, os.path.join(os.path.abspath(sys.path[0]), "..", "/usr/local/lib/python3.6/dist-packages/chartdirector"))
  10. from pychartdir import *
  11.  
  12. # The data for the pie chart
  13. data = [22, 18, 18, 12, 10, 30, 33]
  14.  
  15. # The labels for the pie chart
  16. labels = ["Salaries", "Licenses", "Taxes", "Legal", "Insurance", "Facilities", "Production"]
  17.  
  18. # Create a PieChart object of size 360 x 300 pixels
  19. c = PieChart(360, 300)
  20.  
  21. # Set the center of the pie at (180, 140) and the radius to 100 pixels
  22. c.setPieSize(180, 140, 100)
  23.  
  24. # Add a title to the pie chart
  25. c.addTitle("Project Cost Summary")
  26.  
  27. # Draw the pie in 3D
  28. c.set3D()
  29.  
  30. # Set the pie data and the pie labels
  31. c.setData(data, labels)
  32. # Explode the 1st sector (index = 0)
  33. c.setExplode(0)
  34. # Output the chart
  35. c.makeChart("Image.png")


Image

Image

Image

Image

Image

Image

Image


Multi-Depth Pie Chart

  1. #!/usr/bin/python
  2.  
  3. # The ChartDirector for Python module is assumed to be in "../lib"
  4. import sys, os
  5. sys.path.insert(0, os.path.join(os.path.abspath(sys.path[0]), "..", "/usr/local/lib/python3.6/dist-packages/chartdirector"))
  6.  
  7. from pychartdir import *
  8.  
  9. # The data for the pie chart
  10. data = [72, 18, 15, 12]
  11. #data = [45, 15, 30, 10]
  12. # The labels for the pie chart
  13. #labels = ["Labor", "Machinery", "Facilities", "Computers"]
  14. labels = ['Cows', 'Goats', 'Sheep', 'Other']
  15. # The depths for the sectors
  16. depths = [30, 20, 10, 10]
  17.  
  18. # Create a PieChart object of size 360 x 300 pixels, with a light blue (DDDDFF) background and a 1
  19. # pixel 3D border
  20. c = PieChart(360, 300, 0xddddff, -1, 1)
  21.  
  22. # Set the center of the pie at (180, 175) and the radius to 100 pixels
  23. c.setPieSize(180, 175, 100)
  24.  
  25. # Add a title box using 15pt Times Bold Italic font and blue (AAAAFF) as background color
  26. c.addTitle("Domestic Animals", "Times New Roman Bold Italic", 15).setBackground(0xaaaaff)
  27.  
  28. # Set the pie data and the pie labels
  29. c.setData(data, labels)
  30.  
  31. # Draw the pie in 3D with variable 3D depths
  32. c.set3D2(depths)
  33.  
  34. # Set the start angle to 225 degrees may improve layout when the depths of the sector are sorted in
  35. # descending order, because it ensures the tallest sector is at the back.
  36. c.setStartAngle(225)
  37.  
  38. # Output the chart
  39. c.makeChart("multidepth_pie_chart.png")


Image

Image

Image


Icon Pie Chart

Pie Chart with Weather Icons

Under this case, you can as well omit icons.

  1. #!/usr/bin/python
  2.  
  3. # The ChartDirector for Python module is assumed to be in "../lib"
  4. import sys, os
  5. sys.path.insert(0, os.path.join(os.path.abspath(sys.path[0]), "..", "/usr/local/lib/python3.6/dist-packages/chartdirector"))
  6.  
  7. from pychartdir import *
  8.  
  9. # The data for the pie chart
  10. #data = [72, 18, 15, 12]
  11. data = [45, 15, 30, 10]
  12. # The depths for the sectors
  13. depths = [30, 20, 10, 10]
  14.  
  15. # The labels for the pie chart
  16. labels = ["Sunny", "Cloudy", "Rainy", "Snowy"]
  17.  
  18. # The icons for the sectors
  19. icons = ["sun.png", "cloud.png", "rain.png", "snowy.png"]
  20.  
  21. # Create a PieChart object of size 400 x 310 pixels, with a blue (CCCCFF) vertical metal gradient
  22. # background, black border, 1 pixel 3D border effect and rounded corners
  23. c = PieChart(400, 310, metalColor(0xccccff, 0), 0x000000, 1)
  24. c.setRoundedFrame()
  25.  
  26. # Set the center of the pie at (200, 180) and the radius to 100 pixels
  27. c.setPieSize(200, 180, 100)
  28.  
  29. # Add a title box using 15pt Times Bold Italic font, on a blue (CCCCFF) background with glass effect
  30. c.addTitle("Weather Profile", "Times New Roman Bold Italic", 15).setBackground(
  31.     0xccccff, 0x000000, glassEffect())
  32.  
  33. # Set the pie data and the pie labels
  34. c.setData(data, labels)
  35.  
  36. # Add icons to the chart as a custom field
  37. c.addExtraField(icons)
  38.  
  39. # Configure the sector labels using CDML to include the icon images
  40. c.setLabelFormat("<*block,valign=absmiddle*><*img={field0}*> <*block*>{label}\n{percent}%<*/*><*/*>"
  41.     )
  42.  
  43. # Draw the pie in 3D with variable 3D depths
  44. c.set3D2(depths)
  45.  
  46. # Set the start angle to 225 degrees may improve layout when the depths of the sector are sorted in
  47. # descending order, because it ensures the tallest sector is at the back.
  48. c.setStartAngle(225)
  49.  
  50. # Output the chart
  51. c.makeChart("weathericon_piechart.png")


Image

Image

Image


Pie Chart with Emoji Icons

  1. #!/usr/bin/python
  2.  
  3. # The ChartDirector for Python module is assumed to be in "../lib"
  4. import sys, os
  5. sys.path.insert(0, os.path.join(os.path.abspath(sys.path[0]), "..", "/usr/local/lib/python3.6/dist-packages/chartdirector"))
  6.  
  7. from pychartdir import *
  8.  
  9. # The data for the pie chart
  10. data = [35, 40, 10, 5, 10]
  11.  
  12. # The labels for the pie chart
  13. labels = ["Excellent", "Very Good", "Bad", "Very Bad", "Neutral"]
  14.  
  15. # The icons for the sectors
  16. icons = ["laugh.png", "smile.png", "sad.png", "angry.png", "nocomment.png"]
  17.  
  18. # Create a PieChart object of size 560 x 300 pixels, with a silver background, black border, 1 pxiel
  19. # 3D border effect and rounded corners
  20. c = PieChart(560, 300, silverColor(), 0x000000, 1)
  21. c.setRoundedFrame()
  22.  
  23. # Set the center of the pie at (280, 150) and the radius to 120 pixels
  24. c.setPieSize(280, 150, 120)
  25.  
  26. # Add a title box with title written in CDML, on a sky blue (A0C8FF) background with glass effect
  27. c.addTitle(
  28.     "<*block,valign=absmiddle*><*img=doc.png*> Customer Survey: <*font=Times New Roman " \
  29.     "Italic,color=000000*>Do you like our <*font,color=dd0000*>Hyper<*super*>TM<*/font*> " \
  30.     "products?", "Times New Roman Bold Italic", 15, 0x000080).setBackground(0xa0c8ff, 0x000000,
  31.     glassEffect())
  32.  
  33. # Add a logo to the chart written in CDML as the bottom title aligned to the bottom right
  34. c.addTitle2(BottomRight,
  35.     "<*block,valign=absmiddle*><*img=molecule.png*> <*block*><*color=FF*><*font=Times New Roman " \
  36.     "Bold Italic,size=12*>Molecular Engineering\n<*font=Arial,size=10*>Creating better molecules")
  37.  
  38. # Set the pie data and the pie labels
  39. c.setData(data, labels)
  40.  
  41. # Set 3D style
  42. c.set3D()
  43.  
  44. # Use the side label layout method
  45. c.setLabelLayout(SideLayout)
  46.  
  47. # Set the label background color to transparent
  48. c.setLabelStyle().setBackground(Transparent)
  49.  
  50. # Add icons to the chart as a custom field
  51. c.addExtraField(icons)
  52.  
  53. # Configure the sector labels using CDML to include the icon images
  54. c.setLabelFormat("<*block,valign=absmiddle*><*img={field0}*> {label} ({percent|0}%)")
  55.  
  56. # Explode the 3rd and 4th sectors as a group (index = 2 and 3)
  57. c.setExplodeGroup(2, 3)
  58.  
  59. # Set the start angle to 135 degrees may improve layout when there are many small sectors at the end
  60. # of the data array (that is, data sorted in descending order). It is because this makes the small
  61. # sectors position near the horizontal axis, where the text label has the least tendency to overlap.
  62. # For data sorted in ascending order, a start angle of 45 degrees can be used instead.
  63. c.setStartAngle(135)
  64.  
  65. # Output the chart
  66. c.makeChart("emojiicon_piechart.png")

Image

Image


See all these and more at ChartDirector 7.0 (Python Edition), Pie Chart Gallery, and ChartDirector website
0
TSSFL -- A Creative Journey Towards Infinite Possibilities!
Post Reply
  • Similar Topics
    Replies
    Views
    Last post

Return to “Linux and Unix Based Operating Systems”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 3 guests