series.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. from __future__ import absolute_import
  2. # Copyright (c) 2010-2019 openpyxl
  3. from openpyxl.compat import unicode
  4. from openpyxl.descriptors.serialisable import Serialisable
  5. from openpyxl.descriptors import (
  6. Typed,
  7. String,
  8. Integer,
  9. Bool,
  10. Alias,
  11. Sequence,
  12. )
  13. from openpyxl.descriptors.excel import ExtensionList
  14. from openpyxl.descriptors.nested import (
  15. NestedInteger,
  16. NestedBool,
  17. NestedNoneSet,
  18. NestedText,
  19. )
  20. from .shapes import GraphicalProperties
  21. from .data_source import (
  22. AxDataSource,
  23. NumDataSource,
  24. NumRef,
  25. StrRef,
  26. )
  27. from .error_bar import ErrorBars
  28. from .label import DataLabelList
  29. from .marker import DataPoint, PictureOptions, Marker
  30. from .trendline import Trendline
  31. attribute_mapping = {
  32. 'area': ('idx', 'order', 'tx', 'spPr', 'pictureOptions', 'dPt', 'dLbls', 'errBars',
  33. 'trendline', 'cat', 'val',),
  34. 'bar':('idx', 'order','tx', 'spPr', 'invertIfNegative', 'pictureOptions', 'dPt',
  35. 'dLbls', 'trendline', 'errBars', 'cat', 'val', 'shape'),
  36. 'bubble':('idx','order', 'tx', 'spPr', 'invertIfNegative', 'dPt', 'dLbls',
  37. 'trendline', 'errBars', 'xVal', 'yVal', 'bubbleSize', 'bubble3D'),
  38. 'line':('idx', 'order', 'tx', 'spPr', 'marker', 'dPt', 'dLbls', 'trendline',
  39. 'errBars', 'cat', 'val', 'smooth'),
  40. 'pie':('idx', 'order', 'tx', 'spPr', 'explosion', 'dPt', 'dLbls', 'cat', 'val'),
  41. 'radar':('idx', 'order', 'tx', 'spPr', 'marker', 'dPt', 'dLbls', 'cat', 'val'),
  42. 'scatter':('idx', 'order', 'tx', 'spPr', 'marker', 'dPt', 'dLbls', 'trendline',
  43. 'errBars', 'xVal', 'yVal', 'smooth'),
  44. 'surface':('idx', 'order', 'tx', 'spPr', 'cat', 'val'),
  45. }
  46. class SeriesLabel(Serialisable):
  47. tagname = "tx"
  48. strRef = Typed(expected_type=StrRef, allow_none=True)
  49. v = NestedText(expected_type=unicode, allow_none=True)
  50. value = Alias('v')
  51. __elements__ = ('strRef', 'v')
  52. def __init__(self,
  53. strRef=None,
  54. v=None):
  55. self.strRef = strRef
  56. self.v = v
  57. class Series(Serialisable):
  58. """
  59. Generic series object. Should not be instantiated directly.
  60. User the chart.Series factory instead.
  61. """
  62. tagname = "ser"
  63. idx = NestedInteger()
  64. order = NestedInteger()
  65. tx = Typed(expected_type=SeriesLabel, allow_none=True)
  66. title = Alias('tx')
  67. spPr = Typed(expected_type=GraphicalProperties, allow_none=True)
  68. graphicalProperties = Alias('spPr')
  69. # area chart
  70. pictureOptions = Typed(expected_type=PictureOptions, allow_none=True)
  71. dPt = Sequence(expected_type=DataPoint, allow_none=True)
  72. data_points = Alias("dPt")
  73. dLbls = Typed(expected_type=DataLabelList, allow_none=True)
  74. labels = Alias("dLbls")
  75. trendline = Typed(expected_type=Trendline, allow_none=True)
  76. errBars = Typed(expected_type=ErrorBars, allow_none=True)
  77. cat = Typed(expected_type=AxDataSource, allow_none=True)
  78. identifiers = Alias("cat")
  79. val = Typed(expected_type=NumDataSource, allow_none=True)
  80. extLst = Typed(expected_type=ExtensionList, allow_none=True)
  81. #bar chart
  82. invertIfNegative = NestedBool(allow_none=True)
  83. shape = NestedNoneSet(values=(['cone', 'coneToMax', 'box', 'cylinder', 'pyramid', 'pyramidToMax']))
  84. #bubble chart
  85. xVal = Typed(expected_type=AxDataSource, allow_none=True)
  86. yVal = Typed(expected_type=NumDataSource, allow_none=True)
  87. bubbleSize = Typed(expected_type=NumDataSource, allow_none=True)
  88. zVal = Alias("bubbleSize")
  89. bubble3D = NestedBool(allow_none=True)
  90. #line chart
  91. marker = Typed(expected_type=Marker, allow_none=True)
  92. smooth = NestedBool(allow_none=True)
  93. #pie chart
  94. explosion = NestedInteger(allow_none=True)
  95. __elements__ = ()
  96. def __init__(self,
  97. idx=0,
  98. order=0,
  99. tx=None,
  100. spPr=None,
  101. pictureOptions=None,
  102. dPt=(),
  103. dLbls=None,
  104. trendline=None,
  105. errBars=None,
  106. cat=None,
  107. val=None,
  108. invertIfNegative=None,
  109. shape=None,
  110. xVal=None,
  111. yVal=None,
  112. bubbleSize=None,
  113. bubble3D=None,
  114. marker=None,
  115. smooth=None,
  116. explosion=None,
  117. extLst=None,
  118. ):
  119. self.idx = idx
  120. self.order = order
  121. self.tx = tx
  122. if spPr is None:
  123. spPr = GraphicalProperties()
  124. self.spPr = spPr
  125. self.pictureOptions = pictureOptions
  126. self.dPt = dPt
  127. self.dLbls = dLbls
  128. self.trendline = trendline
  129. self.errBars = errBars
  130. self.cat = cat
  131. self.val = val
  132. self.invertIfNegative = invertIfNegative
  133. self.shape = shape
  134. self.xVal = xVal
  135. self.yVal = yVal
  136. self.bubbleSize = bubbleSize
  137. self.bubble3D = bubble3D
  138. if marker is None:
  139. marker = Marker()
  140. self.marker = marker
  141. self.smooth = smooth
  142. self.explosion = explosion
  143. def to_tree(self, tagname=None, idx=None):
  144. if idx is not None:
  145. if self.order == self.idx:
  146. self.order = idx
  147. self.idx = idx
  148. return super(Series, self).to_tree(tagname)
  149. class XYSeries(Series):
  150. """Dedicated series for charts that have x and y series"""
  151. idx = Series.idx
  152. order = Series.order
  153. tx = Series.tx
  154. spPr = Series.spPr
  155. dPt = Series.dPt
  156. dLbls = Series.dLbls
  157. trendline = Series.trendline
  158. errBars = Series.errBars
  159. xVal = Series.xVal
  160. yVal = Series.yVal
  161. invertIfNegative = Series.invertIfNegative
  162. bubbleSize = Series.bubbleSize
  163. bubble3D = Series.bubble3D
  164. marker = Series.marker
  165. smooth = Series.smooth