Guest User

Untitled

a guest
Aug 14th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. Adding date labels to a reportlab LinePlot chart
  2. import reportlab
  3. from advisor.charting.Font import Font
  4.  
  5. from reportlab.lib.colors import Color, HexColor
  6. from reportlab.lib.pagesizes import cm, inch
  7. from reportlab.graphics.charts.legends import Legend
  8. from reportlab.graphics.charts.textlabels import Label
  9. from reportlab.graphics.charts.linecharts import HorizontalLineChart
  10. from reportlab.graphics.charts.lineplots import LinePlot
  11. from reportlab.graphics.shapes import Drawing, String, _DrawingEditorMixin
  12. from reportlab.graphics.widgets.markers import makeMarker
  13.  
  14.  
  15. class TacticalAugLineGraph(_DrawingEditorMixin, Drawing):
  16. def __init__(self, width=100, height=110, legend=False, *args, **kw):
  17. apply(Drawing.__init__, (self, width, height) + args, kw)
  18.  
  19. chartFont = Font('Gotham-Bold')
  20.  
  21. self._add(self, LinePlot(), name='chart', validate=None, desc=None)
  22. self.chart._inFill = 1
  23. self.chart.x = 20
  24. self.chart.y = 15
  25. self.chart.width = 85
  26. self.chart.height = 95
  27.  
  28. #self.chart.lineLabelFormat = '%d%%'
  29. self.chart.yValueAxis.valueMin = 0
  30. self.chart.yValueAxis.valueMax = 100
  31. self.chart.yValueAxis.valueStep = 10
  32.  
  33. def apply_colors(self, colors):
  34.  
  35. self.chart.lines[0].strokeColor = colors[0]
  36. self.chart.lines[1].strokeColor = colors[1]
  37. self.chart.lines[2].strokeColor = colors[2]
  38. self.chart.lines[3].strokeColor = colors[3]
  39.  
  40. #!/usr/bin/python
  41. from reportlab.graphics.charts.lineplots import LinePlot
  42. from reportlab.graphics.shapes import Drawing
  43. from reportlab.lib import colors
  44. from random import randint
  45. from datetime import date, timedelta
  46.  
  47. # Generate some testdata
  48. data = [
  49. [(x,randint(90,100)) for x in range(0,2001,100)],
  50. [(x,randint(30,80)) for x in range(0,2001,100)],
  51. [(x,randint(5,20)) for x in range(0,2001,100)],
  52. ]
  53.  
  54. # Create the drawing and the lineplot
  55. drawing = Drawing(400, 200)
  56. lp = LinePlot()
  57. lp.x = 50
  58. lp.y = 50
  59. lp.height = 125
  60. lp.width = 300
  61. lp._inFill = 1
  62. lp.data = data
  63. for i in range(len(data)):
  64. lp.lines[i].strokeColor = colors.toColor('hsl(%s,80%%,40%%)'%(i*60))
  65.  
  66. # Specify where the labels should be
  67. lp.xValueAxis.valueSteps = [5, 500, 1402, 1988]
  68. # Create a formatter that takes the value and format it however you like.
  69. def formatter(val):
  70. #return val
  71. #return 'x=%s'%val
  72. return (date(2010,1,1) + timedelta(val)).strftime('%Y-%m-%d')
  73.  
  74. # Use the formatter
  75. lp.xValueAxis.labelTextFormat = formatter
  76. drawing.add(lp)
  77.  
  78. from reportlab.graphics import renderPDF
  79. renderPDF.drawToFile(drawing, 'example.pdf', 'lineplot with dates')
Add Comment
Please, Sign In to add comment