Source code

Download source archive ↓

src/openpyxl/chart/series.py

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