bar_chart.py 4.0 KB

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