A breakdown pie chart - ReportLab Snippets (Beta)

Code snippets are bits of re-usable code submitted by the ReportLab community.
We don't promise that they are accurate, up-to-date or correct - use them at your own risk!

We'd love it if you could participate by submitting your own snippets and sharing your experience with others by commenting.

You will need to create a user account by filling out our very simple form.

View all snippets


A breakdown pie chart

Author:
rptlab
Posted:
Dec. 3, 2009
Language:
Python
Tags:
opensource rl-toolkit

This shows a Pie chart with a Legend, being saved as both a bitmap for the web and a PDF chart. This code was generated using Diagra (ReportLab's commercial charts package), which allows you to define charts in a GUI. However, you don't need this package to run it, and all the chart features demonstrated are available in our open source package.

view plaincopy to clipboardprint?
  1. #AutogeneratedbyReportLabguieditdonotedit
  2. fromreportlab.graphics.charts.piechartsimportPie
  3. fromreportlab.lib.colorsimportblack,red,purple,green,maroon,brown,pink,white,HexColor
  4. fromreportlab.graphics.charts.legendsimportLegend
  5. fromreportlab.graphics.shapesimportDrawing,_DrawingEditorMixin
  6. fromreportlab.lib.validatorsimportAuto
  7. fromreportlab.lib.colorsimportHexColor,black
  8. pdf_chart_colors=[
  9. HexColor("#0000e5"),
  10. HexColor("#1f1feb"),
  11. HexColor("#5757f0"),
  12. HexColor("#8f8ff5"),
  13. HexColor("#c7c7fa"),
  14. HexColor("#f5c2c2"),
  15. HexColor("#eb8585"),
  16. HexColor("#e04747"),
  17. HexColor("#d60a0a"),
  18. HexColor("#cc0000"),
  19. HexColor("#ff0000"),
  20. ]
  21. defsetItems(n,obj,attr,values):
  22. m=len(values)
  23. i=m//n
  24. forjinxrange(n):
  25. setattr(obj[j],attr,values[j*i%m])
  26. classBreakdownPieDrawing(_DrawingEditorMixin,Drawing):
  27. def__init__(self,width=400,height=200,*args,**kw):
  28. apply(Drawing.__init__,(self,width,height)+args,kw)
  29. #addingapiecharttothedrawing
  30. self._add(self,Pie(),name='pie',validate=None,desc=None)
  31. self.pie.width=150
  32. self.pie.height=self.pie.width
  33. self.pie.x=20
  34. self.pie.y=(height-self.pie.height)/2
  35. self.pie.data=[26.90,13.30,11.10,9.40,8.50,7.80,7.00,6.20,8.80,1.00]
  36. self.pie.labels=['Financials','Energy','HealthCare','Telecoms','Consumer','Consumer2','Industrials','Materials','Other','LiquidAssets']
  37. self.pie.simpleLabels=1
  38. self.pie.slices.label_visible=0
  39. self.pie.slices.fontColor=None
  40. self.pie.slices.strokeColor=white
  41. self.pie.slices.strokeWidth=1
  42. #addinglegend
  43. self._add(self,Legend(),name='legend',validate=None,desc=None)
  44. self.legend.x=200
  45. self.legend.y=height/2
  46. self.legend.dx=8
  47. self.legend.dy=8
  48. self.legend.fontName='Helvetica'
  49. self.legend.fontSize=7
  50. self.legend.boxAnchor='w'
  51. self.legend.columnMaximum=10
  52. self.legend.strokeWidth=1
  53. self.legend.strokeColor=black
  54. self.legend.deltax=75
  55. self.legend.deltay=10
  56. self.legend.autoXPadding=5
  57. self.legend.yGap=0
  58. self.legend.dxTextSpace=5
  59. self.legend.alignment='right'
  60. self.legend.dividerLines=1|2|4
  61. self.legend.dividerOffsY=4.5
  62. self.legend.subCols.rpad=30
  63. n=len(self.pie.data)
  64. setItems(n,self.pie.slices,'fillColor',pdf_chart_colors)
  65. self.legend.colorNamePairs=[(self.pie.slices[i].fillColor,(self.pie.labels[i][0:20],'%0.2f'%self.pie.data[i]))foriinxrange(n)]
  66. if__name__=="__main__":#NORUNTESTS
  67. drawing=BreakdownPieDrawing()
  68. #thedrawingwillbesavedaspdfandpngbelow,youcoulddootherthingswithitobviously.
  69. drawing.save(formats=['pdf','png'],outDir='.',fnRoot=None)
#Autogenerated by ReportLab guiedit do not edit
from reportlab.graphics.charts.piecharts import Pie
from reportlab.lib.colors import black, red, purple, green, maroon, brown, pink, white, HexColor
from reportlab.graphics.charts.legends import Legend
from reportlab.graphics.shapes import Drawing, _DrawingEditorMixin
from reportlab.lib.validators import Auto
from reportlab.lib.colors import HexColor, black
pdf_chart_colors = [
HexColor("#0000e5"),
HexColor("#1f1feb"),
HexColor("#5757f0"),
HexColor("#8f8ff5"),
HexColor("#c7c7fa"),
HexColor("#f5c2c2"),
HexColor("#eb8585"),
HexColor("#e04747"),
HexColor("#d60a0a"),
HexColor("#cc0000"),
HexColor("#ff0000"),
]
def setItems(n, obj, attr, values):
m = len(values)
i = m // n
for j in xrange(n):
setattr(obj[j],attr,values[j*i % m])
class BreakdownPieDrawing(_DrawingEditorMixin,Drawing):
def __init__(self,width=400,height=200,*args,**kw):
apply(Drawing.__init__,(self,width,height)+args,kw)
# adding a pie chart to the drawing
self._add(self,Pie(),name='pie',validate=None,desc=None)
self.pie.width                  = 150
self.pie.height                 = self.pie.width
self.pie.x                      = 20
self.pie.y                      = (height-self.pie.height)/2
self.pie.data = [26.90,13.30,11.10,9.40,8.50,7.80,7.00,6.20,8.80,1.00]
self.pie.labels = ['Financials','Energy','Health Care','Telecoms','Consumer','Consumer 2','Industrials','Materials','Other','Liquid Assets']
self.pie.simpleLabels           = 1
self.pie.slices.label_visible   = 0
self.pie.slices.fontColor       = None
self.pie.slices.strokeColor     = white
self.pie.slices.strokeWidth     = 1
# adding legend
self._add(self,Legend(),name='legend',validate=None,desc=None)
self.legend.x               = 200
self.legend.y               = height/2
self.legend.dx              = 8
self.legend.dy              = 8
self.legend.fontName        = 'Helvetica'
self.legend.fontSize        = 7
self.legend.boxAnchor       = 'w'
self.legend.columnMaximum   = 10
self.legend.strokeWidth     = 1
self.legend.strokeColor     = black
self.legend.deltax          = 75
self.legend.deltay          = 10
self.legend.autoXPadding    = 5
self.legend.yGap            = 0
self.legend.dxTextSpace     = 5
self.legend.alignment       = 'right'
self.legend.dividerLines    = 1|2|4
self.legend.dividerOffsY    = 4.5
self.legend.subCols.rpad    = 30
n = len(self.pie.data)
setItems(n,self.pie.slices,'fillColor',pdf_chart_colors)
self.legend.colorNamePairs = [(self.pie.slices[i].fillColor, (self.pie.labels[i][0:20], '%0.2f' % self.pie.data[i])) for i in xrange(n)]
if __name__=="__main__": #NORUNTESTS
drawing = BreakdownPieDrawing()
# the drawing will be saved as pdf and png below, you could do other things with it obviously.
drawing.save(formats=['pdf','png'],outDir='.',fnRoot=None)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。