Source code

Download source archive ↓

src/openpyxl/chart/plotarea.py

1# Copyright (c) 2010-2024 openpyxl
2
3from openpyxl.descriptors.serialisable import Serialisable
4from openpyxl.descriptors import (
5    Typed,
6    Alias,
7)
8from openpyxl.descriptors.excel import (
9    ExtensionList,
10)
11from openpyxl.descriptors.sequence import (
12    MultiSequence,
13    MultiSequencePart,
14)
15from openpyxl.descriptors.nested import (
16    NestedBool,
17)
18
19from ._3d import _3DBase
20from .area_chart import AreaChart, AreaChart3D
21from .bar_chart import BarChart, BarChart3D
22from .bubble_chart import BubbleChart
23from .line_chart import LineChart, LineChart3D
24from .pie_chart import PieChart, PieChart3D, ProjectedPieChart, DoughnutChart
25from .radar_chart import RadarChart
26from .scatter_chart import ScatterChart
27from .stock_chart import StockChart
28from .surface_chart import SurfaceChart, SurfaceChart3D
29from .layout import Layout
30from .shapes import GraphicalProperties
31from .text import RichText
32
33from .axis import (
34    NumericAxis,
35    TextAxis,
36    SeriesAxis,
37    DateAxis,
38)
39
40
41class DataTable(Serialisable):
42
43    tagname = "dTable"
44
45    showHorzBorder = NestedBool(allow_none=True)
46    showVertBorder = NestedBool(allow_none=True)
47    showOutline = NestedBool(allow_none=True)
48    showKeys = NestedBool(allow_none=True)
49    spPr = Typed(expected_type=GraphicalProperties, allow_none=True)
50    graphicalProperties = Alias('spPr')
51    txPr = Typed(expected_type=RichText, allow_none=True)
52    extLst = Typed(expected_type=ExtensionList, allow_none=True)
53
54    __elements__ = ('showHorzBorder', 'showVertBorder', 'showOutline',
55                    'showKeys', 'spPr', 'txPr')
56
57    def __init__(self,
58                 showHorzBorder=None,
59                 showVertBorder=None,
60                 showOutline=None,
61                 showKeys=None,
62                 spPr=None,
63                 txPr=None,
64                 extLst=None,
65                ):
66        self.showHorzBorder = showHorzBorder
67        self.showVertBorder = showVertBorder
68        self.showOutline = showOutline
69        self.showKeys = showKeys
70        self.spPr = spPr
71        self.txPr = txPr
72
73
74class PlotArea(Serialisable):
75
76    tagname = "plotArea"
77
78    layout = Typed(expected_type=Layout, allow_none=True)
79    dTable = Typed(expected_type=DataTable, allow_none=True)
80    spPr = Typed(expected_type=GraphicalProperties, allow_none=True)
81    graphicalProperties = Alias("spPr")
82    extLst = Typed(expected_type=ExtensionList, allow_none=True)
83
84    # at least one chart
85    _charts = MultiSequence()
86    areaChart = MultiSequencePart(expected_type=AreaChart, store="_charts")
87    area3DChart = MultiSequencePart(expected_type=AreaChart3D, store="_charts")
88    lineChart = MultiSequencePart(expected_type=LineChart, store="_charts")
89    line3DChart = MultiSequencePart(expected_type=LineChart3D, store="_charts")
90    stockChart = MultiSequencePart(expected_type=StockChart, store="_charts")
91    radarChart = MultiSequencePart(expected_type=RadarChart, store="_charts")
92    scatterChart = MultiSequencePart(expected_type=ScatterChart, store="_charts")
93    pieChart = MultiSequencePart(expected_type=PieChart, store="_charts")
94    pie3DChart = MultiSequencePart(expected_type=PieChart3D, store="_charts")
95    doughnutChart = MultiSequencePart(expected_type=DoughnutChart, store="_charts")
96    barChart = MultiSequencePart(expected_type=BarChart, store="_charts")
97    bar3DChart = MultiSequencePart(expected_type=BarChart3D, store="_charts")
98    ofPieChart = MultiSequencePart(expected_type=ProjectedPieChart, store="_charts")
99    surfaceChart = MultiSequencePart(expected_type=SurfaceChart, store="_charts")
100    surface3DChart = MultiSequencePart(expected_type=SurfaceChart3D, store="_charts")
101    bubbleChart = MultiSequencePart(expected_type=BubbleChart, store="_charts")
102
103    # axes
104    _axes = MultiSequence()
105    valAx = MultiSequencePart(expected_type=NumericAxis, store="_axes")
106    catAx = MultiSequencePart(expected_type=TextAxis, store="_axes")
107    dateAx = MultiSequencePart(expected_type=DateAxis, store="_axes")
108    serAx = MultiSequencePart(expected_type=SeriesAxis, store="_axes")
109
110    __elements__ = ('layout', '_charts', '_axes', 'dTable', 'spPr')
111
112    def __init__(self,
113                 layout=None,
114                 dTable=None,
115                 spPr=None,
116                 _charts=(),
117                 _axes=(),
118                 extLst=None,
119                ):
120        self.layout = layout
121        self.dTable = dTable
122        self.spPr = spPr
123        self._charts = _charts
124        self._axes = _axes
125
126
127    def to_tree(self, tagname=None, idx=None, namespace=None):
128        axIds = {ax.axId for ax in self._axes}
129        for chart in self._charts:
130            for id, axis in chart._axes.items():
131                if id not in axIds:
132                    setattr(self, axis.tagname, axis)
133                    axIds.add(id)
134
135        return super().to_tree(tagname)
136
137
138    @classmethod
139    def from_tree(cls, node):
140        self = super().from_tree(node)
141        axes = dict((axis.axId, axis) for axis in self._axes)
142        for chart in self._charts:
143            if isinstance(chart, (ScatterChart, BubbleChart)):
144                x, y = (axes[axId] for axId in chart.axId)
145                chart.x_axis = x
146                chart.y_axis = y
147                continue
148
149            for axId in chart.axId:
150                axis = axes.get(axId)
151                if axis is None and isinstance(chart, _3DBase):
152                    # Series Axis can be optional
153                    chart.z_axis = None
154                    continue
155                if axis.tagname in ("catAx", "dateAx"):
156                    chart.x_axis = axis
157                elif axis.tagname == "valAx":
158                    chart.y_axis = axis
159                elif axis.tagname == "serAx":
160                    chart.z_axis = axis
161
162        return self