"Bar chart with table" from reportlab.lib.colors import PCMYKColor, Color, CMYKColor, black from reportlab.graphics.charts.legends import Legend from reportlab.graphics.shapes import Drawing, _DrawingEditorMixin, String, Line from reportlab.lib.validators import Auto from reportlab.lib import colors from reportlab.pdfbase.pdfmetrics import stringWidth, EmbeddedType1Face, registerTypeFace, Font, registerFont from reportlab.graphics.widgets.table import TableWidget from reportlab.lib.formatters import DecimalFormatter from reportlab.graphics.charts.barcharts import VerticalBarChart from reportlab.graphics.charts.textlabels import Label class BarChart06(_DrawingEditorMixin,Drawing): ''' Chart Features -------------- We have added an experimental table legend to our open source library. This table legend can align its column values with another widget's columns (or bars, etc ...): - The bars are filled with one color and stroked with black color. - Bar labels are not displayed, you could achieve that through either setting the **barLabels.visible** to False, or not providing a formatting function (or any python callable) to the barLabelFormat attribute. - Simple category axis - dates are not necessary here. This chart was built with our [Diagra](http://www.reportlab.com/software/diagra/) solution. Not the kind of chart you looking for? Go [up](..) for more charts, or to see different types of charts click on [ReportLab Charts Gallery](/chartgallery/). ''' def __init__(self,width=403,height=163,*args,**kw): Drawing.__init__(self,width,height,*args,**kw) fontName = 'Helvetica' fontSize = 5 bFontName = 'Times-Bold' bFontSize = 7 colorsList = [PCMYKColor(0, 73, 69, 56), PCMYKColor(0, 3, 7, 6),PCMYKColor(41, 25, 0, 21)] self._add(self,VerticalBarChart(),name='chart',validate=None,desc=None) self.chart.height = 73 self.chart.fillColor = None self.chart.data = [[7.7199999999999998, 7.9400000000000004, 9.1699999999999999, 7.04, 7.7199999999999998, 8.1699999999999999], [4.46, 1.97, 13.220000000000001, 10.49, 8.5800000000000001, 10.74], [5.1399999999999997, 9.5999999999999996, 5.3099999999999996, 4.4699999999999998, 3.5099999999999998, 4.8399999999999999]] #self.chart.bars.fillColor = color self.chart.bars.strokeWidth = 0.5 self.chart.bars.strokeColor = PCMYKColor(0,0,0,100) #self.chart.barLabels.angle = 90 #self.chart.barLabelFormat = DecimalFormatter(2) #self.chart.barLabels.boxAnchor ='w' #self.chart.barLabels.boxFillColor = None #self.chart.barLabels.boxStrokeColor= None #self.chart.barLabels.fontName = fontName #self.chart.barLabels.fontSize = fontSize #self.chart.barLabels.dy = 3 #self.chart.barLabels.boxTarget = 'hi' #self.chart.annotations=[lambda c,cA,vA: Line(c.x,c.y,c.x+c.width,c.y,strokeColor=black,strokeWidth=0.5)] for i, color in enumerate(colorsList): self.chart.bars[i].fillColor = color self.chart.valueAxis.labels.fontName = fontName self.chart.valueAxis.labels.fontSize = fontSize self.chart.valueAxis.strokeDashArray = (5,0) self.chart.valueAxis.visibleGrid = False self.chart.valueAxis.visibleTicks = False self.chart.valueAxis.tickLeft = 0 self.chart.valueAxis.tickRight = 11 #self.chart.valueAxis.gridStrokeDashArray = (0,1,0) #self.chart.valueAxis.valueStep = 200 self.chart.valueAxis.strokeWidth = 0.25 #self.chart.valueAxis.gridStrokeWidth = 0.25 #self.chart.valueAxis.gridStrokeDashArray = (1,1,1) self.chart.valueAxis.avoidBoundFrac = 0#1#0.5 self.chart.valueAxis.rangeRound ='both' self.chart.valueAxis.gridStart = 13 self.chart.valueAxis.gridEnd = 342 self.chart.valueAxis.labelTextFormat = None #DecimalFormatter(1, suffix=None, prefix=None) self.chart.valueAxis.forceZero = True self.chart.valueAxis.labels.boxAnchor = 'e' self.chart.valueAxis.labels.dx = -1 self.chart.categoryAxis.strokeDashArray = (5,0) #self.chart.categoryAxis.gridStrokeDashArray = (1,1,1) self.chart.categoryAxis.visibleGrid = False self.chart.categoryAxis.visibleTicks = False self.chart.categoryAxis.strokeWidth = 0.25 self.chart.categoryAxis.tickUp = 5 self.chart.categoryAxis.tickDown = 0 self.chart.categoryAxis.labelAxisMode ='low' self.chart.categoryAxis.labels.textAnchor ='end' self.chart.categoryAxis.labels.fillColor = black self.chart.categoryAxis.labels.angle = 0 self.chart.categoryAxis.labels.fontName = bFontName self.chart.categoryAxis.labels.fontSize = bFontSize #self.chart.categoryAxis.tickShift = 1 #self.chart.categoryAxis.strokeDashArray = (0,1,0) # self.chart.categoryAxis.labels.boxAnchor = 'autox' self.chart.categoryAxis.labels.boxAnchor = 'e' self.chart.categoryAxis.labels.dx = 7#-10 self.chart.categoryAxis.labels.dy = -5 self._add(self,Legend(),name='legend',validate=None,desc=None) self.legend.deltay = 8 self.legend.fontName = fontName self.legend.fontSize = fontSize self.legend.strokeWidth = 0.5 self.legend.strokeColor = PCMYKColor(0,0,0,100) self.legend.autoXPadding = 0 self.legend.dy = 5 self.legend.variColumn = True self.legend.subCols.minWidth = self.chart.width/2 # 175 self.legend.colorNamePairs = Auto(obj=self.chart) self._add(self,TableWidget(),name='table',validate=None,desc=None) self.table.x = 0 self.table.y = 0 self.table.height = 45 self.table.borderStrokeColor = PCMYKColor(0, 12, 24, 36) self.table.fillColor = PCMYKColor(0, 3, 7, 6) self.table.borderStrokeWidth = 0.5 self.table.horizontalDividerStrokeColor = PCMYKColor(0, 12, 24, 36) self.table.verticalDividerStrokeColor = None self.table.horizontalDividerStrokeWidth = 0.5 self.table.verticalDividerStrokeWidth = 0 self.table.dividerDashArray = None self.table.data = [['BP', None, '7.72', '7.94', '9.17', '7.04', '7.72', '8.17'], ['Shell Transport & Trading', None, '4.46', '1.97', '13.22', '10.49', '8.58', '10.74'], ['Liberty International', None, '5.14', '9.60', '5.31', '4.47', '3.51', '4.84']] self.table.boxAnchor = 'sw' self.table.fontName = bFontName self.table.fontSize = bFontSize self.table.fontColor = colors.black self.table.alignment = 'left' self.table.textAnchor = 'start' for i in range(len(self.chart.data)): self.chart.bars[i].name = self.table.data[i][0] self.chart.categoryAxis.categoryNames = ['200%d'%i for i in range(1,7)] self.width = 400 self.table.width = 400 self.height = 200 self.legend.dx = 8 self.legend.dxTextSpace = 5 self.legend.deltax = 0 self.legend.alignment = 'right' self.legend.columnMaximum = 3 self.chart.y = 75 self.chart.barWidth = 2 self.chart.groupSpacing = 5 self.chart.width = 250 self.chart.barSpacing = 0.5 self.chart.x = 140 self.legend.y = 75 self.legend.boxAnchor = 'sw' self.legend.x = 24 self.chart.bars[0].fillColor = PCMYKColor(100,60,0,50,alpha=100) self.chart.bars[1].fillColor = PCMYKColor(23,51,0,4,alpha=100) self.chart.bars[2].fillColor = PCMYKColor(100,0,90,50,alpha=100) if __name__=="__main__": #NORUNTESTS BarChart06().save(formats=['pdf'],outDir='.',fnRoot=None)