Repository navigation
Expand file tree
/
Copy pathplot.py
More file actions
345 lines (287 loc) · 8.98 KB
/
Copy pathplot.py
File metadata and controls
345 lines (287 loc) · 8.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
"""Plot-related oxml objects."""
from __future__ import annotations
from pptx.oxml.chart.datalabel import CT_DLbls
from pptx.oxml.simpletypes import (
ST_BarDir,
ST_BubbleScale,
ST_GapAmount,
ST_Grouping,
ST_Overlap,
)
from pptx.oxml.xmlchemy import (
BaseOxmlElement,
OneAndOnlyOne,
OptionalAttribute,
ZeroOrMore,
ZeroOrOne,
)
class BaseChartElement(BaseOxmlElement):
"""
Base class for barChart, lineChart, and other plot elements.
"""
@property
def cat(self):
"""
Return the `c:cat` element of the first series in this xChart, or
|None| if not present.
"""
cats = self.xpath("./c:ser[1]/c:cat")
return cats[0] if cats else None
@property
def cat_pt_count(self):
"""
Return the value of the `c:ptCount` descendent of this xChart
element. Its parent can be one of three element types. This value
represents the true number of (leaf) categories, although they might
not all have a corresponding `c:pt` sibling; a category with no label
does not get a `c:pt` element. Returns 0 if there is no `c:ptCount`
descendent.
"""
cat_ptCounts = self.xpath("./c:ser//c:cat//c:ptCount")
if not cat_ptCounts:
return 0
return cat_ptCounts[0].val
@property
def cat_pts(self):
"""
Return a sequence representing the `c:pt` elements under the `c:cat`
element of the first series in this xChart element. A category having
no value will have no corresponding `c:pt` element; |None| will
appear in that position in such cases. Items appear in `idx` order.
Only those in the first ``<c:lvl>`` element are included in the case
of multi-level categories.
"""
cat_pts = self.xpath("./c:ser[1]/c:cat//c:lvl[1]/c:pt")
if not cat_pts:
cat_pts = self.xpath("./c:ser[1]/c:cat//c:pt")
cat_pt_dict = dict((pt.idx, pt) for pt in cat_pts)
return [cat_pt_dict.get(idx, None) for idx in range(self.cat_pt_count)]
@property
def grouping_val(self):
"""
Return the value of the ``./c:grouping{val=?}`` attribute, taking
defaults into account when items are not present.
"""
grouping = self.grouping
if grouping is None:
return ST_Grouping.STANDARD
val = grouping.val
if val is None:
return ST_Grouping.STANDARD
return val
def iter_sers(self):
"""
Generate each ``<c:ser>`` child element in this xChart in
c:order/@val sequence (not document or c:idx order).
"""
def ser_order(ser):
return ser.order.val
return (ser for ser in sorted(self.xpath("./c:ser"), key=ser_order))
@property
def sers(self):
"""
Sequence of ``<c:ser>`` child elements in this xChart in c:order/@val
sequence (not document or c:idx order).
"""
return tuple(self.iter_sers())
def _new_dLbls(self):
return CT_DLbls.new_dLbls()
class CT_Area3DChart(BaseChartElement):
"""
``<c:area3DChart>`` element.
"""
grouping = ZeroOrOne(
"c:grouping",
successors=(
"c:varyColors",
"c:ser",
"c:dLbls",
"c:dropLines",
"c:gapDepth",
"c:axId",
),
)
class CT_AreaChart(BaseChartElement):
"""
``<c:areaChart>`` element.
"""
_tag_seq = (
"c:grouping",
"c:varyColors",
"c:ser",
"c:dLbls",
"c:dropLines",
"c:axId",
"c:extLst",
)
grouping = ZeroOrOne("c:grouping", successors=_tag_seq[1:])
varyColors = ZeroOrOne("c:varyColors", successors=_tag_seq[2:])
ser = ZeroOrMore("c:ser", successors=_tag_seq[3:])
dLbls = ZeroOrOne("c:dLbls", successors=_tag_seq[4:])
del _tag_seq
class CT_BarChart(BaseChartElement):
"""
``<c:barChart>`` element.
"""
_tag_seq = (
"c:barDir",
"c:grouping",
"c:varyColors",
"c:ser",
"c:dLbls",
"c:gapWidth",
"c:overlap",
"c:serLines",
"c:axId",
"c:extLst",
)
barDir = OneAndOnlyOne("c:barDir")
grouping = ZeroOrOne("c:grouping", successors=_tag_seq[2:])
varyColors = ZeroOrOne("c:varyColors", successors=_tag_seq[3:])
ser = ZeroOrMore("c:ser", successors=_tag_seq[4:])
dLbls = ZeroOrOne("c:dLbls", successors=_tag_seq[5:])
gapWidth = ZeroOrOne("c:gapWidth", successors=_tag_seq[6:])
overlap = ZeroOrOne("c:overlap", successors=_tag_seq[7:])
del _tag_seq
@property
def grouping_val(self):
"""
Return the value of the ``./c:grouping{val=?}`` attribute, taking
defaults into account when items are not present.
"""
grouping = self.grouping
if grouping is None:
return ST_Grouping.CLUSTERED
val = grouping.val
if val is None:
return ST_Grouping.CLUSTERED
return val
class CT_BarDir(BaseOxmlElement):
"""
``<c:barDir>`` child of a barChart element, specifying the orientation of
the bars, 'bar' if they are horizontal and 'col' if they are vertical.
"""
val = OptionalAttribute("val", ST_BarDir, default=ST_BarDir.COL)
class CT_BubbleChart(BaseChartElement):
"""
``<c:bubbleChart>`` custom element class
"""
_tag_seq = (
"c:varyColors",
"c:ser",
"c:dLbls",
"c:axId",
"c:bubble3D",
"c:bubbleScale",
"c:showNegBubbles",
"c:sizeRepresents",
"c:axId",
"c:extLst",
)
ser = ZeroOrMore("c:ser", successors=_tag_seq[2:])
dLbls = ZeroOrOne("c:dLbls", successors=_tag_seq[3:])
bubble3D = ZeroOrOne("c:bubble3D", successors=_tag_seq[5:])
bubbleScale = ZeroOrOne("c:bubbleScale", successors=_tag_seq[6:])
del _tag_seq
class CT_BubbleScale(BaseChartElement):
"""
``<c:bubbleScale>`` custom element class
"""
val = OptionalAttribute("val", ST_BubbleScale, default=100)
class CT_DoughnutChart(BaseChartElement):
"""
``<c:doughnutChart>`` element.
"""
_tag_seq = (
"c:varyColors",
"c:ser",
"c:dLbls",
"c:firstSliceAng",
"c:holeSize",
"c:extLst",
)
varyColors = ZeroOrOne("c:varyColors", successors=_tag_seq[1:])
ser = ZeroOrMore("c:ser", successors=_tag_seq[2:])
dLbls = ZeroOrOne("c:dLbls", successors=_tag_seq[3:])
del _tag_seq
class CT_GapAmount(BaseOxmlElement):
"""
``<c:gapWidth>`` child of ``<c:barChart>`` element, also used for other
purposes like error bars.
"""
val = OptionalAttribute("val", ST_GapAmount, default=150)
class CT_Grouping(BaseOxmlElement):
"""
``<c:grouping>`` child of an xChart element, specifying a value like
'clustered' or 'stacked'. Also used for variants with the same tag name
like CT_BarGrouping.
"""
val = OptionalAttribute("val", ST_Grouping)
class CT_LineChart(BaseChartElement):
"""
``<c:lineChart>`` custom element class
"""
_tag_seq = (
"c:grouping",
"c:varyColors",
"c:ser",
"c:dLbls",
"c:dropLines",
"c:hiLowLines",
"c:upDownBars",
"c:marker",
"c:smooth",
"c:axId",
"c:extLst",
)
grouping = ZeroOrOne("c:grouping", successors=(_tag_seq[1:]))
varyColors = ZeroOrOne("c:varyColors", successors=_tag_seq[2:])
ser = ZeroOrMore("c:ser", successors=_tag_seq[3:])
dLbls = ZeroOrOne("c:dLbls", successors=(_tag_seq[4:]))
del _tag_seq
class CT_Overlap(BaseOxmlElement):
"""
``<c:overlap>`` element specifying bar overlap as an integer percentage
of bar width, in range -100 to 100.
"""
val = OptionalAttribute("val", ST_Overlap, default=0)
class CT_PieChart(BaseChartElement):
"""
``<c:pieChart>`` custom element class
"""
_tag_seq = ("c:varyColors", "c:ser", "c:dLbls", "c:firstSliceAng", "c:extLst")
varyColors = ZeroOrOne("c:varyColors", successors=_tag_seq[1:])
ser = ZeroOrMore("c:ser", successors=_tag_seq[2:])
dLbls = ZeroOrOne("c:dLbls", successors=_tag_seq[3:])
del _tag_seq
class CT_RadarChart(BaseChartElement):
"""
``<c:radarChart>`` custom element class
"""
_tag_seq = (
"c:radarStyle",
"c:varyColors",
"c:ser",
"c:dLbls",
"c:axId",
"c:extLst",
)
varyColors = ZeroOrOne("c:varyColors", successors=_tag_seq[2:])
ser = ZeroOrMore("c:ser", successors=_tag_seq[3:])
dLbls = ZeroOrOne("c:dLbls", successors=(_tag_seq[4:]))
del _tag_seq
class CT_ScatterChart(BaseChartElement):
"""
``<c:scatterChart>`` custom element class
"""
_tag_seq = (
"c:scatterStyle",
"c:varyColors",
"c:ser",
"c:dLbls",
"c:axId",
"c:extLst",
)
varyColors = ZeroOrOne("c:varyColors", successors=_tag_seq[2:])
ser = ZeroOrMore("c:ser", successors=_tag_seq[3:])
del _tag_seq