Skip to main content

umya_spreadsheet/structs/drawing/
graphic_data.rs

1// *:graphicData
2use std::io::Cursor;
3
4use quick_xml::{
5    Reader,
6    Writer,
7    events::{
8        BytesStart,
9        Event,
10    },
11};
12
13use super::charts::ChartSpace;
14use crate::{
15    helper::const_str::{
16        DRAWINGML_CHART_NS,
17        REL_OFC_NS,
18    },
19    reader::{
20        driver::{
21            get_attribute,
22            xml_read_loop,
23        },
24        xlsx::chart,
25    },
26    structs::raw::RawRelationships,
27    traits::AdjustmentCoordinateWithSheet,
28    writer::driver::{
29        write_end_tag,
30        write_start_tag,
31    },
32};
33
34#[derive(Clone, Default, Debug)]
35pub struct GraphicData {
36    chart_space: ChartSpace,
37}
38
39impl GraphicData {
40    #[inline]
41    #[must_use]
42    pub fn chart_space(&self) -> &ChartSpace {
43        &self.chart_space
44    }
45
46    #[inline]
47    #[must_use]
48    #[deprecated(since = "3.0.0", note = "Use chart_space()")]
49    pub fn get_chart_space(&self) -> &ChartSpace {
50        self.chart_space()
51    }
52
53    #[inline]
54    pub fn chart_space_mut(&mut self) -> &mut ChartSpace {
55        &mut self.chart_space
56    }
57
58    #[inline]
59    #[deprecated(since = "3.0.0", note = "Use chart_space_mut()")]
60    pub fn get_chart_space_mut(&mut self) -> &mut ChartSpace {
61        self.chart_space_mut()
62    }
63
64    #[inline]
65    pub fn set_chart_space(&mut self, value: ChartSpace) -> &GraphicData {
66        self.chart_space = value;
67        self
68    }
69
70    pub(crate) fn set_attributes<R: std::io::BufRead>(
71        &mut self,
72        reader: &mut Reader<R>,
73        _e: &BytesStart,
74        drawing_relationships: Option<&RawRelationships>,
75    ) {
76        xml_read_loop!(
77            reader,
78            Event::Empty(ref e) => {
79                if e.name().into_inner() == b"c:chart" {
80                    let chart_id = get_attribute(e, b"r:id").unwrap();
81                    let relationship = drawing_relationships
82                        .unwrap()
83                        .relationship_by_rid(&chart_id);
84                    chart::read(relationship.raw_file(), &mut self.chart_space);
85                }
86            },
87            Event::End(ref e) => {
88                if e.name().into_inner() == b"a:graphicData" {
89                    return;
90                }
91            },
92            Event::Eof => panic!("Error: Could not find {} end element", "a:graphicData")
93        );
94    }
95
96    pub(crate) fn write_to(
97        writer: &mut Writer<Cursor<Vec<u8>>>,
98        rel_list: &mut Vec<(String, String)>,
99    ) {
100        // a:graphicData
101        write_start_tag(
102            writer,
103            "a:graphicData",
104            vec![("uri", DRAWINGML_CHART_NS).into()],
105            false,
106        );
107
108        // c:chart
109        rel_list.push((String::from("CHART"), String::new()));
110        write_start_tag(
111            writer,
112            "c:chart",
113            vec![
114                ("xmlns:c", DRAWINGML_CHART_NS).into(),
115                ("xmlns:r", REL_OFC_NS).into(),
116                ("r:id", format!("rId{}", rel_list.len()).as_str()).into(),
117            ],
118            true,
119        );
120
121        write_end_tag(writer, "a:graphicData");
122    }
123}
124impl AdjustmentCoordinateWithSheet for GraphicData {
125    #[inline]
126    fn adjustment_insert_coordinate_with_sheet(
127        &mut self,
128        sheet_name: &str,
129        root_col_num: u32,
130        offset_col_num: u32,
131        root_row_num: u32,
132        offset_row_num: u32,
133    ) {
134        self.chart_space.adjustment_insert_coordinate_with_sheet(
135            sheet_name,
136            root_col_num,
137            offset_col_num,
138            root_row_num,
139            offset_row_num,
140        );
141    }
142
143    #[inline]
144    fn adjustment_remove_coordinate_with_sheet(
145        &mut self,
146        sheet_name: &str,
147        root_col_num: u32,
148        offset_col_num: u32,
149        root_row_num: u32,
150        offset_row_num: u32,
151    ) {
152        self.chart_space.adjustment_remove_coordinate_with_sheet(
153            sheet_name,
154            root_col_num,
155            offset_col_num,
156            root_row_num,
157            offset_row_num,
158        );
159    }
160}