bar_chart.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. from __future__ import absolute_import
  2. # Copyright (c) 2010-2019 openpyxl
  3. from openpyxl.descriptors.serialisable import Serialisable
  4. from openpyxl.descriptors import (
  5. Typed,
  6. Bool,
  7. Integer,
  8. Sequence,
  9. Alias,
  10. )
  11. from openpyxl.descriptors.excel import ExtensionList
  12. from openpyxl.descriptors.nested import (
  13. NestedNoneSet,
  14. NestedSet,
  15. NestedBool,
  16. NestedInteger,
  17. NestedMinMax,
  18. )
  19. from .descriptors import (
  20. NestedGapAmount,
  21. NestedOverlap,
  22. )
  23. from ._chart import ChartBase
  24. from ._3d import _3DBase
  25. from .axis import TextAxis, NumericAxis, SeriesAxis, ChartLines
  26. from .shapes import GraphicalProperties
  27. from .series import Series
  28. from .legend import Legend
  29. from .label import DataLabelList
  30. class _BarChartBase(ChartBase):
  31. barDir = NestedSet(values=(['bar', 'col']))
  32. type = Alias("barDir")
  33. grouping = NestedSet(values=(['percentStacked', 'clustered', 'standard',
  34. 'stacked']))
  35. varyColors = NestedBool(nested=True, allow_none=True)
  36. ser = Sequence(expected_type=Series, allow_none=True)
  37. dLbls = Typed(expected_type=DataLabelList, allow_none=True)
  38. dataLabels = Alias("dLbls")
  39. __elements__ = ('barDir', 'grouping', 'varyColors', 'ser', 'dLbls')
  40. _series_type = "bar"
  41. def __init__(self,
  42. barDir="col",
  43. grouping="clustered",
  44. varyColors=None,
  45. ser=(),
  46. dLbls=None,
  47. **kw
  48. ):
  49. self.barDir = barDir
  50. self.grouping = grouping
  51. self.varyColors = varyColors
  52. self.ser = ser
  53. self.dLbls = dLbls
  54. super(_BarChartBase, self).__init__(**kw)
  55. class BarChart(_BarChartBase):
  56. tagname = "barChart"
  57. barDir = _BarChartBase.barDir
  58. grouping = _BarChartBase.grouping
  59. varyColors = _BarChartBase.varyColors
  60. ser = _BarChartBase.ser
  61. dLbls = _BarChartBase.dLbls
  62. gapWidth = NestedGapAmount()
  63. overlap = NestedOverlap()
  64. serLines = Typed(expected_type=ChartLines, allow_none=True)
  65. extLst = Typed(expected_type=ExtensionList, allow_none=True)
  66. # chart properties actually used by containing classes
  67. x_axis = Typed(expected_type=TextAxis)
  68. y_axis = Typed(expected_type=NumericAxis)
  69. __elements__ = _BarChartBase.__elements__ + ('gapWidth', 'overlap', 'serLines', 'axId')
  70. def __init__(self,
  71. gapWidth=150,
  72. overlap=None,
  73. serLines=None,
  74. extLst=None,
  75. **kw
  76. ):
  77. self.gapWidth = gapWidth
  78. self.overlap = overlap
  79. self.serLines = serLines
  80. self.x_axis = TextAxis()
  81. self.y_axis = NumericAxis()
  82. self.legend = Legend()
  83. super(BarChart, self).__init__(**kw)
  84. class BarChart3D(_BarChartBase, _3DBase):
  85. tagname = "bar3DChart"
  86. barDir = _BarChartBase.barDir
  87. grouping = _BarChartBase.grouping
  88. varyColors = _BarChartBase.varyColors
  89. ser = _BarChartBase.ser
  90. dLbls = _BarChartBase.dLbls
  91. view3D = _3DBase.view3D
  92. floor = _3DBase.floor
  93. sideWall = _3DBase.sideWall
  94. backWall = _3DBase.backWall
  95. gapWidth = NestedGapAmount()
  96. gapDepth = NestedGapAmount()
  97. shape = NestedNoneSet(values=(['cone', 'coneToMax', 'box', 'cylinder', 'pyramid', 'pyramidToMax']))
  98. serLines = Typed(expected_type=ChartLines, allow_none=True)
  99. extLst = Typed(expected_type=ExtensionList, allow_none=True)
  100. x_axis = Typed(expected_type=TextAxis)
  101. y_axis = Typed(expected_type=NumericAxis)
  102. z_axis = Typed(expected_type=SeriesAxis, allow_none=True)
  103. __elements__ = _BarChartBase.__elements__ + ('gapWidth', 'gapDepth', 'shape', 'serLines', 'axId')
  104. def __init__(self,
  105. gapWidth=150,
  106. gapDepth=150,
  107. shape=None,
  108. serLines=None,
  109. extLst=None,
  110. **kw
  111. ):
  112. self.gapWidth = gapWidth
  113. self.gapDepth = gapDepth
  114. self.shape = shape
  115. self.serLines = serLines
  116. self.x_axis = TextAxis()
  117. self.y_axis = NumericAxis()
  118. self.z_axis = SeriesAxis()
  119. super(BarChart3D, self).__init__(**kw)