"Pie with nested legend" from reportlab.graphics.charts.legends import Legend from reportlab.graphics.charts.piecharts import Pie from reportlab.graphics.shapes import Drawing, _DrawingEditorMixin from reportlab.lib.colors import black, white, PCMYKColor class PieChart05(_DrawingEditorMixin,Drawing): ''' Chart Features -------------- - multiple columns with different headings. the legend.colorNamePairs is a list of tuples where is tuple is made of (color, text) if the legend row has no swatch then simple provide None as the value for the color [(red, ('Relative Value','41.6%')), (None, ('Mortgage','20')), ... (black, ('Events Driven','30.0%')), etc] ''' def __init__(self,width=400,height=200,*args,**kw): Drawing.__init__(self,width,height,*args,**kw) # font fontName = 'Helvetica' self._boldFontName = boldFontName = 'Helvetica' # chart self._add(self,Pie(),name='chart',validate=None,desc=None) #slices self.chart.slices.strokeColor = white self.chart.slices.strokeWidth = 0.25 #colours colorsList = [PCMYKColor(100,0,90,50),PCMYKColor(65.1,72.16,33.33,14.51),PCMYKColor(50.98,28.63,41.18,1.18),PCMYKColor(30.2,30.59,16.08,0),PCMYKColor(0,0,0,0),PCMYKColor(0,0,0,0),PCMYKColor(0,0,0,0)] for i, color in enumerate(colorsList[:4]): self.chart.slices[i].fillColor = color #legend self._add(self,Legend(),name='legend',validate=None,desc=None) self.legend.alignment ='right' self.legend.boxAnchor='nw' self.legend.fontName = fontName self.legend.fontSize = 6 self.legend.strokeWidth = 0 self.legend.strokeColor = None self.legend.dy = 6 self.legend.dx = 6 self.legend.dxTextSpace = 4 #space between swatch and text self.legend.deltay = 6 #space between legend rows self.legend.columnMaximum = 99 self.legend.variColumn = 1 # self.legend.deltax = 175 #sample data self.chart.data = [41.600000000000001, 29.999999999999996, 26.400000000000002, 2.0] self.chart.width = 150 self.chart.height = 150 self.chart.x = 200 self.chart.y = 25 self.chart.slices[0].fillColor = PCMYKColor(100,60,0,50,alpha=100) self.chart.slices[1].fillColor = PCMYKColor(100,0,90,50,alpha=100) self.chart.slices[2].fillColor = PCMYKColor(0,100,100,40,alpha=100) self.chart.slices[3].fillColor = PCMYKColor(66,13,0,22,alpha=100) self.legend.colorNamePairs = [(PCMYKColor(100,60,0,50,alpha=100), (u'Relative Value', '41.6%')), (None, (u' Mortgage Arbitrage', '10.2%')), (None, (u' Relative Value Credit', '8.7%')), (None, (u' Equity L/S High Hedge', '6.1%')), (None, (u' Convertible Arbitrage', '4.9%')), (None, (u' Fixed Income Arbitrage', '3.9%')), (None, (u' Statistical Arbitrage', '3.1%')), (None, (u' Volatility Arbitrage', '2.5%')), (None, (u' Closed End Fund', '1.7%')), (None, (u' Other Arbitrage', '0.5%')), (PCMYKColor(100,0,90,50,alpha=100), (u'Event Driven', '30.0%')), (None, (u' Restructurings and Value', '13.8%')), (None, (u' Merger/Risk Arbitrage', '5.6%')), (None, (u' Event Driven Credit', '4.9%')), (None, (u' Distressed', '3.7%')), (None, (u' Private Placements', '2.0%')), (PCMYKColor(0,100,100,40,alpha=100), (u'Opportunistic', '26.4%')), (None, (u' Equity L/S Opportunistic', '18.8%')), (None, (u' Macro', '3.7%')), (None, (u' Other Opportunistic', '2.1%')), (None, (u' CTA/Managed Futures', '1.8%')), (PCMYKColor(66,13,0,22,alpha=100), (u'Cash', '2.0%'))] self.legend.y = 185 self.legend.x = 12 if __name__=="__main__": #NORUNTESTS PieChart05().save(formats=['pdf'],outDir='.',fnRoot=None)