Source code

Download source archive ↓

src/openpyxl/chart/bar_chart.py

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