chartspace.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. # Copyright (c) 2010-2024 openpyxl
  2. """
  3. Enclosing chart object. The various chart types are actually child objects.
  4. Will probably need to call this indirectly
  5. """
  6. from openpyxl.descriptors.serialisable import Serialisable
  7. from openpyxl.descriptors import (
  8. Typed,
  9. String,
  10. Alias,
  11. )
  12. from openpyxl.descriptors.excel import (
  13. ExtensionList,
  14. Relation
  15. )
  16. from openpyxl.descriptors.nested import (
  17. NestedBool,
  18. NestedNoneSet,
  19. NestedString,
  20. NestedMinMax,
  21. )
  22. from openpyxl.descriptors.sequence import NestedSequence
  23. from openpyxl.xml.constants import CHART_NS
  24. from openpyxl.drawing.colors import ColorMapping
  25. from .text import RichText
  26. from .shapes import GraphicalProperties
  27. from .legend import Legend
  28. from ._3d import _3DBase
  29. from .plotarea import PlotArea
  30. from .title import Title
  31. from .pivot import (
  32. PivotFormat,
  33. PivotSource,
  34. )
  35. from .print_settings import PrintSettings
  36. class ChartContainer(Serialisable):
  37. tagname = "chart"
  38. title = Typed(expected_type=Title, allow_none=True)
  39. autoTitleDeleted = NestedBool(allow_none=True)
  40. pivotFmts = NestedSequence(expected_type=PivotFormat)
  41. view3D = _3DBase.view3D
  42. floor = _3DBase.floor
  43. sideWall = _3DBase.sideWall
  44. backWall = _3DBase.backWall
  45. plotArea = Typed(expected_type=PlotArea, )
  46. legend = Typed(expected_type=Legend, allow_none=True)
  47. plotVisOnly = NestedBool()
  48. dispBlanksAs = NestedNoneSet(values=(['span', 'gap', 'zero']))
  49. showDLblsOverMax = NestedBool(allow_none=True)
  50. extLst = Typed(expected_type=ExtensionList, allow_none=True)
  51. __elements__ = ('title', 'autoTitleDeleted', 'pivotFmts', 'view3D',
  52. 'floor', 'sideWall', 'backWall', 'plotArea', 'legend', 'plotVisOnly',
  53. 'dispBlanksAs', 'showDLblsOverMax')
  54. def __init__(self,
  55. title=None,
  56. autoTitleDeleted=None,
  57. pivotFmts=(),
  58. view3D=None,
  59. floor=None,
  60. sideWall=None,
  61. backWall=None,
  62. plotArea=None,
  63. legend=None,
  64. plotVisOnly=True,
  65. dispBlanksAs="gap",
  66. showDLblsOverMax=None,
  67. extLst=None,
  68. ):
  69. self.title = title
  70. self.autoTitleDeleted = autoTitleDeleted
  71. self.pivotFmts = pivotFmts
  72. self.view3D = view3D
  73. self.floor = floor
  74. self.sideWall = sideWall
  75. self.backWall = backWall
  76. if plotArea is None:
  77. plotArea = PlotArea()
  78. self.plotArea = plotArea
  79. self.legend = legend
  80. self.plotVisOnly = plotVisOnly
  81. self.dispBlanksAs = dispBlanksAs
  82. self.showDLblsOverMax = showDLblsOverMax
  83. class Protection(Serialisable):
  84. tagname = "protection"
  85. chartObject = NestedBool(allow_none=True)
  86. data = NestedBool(allow_none=True)
  87. formatting = NestedBool(allow_none=True)
  88. selection = NestedBool(allow_none=True)
  89. userInterface = NestedBool(allow_none=True)
  90. __elements__ = ("chartObject", "data", "formatting", "selection", "userInterface")
  91. def __init__(self,
  92. chartObject=None,
  93. data=None,
  94. formatting=None,
  95. selection=None,
  96. userInterface=None,
  97. ):
  98. self.chartObject = chartObject
  99. self.data = data
  100. self.formatting = formatting
  101. self.selection = selection
  102. self.userInterface = userInterface
  103. class ExternalData(Serialisable):
  104. tagname = "externalData"
  105. autoUpdate = NestedBool(allow_none=True)
  106. id = String() # Needs namespace
  107. def __init__(self,
  108. autoUpdate=None,
  109. id=None
  110. ):
  111. self.autoUpdate = autoUpdate
  112. self.id = id
  113. class ChartSpace(Serialisable):
  114. tagname = "chartSpace"
  115. date1904 = NestedBool(allow_none=True)
  116. lang = NestedString(allow_none=True)
  117. roundedCorners = NestedBool(allow_none=True)
  118. style = NestedMinMax(allow_none=True, min=1, max=48)
  119. clrMapOvr = Typed(expected_type=ColorMapping, allow_none=True)
  120. pivotSource = Typed(expected_type=PivotSource, allow_none=True)
  121. protection = Typed(expected_type=Protection, allow_none=True)
  122. chart = Typed(expected_type=ChartContainer)
  123. spPr = Typed(expected_type=GraphicalProperties, allow_none=True)
  124. graphical_properties = Alias("spPr")
  125. txPr = Typed(expected_type=RichText, allow_none=True)
  126. textProperties = Alias("txPr")
  127. externalData = Typed(expected_type=ExternalData, allow_none=True)
  128. printSettings = Typed(expected_type=PrintSettings, allow_none=True)
  129. userShapes = Relation()
  130. extLst = Typed(expected_type=ExtensionList, allow_none=True)
  131. __elements__ = ('date1904', 'lang', 'roundedCorners', 'style',
  132. 'clrMapOvr', 'pivotSource', 'protection', 'chart', 'spPr', 'txPr',
  133. 'externalData', 'printSettings', 'userShapes')
  134. def __init__(self,
  135. date1904=None,
  136. lang=None,
  137. roundedCorners=None,
  138. style=None,
  139. clrMapOvr=None,
  140. pivotSource=None,
  141. protection=None,
  142. chart=None,
  143. spPr=None,
  144. txPr=None,
  145. externalData=None,
  146. printSettings=None,
  147. userShapes=None,
  148. extLst=None,
  149. ):
  150. self.date1904 = date1904
  151. self.lang = lang
  152. self.roundedCorners = roundedCorners
  153. self.style = style
  154. self.clrMapOvr = clrMapOvr
  155. self.pivotSource = pivotSource
  156. self.protection = protection
  157. self.chart = chart
  158. self.spPr = spPr
  159. self.txPr = txPr
  160. self.externalData = externalData
  161. self.printSettings = printSettings
  162. self.userShapes = userShapes
  163. def to_tree(self, tagname=None, idx=None, namespace=None):
  164. tree = super().to_tree()
  165. tree.set("xmlns", CHART_NS)
  166. return tree