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.
- #AutogeneratedbyReportLabguieditdonotedit
- fromreportlab.graphics.charts.piechartsimportPie
- fromreportlab.lib.colorsimportblack,red,purple,green,maroon,brown,pink,white,HexColor
- fromreportlab.graphics.charts.legendsimportLegend
- fromreportlab.graphics.shapesimportDrawing,_DrawingEditorMixin
- fromreportlab.lib.validatorsimportAuto
- fromreportlab.lib.colorsimportHexColor,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"),
- ]
- defsetItems(n,obj,attr,values):
- m=len(values)
- i=m//n
- forjinxrange(n):
- setattr(obj[j],attr,values[j*i%m])
- classBreakdownPieDrawing(_DrawingEditorMixin,Drawing):
- def__init__(self,width=400,height=200,*args,**kw):
- apply(Drawing.__init__,(self,width,height)+args,kw)
- #addingapiecharttothedrawing
- 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','HealthCare','Telecoms','Consumer','Consumer2','Industrials','Materials','Other','LiquidAssets']
- 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
- #addinglegend
- 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]))foriinxrange(n)]
- if__name__=="__main__":#NORUNTESTS
- drawing=BreakdownPieDrawing()
- #thedrawingwillbesavedaspdfandpngbelow,youcoulddootherthingswithitobviously.
- 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)