"Dashed lines and number formats" from reportlab.graphics.charts.lineplots import LinePlot from reportlab.lib.colors import PCMYKColor from reportlab.graphics.charts.legends import LineLegend from reportlab.graphics.shapes import Drawing, _DrawingEditorMixin, Line from reportlab.lib.validators import Auto from reportlab.lib.styles import black from reportlab.graphics.charts.axes import XValueAxis, YValueAxis, AdjYValueAxis, NormalDateXValueAxis class LineChart03(_DrawingEditorMixin,Drawing): ''' Chart features ============ - **chart.yValueAxis.labelTextScale = 1000:** Multiplies y-values in the data by a scaling factor of 1000 - **chart.xValueAxis.xLabelFormat = '{mm}/{dd}/{yy}':** Sets the date format to display on the x-axis. The x-axis accepts data values as integers in the format yyyymmdd - **chart.xValueAxis.labels.angle = 90:** Rotates the x-axis labels such that they are vertical - **chart.yValueAxis.labelTextFormat = '$%0.2f':** Describes the formatting of the y-axis; note the addition of the '$' sign - **chart.lines[0].strokeDashArray = (1, 1):** Gives the dash pattern of the line, lengths in points, alternating between filled and empty space - **chart.lines[1].strokeDashArray = (6, 2):** Gives the dash pattern of the line, lengths in points, alternating between filled and empty space - **chart.lines[2].strokeDashArray = (2, 2, 2, 2, 10):** Gives the dash pattern of the line, lengths in points, alternating between filled and empty space ''' def __init__(self,width=558,height=140,*args,**kw): Drawing.__init__(self,width,height,*args,**kw) # font fontName = 'Helvetica' # common values strokeWidth = 0.5 dashArray = (0.3,1) lineCap = 1 overShoot = 7.5 # chart self._add(self,LinePlot(),name='chart',validate=None,desc=None) # colours colorsList = [PCMYKColor(65.1,72.16,33.33,14.51),PCMYKColor(50.98,28.63,41.18,1.18),PCMYKColor(0,0,0,100),PCMYKColor(0,0,0,0),PCMYKColor(0,0,0,0),PCMYKColor(0,0,0,0)] for i, color in enumerate(colorsList[:3]): self.chart.lines[i].strokeColor = color # x axis self.chart.xValueAxis = NormalDateXValueAxis() self.chart.xValueAxis.labels.fontName = fontName self.chart.xValueAxis.labels.fontSize = 6 self.chart.xValueAxis.labels.boxAnchor ='autox' self.chart.xValueAxis.labels.angle = 90 self.chart.xValueAxis.labels.rightPadding = 2 self.chart.xValueAxis.xLabelFormat = '{mm}/{dd}/{yy}' self.chart.xValueAxis.strokeDashArray = dashArray self.chart.xValueAxis.strokeLineCap = lineCap self.chart.xValueAxis.maximumTicks = 20 self.chart.xValueAxis.forceFirstDate = 1 self.chart.xValueAxis.dailyFreq =0 self.chart.xValueAxis.forceEndDate =1 self.chart.xValueAxis.minimumTickSpacing = 15 #self.chart.xValueAxis.niceMonth=0 # y axis #self.chart.yValueAxis = AdjYValueAxis() self.chart.yValueAxis.labels.fontName = fontName self.chart.yValueAxis.labels.fontSize = 6 self.chart.yValueAxis.labelTextFormat = '$%0.2f' self.chart.yValueAxis.labels.rightPadding = 7 self.chart.yValueAxis.strokeWidth = 0.5 self.chart.yValueAxis.strokeDashArray = dashArray self.chart.yValueAxis.strokeLineCap = lineCap #self.chart.yValueAxis.origShiftIPC = 1 self.chart.yValueAxis.maximumTicks = 15 self.chart.yValueAxis.rangeRound ='both' self.chart.yValueAxis.avoidBoundFrac = 0.1 self.chart.yValueAxis.gridStrokeWidth = strokeWidth self.chart.yValueAxis.gridStrokeDashArray = dashArray self.chart.yValueAxis.gridStrokeLineCap = lineCap self.chart.yValueAxis.visibleAxis = 0 self.chart.yValueAxis.visibleGrid = 1 # legend self._add(self,LineLegend(),name='legend',validate=None,desc=None) self.legend.colorNamePairs = Auto(obj=self.chart) self.legend.fontName = fontName self.legend.fontSize = 7 self.legend.alignment ='right' self.legend.columnMaximum = 1 self.legend.dxTextSpace = 5 self.legend.variColumn = 1 self.legend.autoXPadding = 15 self._add(self,Line(self.chart.x,self.chart.y,self.chart.x,self.chart.y+self.chart.height,strokeWidth=strokeWidth,strokeDashArray=dashArray,strokeLineCap=lineCap),name='L0',validate=None,desc=None) self._add(self,Line(self.chart.x+self.chart.width,self.chart.y,self.chart.x+self.chart.width,self.chart.y+self.chart.height,strokeWidth=strokeWidth,strokeDashArray=dashArray,strokeLineCap=lineCap),name='L1',validate=None,desc=None) self._add(self,Line(self.chart.x-overShoot,self.chart.y,self.chart.x+self.chart.width+overShoot,self.chart.y, strokeWidth=strokeWidth),name='L2',validate=None,desc=None) # sample data self.chart.data = [[(20070201, 1.0), (20070228, 1.0089999999999999), (20070331, 1.0264), (20070430, 1.0430999999999999), (20070531, 1.0649), (20070630, 1.0720000000000001), (20070731, 1.0742), (20070831, 1.0553999999999999), (20070930, 1.0713999999999999), (20071031, 1.1031), (20071130, 1.093), (20071231, 1.1005), (20080131, 1.0740000000000001)], [(20070201, 1.0), (20070228, 1.0154000000000001), (20070331, 1.0155000000000001), (20070430, 1.0208999999999999), (20070531, 1.0132000000000001), (20070630, 1.0101), (20070731, 1.0185), (20070831, 1.0309999999999999), (20070930, 1.0388999999999999), (20071031, 1.0482), (20071130, 1.0670999999999999), (20071231, 1.0701000000000001), (20080131, 1.0880000000000001)], [(20070201, 1.0), (20070228, 1.0089999999999999), (20070331, 1.0182), (20070430, 1.0311999999999999), (20070531, 1.0471999999999999), (20070630, 1.0518000000000001), (20070731, 1.0532999999999999), (20070831, 1.0344), (20070930, 1.0470999999999999), (20071031, 1.0699000000000001), (20071130, 1.0613999999999999), (20071231, 1.0621), (20080131, 1.0455000000000001)]] self._colorsList = [PCMYKColor(65.1,72.16,33.33,14.51,alpha=100), PCMYKColor(50.98,28.63,41.18,1.18,alpha=100), PCMYKColor(0,0,0,100,alpha=100), PCMYKColor(0,0,0,0,alpha=100), PCMYKColor(0,0,0,0,alpha=100), PCMYKColor(0,0,0,0,alpha=100)] sNames = 'Liberty International', 'Persimmon', 'Royal Bank of Scotland', for i in range(len(self.chart.data)): self.chart.lines[i].name = sNames[i] for i in range(len(self.chart.data)): self.chart.lines[i].strokeColor = self._colorsList[i] self.chart.lines[0].strokeColor = PCMYKColor(23,51,0,4,alpha=100) self.chart.lines[2].strokeColor = PCMYKColor(100,60,0,50,alpha=100) self.chart.lines[1].strokeColor = PCMYKColor(66,13,0,22,alpha=100) self.chart.lines.strokeWidth = 2 self.chart.lines[1].strokeDashArray = (6, 2) self.chart.lines[2].strokeDashArray = (2, 2, 2, 2, 10) self.chart.lines[0].strokeDashArray = (1, 1) self.chart.yValueAxis.labelTextScale = 1000 self.chart.x = 50 self.chart.xValueAxis.visible = 1 self.chart.xValueAxis.visibleGrid = 0 self.chart.xValueAxis.visibleTicks = 1 self.chart.xValueAxis.subGridStrokeWidth = 0.25 self.chart.xValueAxis.strokeWidth = 0.5 self.chart.xValueAxis.gridStrokeWidth = 0.25 self.width = 400 self.height = 200 self.chart.y = 50 self.chart.width = 300 self.chart.height = 100 self.L1.x1 = 350 self.L1.x2 = 350 self.L1.y1 = 50 self.L1.y2 = 150 self.L2.x1 = 50 self.L2.x2 = 350 self.L2.y1 = 50 self.L2.y2 = 50 self.L0.x1 = 50 self.L0.x2 = 50 self.L0.y1 = 50 self.L0.y2 = 150 self.L0.strokeWidth = 1 self.L1.strokeWidth = 1 self.L2.strokeWidth = 0.75 self.chart.yValueAxis.tickLeft = 0 self.legend.y = 175 self.legend.x = 100 if __name__=="__main__": #NORUNTESTS LineChart03().save(formats=['pdf'],outDir='.',fnRoot=None)