"Chart with background color" from reportlab.lib.colors import purple, PCMYKColor, black, pink, green, blue from reportlab.graphics.charts.lineplots import LinePlot from reportlab.graphics.charts.legends import LineLegend from reportlab.graphics.shapes import Drawing, _DrawingEditorMixin, Rect from reportlab.lib.validators import Auto from reportlab.pdfbase.pdfmetrics import stringWidth, EmbeddedType1Face, registerTypeFace, Font, registerFont from reportlab.graphics.charts.axes import XValueAxis, YValueAxis, AdjYValueAxis, NormalDateXValueAxis class LineChart02(_DrawingEditorMixin,Drawing): ''' Chart Features ============ There is a canvas background which is a light blue rectangle with same size and dimension of the drawing: - **background.fillColor** sets the CMYK fill color - **background.height**, **background.width**, **background.x**, and **background.y** define the size and position of the fill. - **background.strokeColor** sets the color of the dotted border. - **background.strokeDashArray** sets the type of dash. (3,2) gives the relative lengths of the filled and empty space. - **background.strokeWidth** gives the weight of the border - Note also that **legend.colorNamePairs** is set automatically based on the colors and names of the lines. This makes a lot of sense compared to other examples in which these are defined separately and may differ from the lines they are associated with. ''' def __init__(self,width=400,height=200,*args,**kw): Drawing.__init__(self,width,height,*args,**kw) fontName = 'Helvetica' self.width = 298 self.height = 164 self._add(self,LinePlot(),name='chart',validate=None,desc=None) self.chart.xValueAxis = NormalDateXValueAxis() self.background = Rect(0, 0, self.width, self.height, strokeWidth=0, fillColor=PCMYKColor(0,0,10,0)) self._add(self,LineLegend(),name='legend',validate=None,desc=None) self.chart.yValueAxis.visibleGrid = 1 self.chart.yValueAxis.visibleAxis = 0 self.chart.yValueAxis.visibleTicks = 0 self.chart.yValueAxis.labels.fontName = fontName self.chart.yValueAxis.labels.fontSize = 8 self.chart.yValueAxis.labelTextFormat = '%0.0f' self.chart.yValueAxis.strokeWidth = 0 self.chart.yValueAxis.gridStrokeWidth = 0.25 self.chart.yValueAxis.labels.rightPadding = 5 self.chart.yValueAxis.maximumTicks = 15 self.chart.yValueAxis.rangeRound ='both' self.chart.yValueAxis.avoidBoundFrac = 0.1 self.chart.yValueAxis.labels.dx = 3 self.chart.yValueAxis.forceZero = 1 self.chart.xValueAxis.labels.boxAnchor = 'autox' self.chart.xValueAxis.xLabelFormat = '{yy}' self.chart.xValueAxis.labels.fontName = fontName self.chart.xValueAxis.labels.fontSize = 8 self.chart.xValueAxis.strokeWidth = 0 self.chart.xValueAxis.visibleAxis = 0 self.chart.xValueAxis.visibleTicks = 0 self.chart.xValueAxis.labels.rightPadding = 2 self.chart.xValueAxis.maximumTicks = 30 self.chart.xValueAxis.forceFirstDate = 1 self.legend.colorNamePairs = Auto(obj=self.chart) self.legend.x = 6 self.legend.y = 30 self.legend.dx = 6 self.legend.dy = 10 self.legend.fontName = fontName self.legend.fontSize = 8 self.legend.alignment ='right' self.legend.columnMaximum = 3 self.legend.dxTextSpace = 4 self.legend.variColumn = 1 self.legend.boxAnchor = 'nw' self.legend.deltay = 10 self.legend.autoXPadding = 65 # sample data self.chart.data = [[(19910301, 94.260000000000005), (19910301, 102.25), (19920301, 101.47), (19930301, 134.84999999999999), (19940301, 121.52), (19950301, 145.87), (19960301, 166.0), (19970301, 172.91), (19980301, 212.83000000000001), (19990301, 391.86000000000001), (20000301, 267.38999999999999), (20010301, 218.22), (20020301, 161.50999999999999), (20030301, 211.46000000000001), (20040301, 245.16), (20050301, 278.97000000000003), (20060301, 337.45999999999998), (20070301, 392.66000000000003), (20080301, 357.25)], [(19910301, 94.319999999999993), (19910301, 100.36), (19920301, 87.530000000000001), (19930301, 109.78), (19940301, 114.98999999999999), (19950301, 128.38999999999999), (19960301, 133.13999999999999), (19970301, 136.24000000000001), (19980301, 166.84999999999999), (19990301, 216.41), (20000301, 163.62), (20010301, 123.68000000000001), (20020301, 104.19), (20030301, 138.03), (20040301, 160.78), (20050301, 182.69999999999999), (20060301, 215.40000000000001), (20070301, 250.84), (20080301, 230.37)]] sNames = 'Shell Transport & Trading', 'Liberty International' for i in range(len(self.chart.data)): self.chart.lines[i].name=sNames[i] self.chart.lines[0].strokeColor = PCMYKColor(100,0,90,50,alpha=100) self.chart.lines[1].strokeColor = PCMYKColor(0,100,100,40,alpha=100) self.chart.xValueAxis.subGridStrokeColor = black self.chart.yValueAxis.gridStrokeColor = PCMYKColor(100,100,100,100,alpha=100) self.background.strokeColor = black self.chart.strokeWidth = 1 self.chart.yValueAxis.visible = 1 self.chart.yValueAxis.strokeColor = black self.chart.yValueAxis.labelTextScale = 1 self.chart.yValueAxis.labels.fillColor = black self.background.fillOpacity = 0.5 self.background.strokeWidth = 3 self.background.strokeDashArray = (3, 2) self.height = 164 self.chart.width = 265 self.chart.height = 95 self.chart.y = 55 self.chart.x = 23 self.chart.lines.strokeWidth = 2 self.background.fillColor = PCMYKColor(66,13,0,22,alpha=30) self.background.x = 0 if __name__=="__main__": #NORUNTESTS LineChart02().save(formats=['pdf'],outDir='.',fnRoot=None)