areas.py 4.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #Copyright ReportLab Europe Ltd. 2000-2017
  2. #see license.txt for license details
  3. #history https://hg.reportlab.com/hg-public/reportlab/log/tip/src/reportlab/graphics/charts/areas.py
  4. __version__='3.3.0'
  5. __doc__='''This module defines a Area mixin classes'''
  6. from reportlab.lib.validators import isNumber, isColor, isColorOrNone, isNoneOrShape
  7. from reportlab.graphics.widgetbase import Widget
  8. from reportlab.graphics.shapes import Rect, Group, Line, Polygon
  9. from reportlab.lib.attrmap import AttrMap, AttrMapValue
  10. from reportlab.lib.colors import grey
  11. class PlotArea(Widget):
  12. "Abstract base class representing a chart's plot area, pretty unusable by itself."
  13. _attrMap = AttrMap(
  14. x = AttrMapValue(isNumber, desc='X position of the lower-left corner of the chart.'),
  15. y = AttrMapValue(isNumber, desc='Y position of the lower-left corner of the chart.'),
  16. width = AttrMapValue(isNumber, desc='Width of the chart.'),
  17. height = AttrMapValue(isNumber, desc='Height of the chart.'),
  18. strokeColor = AttrMapValue(isColorOrNone, desc='Color of the plot area border.'),
  19. strokeWidth = AttrMapValue(isNumber, desc='Width plot area border.'),
  20. fillColor = AttrMapValue(isColorOrNone, desc='Color of the plot area interior.'),
  21. background = AttrMapValue(isNoneOrShape, desc='Handle to background object e.g. Rect(0,0,width,height).'),
  22. debug = AttrMapValue(isNumber, desc='Used only for debugging.'),
  23. )
  24. def __init__(self):
  25. self.x = 20
  26. self.y = 10
  27. self.height = 85
  28. self.width = 180
  29. self.strokeColor = None
  30. self.strokeWidth = 1
  31. self.fillColor = None
  32. self.background = None
  33. self.debug = 0
  34. def makeBackground(self):
  35. if self.background is not None:
  36. BG = self.background
  37. if isinstance(BG,Group):
  38. g = BG
  39. for bg in g.contents:
  40. bg.x = self.x
  41. bg.y = self.y
  42. bg.width = self.width
  43. bg.height = self.height
  44. else:
  45. g = Group()
  46. if type(BG) not in (type(()),type([])): BG=(BG,)
  47. for bg in BG:
  48. bg.x = self.x
  49. bg.y = self.y
  50. bg.width = self.width
  51. bg.height = self.height
  52. g.add(bg)
  53. return g
  54. else:
  55. strokeColor,strokeWidth,fillColor=self.strokeColor, self.strokeWidth, self.fillColor
  56. if (strokeWidth and strokeColor) or fillColor:
  57. g = Group()
  58. _3d_dy = getattr(self,'_3d_dy',None)
  59. x = self.x
  60. y = self.y
  61. h = self.height
  62. w = self.width
  63. if _3d_dy is not None:
  64. _3d_dx = self._3d_dx
  65. if fillColor and not strokeColor:
  66. from reportlab.lib.colors import Blacker
  67. c = Blacker(fillColor, getattr(self,'_3d_blacken',0.7))
  68. else:
  69. c = strokeColor
  70. if not strokeWidth: strokeWidth = 0.5
  71. if fillColor or strokeColor or c:
  72. bg = Polygon([x,y,x,y+h,x+_3d_dx,y+h+_3d_dy,x+w+_3d_dx,y+h+_3d_dy,x+w+_3d_dx,y+_3d_dy,x+w,y],
  73. strokeColor=strokeColor or c or grey, strokeWidth=strokeWidth, fillColor=fillColor)
  74. g.add(bg)
  75. g.add(Line(x,y,x+_3d_dx,y+_3d_dy, strokeWidth=0.5, strokeColor=c))
  76. g.add(Line(x+_3d_dx,y+_3d_dy, x+_3d_dx,y+h+_3d_dy,strokeWidth=0.5, strokeColor=c))
  77. fc = Blacker(c, getattr(self,'_3d_blacken',0.8))
  78. g.add(Polygon([x,y,x+_3d_dx,y+_3d_dy,x+w+_3d_dx,y+_3d_dy,x+w,y],
  79. strokeColor=strokeColor or c or grey, strokeWidth=strokeWidth, fillColor=fc))
  80. bg = Line(x+_3d_dx,y+_3d_dy, x+w+_3d_dx,y+_3d_dy,strokeWidth=0.5, strokeColor=c)
  81. else:
  82. bg = None
  83. else:
  84. bg = Rect(x, y, w, h,
  85. strokeColor=strokeColor, strokeWidth=strokeWidth, fillColor=fillColor)
  86. if bg: g.add(bg)
  87. return g
  88. else:
  89. return None