reader.py 1.78 KB
Newer Older
1
from __future__ import absolute_import
CharlieC's avatar
CharlieC committed
2
# Copyright (c) 2010-2017 openpyxl
3
4
5
6
7
8
9
10

"""
Read a chart
"""

from .chartspace import ChartSpace, PlotArea
from openpyxl.xml.functions import fromstring

11
12
13
from openpyxl.packaging.relationship import get_rel, get_rels_path, get_dependents
from openpyxl.drawing.spreadsheet_drawing import SpreadsheetDrawing

14
15
16
17
18
19
20
21
_types = ('areaChart', 'area3DChart', 'lineChart', 'line3DChart',
         'stockChart', 'radarChart', 'scatterChart', 'pieChart', 'pie3DChart',
         'doughnutChart', 'barChart', 'bar3DChart', 'ofPieChart', 'surfaceChart',
         'surface3DChart', 'bubbleChart',)

_axes = ('valAx', 'catAx', 'dateAx', 'serAx',)


22
23
def read_chart(chartspace):
    cs = chartspace
24
    plot = cs.chart.plotArea
25
    charts = []
26
27
28
    for t in _types:
        chart = getattr(plot, t, None)
        if chart is not None:
29
30
31
32
            charts.append(chart)

    chart = charts[0]
    chart._charts = charts
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47

    chart.title = cs.chart.title
    chart.layout = plot.layout
    chart.legend = cs.chart.legend

    for x in _axes:
        ax = getattr(plot, x)
        if ax:
            if x == 'valAx':
                chart.y_axis = ax[0]
            elif x == 'serAx':
                chart.z_axis = ax[0]
            else:
                chart.x_axis = ax[0]
    return chart
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62


def find_charts(archive, path):
    """
    Given the path to a drawing file extract anchors with charts
    """

    src = archive.read(path)
    tree = fromstring(src)
    drawing = SpreadsheetDrawing.from_tree(tree)

    rels_path = get_rels_path(path)
    deps = get_dependents(archive, rels_path)

    charts = []
63
    for rel in drawing._chart_rels:
64
65
        cs = get_rel(archive, deps, rel.id, ChartSpace)
        chart = read_chart(cs)
66
67
        chart.anchor = rel.anchor
        charts.append(chart)
68
69

    return charts