Skip to main content

sheetkit_xml/
chart.rs

1//! Chart XML schema structures.
2//!
3//! Represents `xl/charts/chart{N}.xml` in the OOXML package.
4
5use serde::{Deserialize, Serialize};
6
7use crate::namespaces;
8
9/// Root element for a chart part.
10#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
11#[serde(rename = "chartSpace")]
12pub struct ChartSpace {
13    #[serde(rename = "@xmlns:c", default = "default_chart_ns")]
14    pub xmlns_c: String,
15
16    #[serde(rename = "@xmlns:a", default = "default_drawing_ml_ns")]
17    pub xmlns_a: String,
18
19    #[serde(rename = "@xmlns:r", default = "crate::default_relationships_ns")]
20    pub xmlns_r: String,
21
22    #[serde(rename = "c:chart")]
23    pub chart: Chart,
24}
25
26impl Default for ChartSpace {
27    fn default() -> Self {
28        Self {
29            xmlns_c: namespaces::DRAWING_ML_CHART.to_string(),
30            xmlns_a: namespaces::DRAWING_ML.to_string(),
31            xmlns_r: namespaces::RELATIONSHIPS.to_string(),
32            chart: Chart::default(),
33        }
34    }
35}
36
37/// The chart element containing plot area, legend, and title.
38#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
39pub struct Chart {
40    #[serde(rename = "c:title", skip_serializing_if = "Option::is_none")]
41    pub title: Option<ChartTitle>,
42
43    #[serde(rename = "c:view3D", skip_serializing_if = "Option::is_none")]
44    pub view_3d: Option<View3D>,
45
46    #[serde(rename = "c:plotArea")]
47    pub plot_area: PlotArea,
48
49    #[serde(rename = "c:legend", skip_serializing_if = "Option::is_none")]
50    pub legend: Option<Legend>,
51
52    #[serde(rename = "c:plotVisOnly", skip_serializing_if = "Option::is_none")]
53    pub plot_vis_only: Option<BoolVal>,
54}
55
56/// Chart title.
57#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
58pub struct ChartTitle {
59    #[serde(rename = "c:tx")]
60    pub tx: TitleTx,
61}
62
63/// Title text body.
64#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
65pub struct TitleTx {
66    #[serde(rename = "c:rich")]
67    pub rich: RichText,
68}
69
70/// Rich text body for chart titles.
71#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
72pub struct RichText {
73    #[serde(rename = "a:bodyPr")]
74    pub body_pr: BodyPr,
75
76    #[serde(rename = "a:p")]
77    pub paragraphs: Vec<Paragraph>,
78}
79
80/// Body properties (empty marker for chart titles).
81#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
82pub struct BodyPr {}
83
84/// A paragraph in rich text.
85#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
86pub struct Paragraph {
87    #[serde(rename = "a:r", default)]
88    pub runs: Vec<Run>,
89}
90
91/// A text run within a paragraph.
92#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
93pub struct Run {
94    #[serde(rename = "a:t")]
95    pub t: String,
96}
97
98/// Plot area containing chart type definitions and axes.
99#[derive(Debug, Clone, PartialEq, Serialize, Default)]
100pub struct PlotArea {
101    #[serde(rename = "c:layout", skip_serializing_if = "Option::is_none")]
102    pub layout: Option<Layout>,
103
104    #[serde(rename = "c:barChart", skip_serializing_if = "Option::is_none")]
105    pub bar_chart: Option<BarChart>,
106
107    #[serde(rename = "c:bar3DChart", skip_serializing_if = "Option::is_none")]
108    pub bar_3d_chart: Option<Bar3DChart>,
109
110    #[serde(rename = "c:lineChart", skip_serializing_if = "Option::is_none")]
111    pub line_chart: Option<LineChart>,
112
113    #[serde(rename = "c:line3DChart", skip_serializing_if = "Option::is_none")]
114    pub line_3d_chart: Option<Line3DChart>,
115
116    #[serde(rename = "c:pieChart", skip_serializing_if = "Option::is_none")]
117    pub pie_chart: Option<PieChart>,
118
119    #[serde(rename = "c:pie3DChart", skip_serializing_if = "Option::is_none")]
120    pub pie_3d_chart: Option<Pie3DChart>,
121
122    #[serde(rename = "c:doughnutChart", skip_serializing_if = "Option::is_none")]
123    pub doughnut_chart: Option<DoughnutChart>,
124
125    #[serde(rename = "c:areaChart", skip_serializing_if = "Option::is_none")]
126    pub area_chart: Option<AreaChart>,
127
128    #[serde(rename = "c:area3DChart", skip_serializing_if = "Option::is_none")]
129    pub area_3d_chart: Option<Area3DChart>,
130
131    #[serde(rename = "c:scatterChart", skip_serializing_if = "Option::is_none")]
132    pub scatter_chart: Option<ScatterChart>,
133
134    #[serde(rename = "c:bubbleChart", skip_serializing_if = "Option::is_none")]
135    pub bubble_chart: Option<BubbleChart>,
136
137    #[serde(rename = "c:radarChart", skip_serializing_if = "Option::is_none")]
138    pub radar_chart: Option<RadarChart>,
139
140    #[serde(rename = "c:stockChart", skip_serializing_if = "Option::is_none")]
141    pub stock_chart: Option<StockChart>,
142
143    #[serde(rename = "c:surfaceChart", skip_serializing_if = "Option::is_none")]
144    pub surface_chart: Option<SurfaceChart>,
145
146    #[serde(rename = "c:surface3DChart", skip_serializing_if = "Option::is_none")]
147    pub surface_3d_chart: Option<Surface3DChart>,
148
149    #[serde(rename = "c:ofPieChart", skip_serializing_if = "Option::is_none")]
150    pub of_pie_chart: Option<OfPieChart>,
151
152    #[serde(rename = "c:catAx", skip_serializing_if = "Option::is_none")]
153    pub cat_ax: Option<CatAx>,
154
155    #[serde(rename = "c:valAx", skip_serializing_if = "Option::is_none")]
156    pub val_ax: Option<ValAx>,
157
158    #[serde(rename = "c:valAx", default, skip_serializing_if = "Vec::is_empty")]
159    pub additional_val_axes: Vec<ValAx>,
160
161    #[serde(rename = "c:serAx", skip_serializing_if = "Option::is_none")]
162    pub ser_ax: Option<SerAx>,
163}
164
165#[derive(Deserialize)]
166struct PlotAreaDe {
167    #[serde(rename = "c:layout")]
168    layout: Option<Layout>,
169    #[serde(rename = "c:barChart")]
170    bar_chart: Option<BarChart>,
171    #[serde(rename = "c:bar3DChart")]
172    bar_3d_chart: Option<Bar3DChart>,
173    #[serde(rename = "c:lineChart")]
174    line_chart: Option<LineChart>,
175    #[serde(rename = "c:line3DChart")]
176    line_3d_chart: Option<Line3DChart>,
177    #[serde(rename = "c:pieChart")]
178    pie_chart: Option<PieChart>,
179    #[serde(rename = "c:pie3DChart")]
180    pie_3d_chart: Option<Pie3DChart>,
181    #[serde(rename = "c:doughnutChart")]
182    doughnut_chart: Option<DoughnutChart>,
183    #[serde(rename = "c:areaChart")]
184    area_chart: Option<AreaChart>,
185    #[serde(rename = "c:area3DChart")]
186    area_3d_chart: Option<Area3DChart>,
187    #[serde(rename = "c:scatterChart")]
188    scatter_chart: Option<ScatterChart>,
189    #[serde(rename = "c:bubbleChart")]
190    bubble_chart: Option<BubbleChart>,
191    #[serde(rename = "c:radarChart")]
192    radar_chart: Option<RadarChart>,
193    #[serde(rename = "c:stockChart")]
194    stock_chart: Option<StockChart>,
195    #[serde(rename = "c:surfaceChart")]
196    surface_chart: Option<SurfaceChart>,
197    #[serde(rename = "c:surface3DChart")]
198    surface_3d_chart: Option<Surface3DChart>,
199    #[serde(rename = "c:ofPieChart")]
200    of_pie_chart: Option<OfPieChart>,
201    #[serde(rename = "c:catAx")]
202    cat_ax: Option<CatAx>,
203    #[serde(rename = "c:valAx", alias = "valAx", default)]
204    val_axes: Vec<ValAx>,
205    #[serde(rename = "c:serAx")]
206    ser_ax: Option<SerAx>,
207}
208
209impl<'de> Deserialize<'de> for PlotArea {
210    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
211        let parsed = PlotAreaDe::deserialize(deserializer)?;
212        let mut val_axes = parsed.val_axes.into_iter();
213        Ok(Self {
214            layout: parsed.layout,
215            bar_chart: parsed.bar_chart,
216            bar_3d_chart: parsed.bar_3d_chart,
217            line_chart: parsed.line_chart,
218            line_3d_chart: parsed.line_3d_chart,
219            pie_chart: parsed.pie_chart,
220            pie_3d_chart: parsed.pie_3d_chart,
221            doughnut_chart: parsed.doughnut_chart,
222            area_chart: parsed.area_chart,
223            area_3d_chart: parsed.area_3d_chart,
224            scatter_chart: parsed.scatter_chart,
225            bubble_chart: parsed.bubble_chart,
226            radar_chart: parsed.radar_chart,
227            stock_chart: parsed.stock_chart,
228            surface_chart: parsed.surface_chart,
229            surface_3d_chart: parsed.surface_3d_chart,
230            of_pie_chart: parsed.of_pie_chart,
231            cat_ax: parsed.cat_ax,
232            val_ax: val_axes.next(),
233            additional_val_axes: val_axes.collect(),
234            ser_ax: parsed.ser_ax,
235        })
236    }
237}
238
239/// Layout (empty, uses automatic layout).
240#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
241pub struct Layout {}
242
243/// Bar chart definition.
244#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
245pub struct BarChart {
246    #[serde(rename = "c:barDir")]
247    pub bar_dir: StringVal,
248
249    #[serde(rename = "c:grouping")]
250    pub grouping: StringVal,
251
252    #[serde(rename = "c:ser", default)]
253    pub series: Vec<Series>,
254
255    #[serde(rename = "c:axId", default)]
256    pub ax_ids: Vec<UintVal>,
257}
258
259/// 3D bar chart definition.
260#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
261pub struct Bar3DChart {
262    #[serde(rename = "c:barDir")]
263    pub bar_dir: StringVal,
264
265    #[serde(rename = "c:grouping")]
266    pub grouping: StringVal,
267
268    #[serde(rename = "c:ser", default)]
269    pub series: Vec<Series>,
270
271    #[serde(rename = "c:shape", skip_serializing_if = "Option::is_none")]
272    pub shape: Option<StringVal>,
273
274    #[serde(rename = "c:axId", default)]
275    pub ax_ids: Vec<UintVal>,
276}
277
278/// Line chart definition.
279#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
280pub struct LineChart {
281    #[serde(rename = "c:grouping")]
282    pub grouping: StringVal,
283
284    #[serde(rename = "c:ser", default)]
285    pub series: Vec<Series>,
286
287    #[serde(rename = "c:axId", default)]
288    pub ax_ids: Vec<UintVal>,
289}
290
291/// 3D line chart definition.
292#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
293pub struct Line3DChart {
294    #[serde(rename = "c:grouping")]
295    pub grouping: StringVal,
296
297    #[serde(rename = "c:ser", default)]
298    pub series: Vec<Series>,
299
300    #[serde(rename = "c:axId", default)]
301    pub ax_ids: Vec<UintVal>,
302}
303
304/// Pie chart definition.
305#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
306pub struct PieChart {
307    #[serde(rename = "c:ser", default)]
308    pub series: Vec<Series>,
309}
310
311/// 3D pie chart definition.
312#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
313pub struct Pie3DChart {
314    #[serde(rename = "c:ser", default)]
315    pub series: Vec<Series>,
316}
317
318/// Doughnut chart definition.
319#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
320pub struct DoughnutChart {
321    #[serde(rename = "c:ser", default)]
322    pub series: Vec<Series>,
323
324    #[serde(rename = "c:holeSize", skip_serializing_if = "Option::is_none")]
325    pub hole_size: Option<UintVal>,
326}
327
328/// Area chart definition.
329#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
330pub struct AreaChart {
331    #[serde(rename = "c:grouping")]
332    pub grouping: StringVal,
333
334    #[serde(rename = "c:ser", default)]
335    pub series: Vec<Series>,
336
337    #[serde(rename = "c:axId", default)]
338    pub ax_ids: Vec<UintVal>,
339}
340
341/// 3D area chart definition.
342#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
343pub struct Area3DChart {
344    #[serde(rename = "c:grouping")]
345    pub grouping: StringVal,
346
347    #[serde(rename = "c:ser", default)]
348    pub series: Vec<Series>,
349
350    #[serde(rename = "c:axId", default)]
351    pub ax_ids: Vec<UintVal>,
352}
353
354/// Scatter chart definition.
355#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
356pub struct ScatterChart {
357    #[serde(rename = "c:scatterStyle")]
358    pub scatter_style: StringVal,
359
360    #[serde(rename = "c:ser", default)]
361    pub series: Vec<ScatterSeries>,
362
363    #[serde(rename = "c:axId", default)]
364    pub ax_ids: Vec<UintVal>,
365}
366
367/// Scatter series (uses xVal/yVal instead of cat/val).
368#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
369pub struct ScatterSeries {
370    #[serde(rename = "c:idx")]
371    pub idx: UintVal,
372
373    #[serde(rename = "c:order")]
374    pub order: UintVal,
375
376    #[serde(rename = "c:tx", skip_serializing_if = "Option::is_none")]
377    pub tx: Option<SeriesText>,
378
379    #[serde(rename = "c:xVal", skip_serializing_if = "Option::is_none")]
380    pub x_val: Option<CategoryRef>,
381
382    #[serde(rename = "c:yVal", skip_serializing_if = "Option::is_none")]
383    pub y_val: Option<ValueRef>,
384}
385
386/// Bubble chart definition.
387#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
388pub struct BubbleChart {
389    #[serde(rename = "c:ser", default)]
390    pub series: Vec<BubbleSeries>,
391
392    #[serde(rename = "c:axId", default)]
393    pub ax_ids: Vec<UintVal>,
394}
395
396/// Bubble series (uses xVal/yVal/bubbleSize).
397#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
398pub struct BubbleSeries {
399    #[serde(rename = "c:idx")]
400    pub idx: UintVal,
401
402    #[serde(rename = "c:order")]
403    pub order: UintVal,
404
405    #[serde(rename = "c:tx", skip_serializing_if = "Option::is_none")]
406    pub tx: Option<SeriesText>,
407
408    #[serde(rename = "c:xVal", skip_serializing_if = "Option::is_none")]
409    pub x_val: Option<CategoryRef>,
410
411    #[serde(rename = "c:yVal", skip_serializing_if = "Option::is_none")]
412    pub y_val: Option<ValueRef>,
413
414    #[serde(rename = "c:bubbleSize", skip_serializing_if = "Option::is_none")]
415    pub bubble_size: Option<ValueRef>,
416
417    #[serde(rename = "c:bubble3D", skip_serializing_if = "Option::is_none")]
418    pub bubble_3d: Option<BoolVal>,
419}
420
421/// Radar chart definition.
422#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
423pub struct RadarChart {
424    #[serde(rename = "c:radarStyle")]
425    pub radar_style: StringVal,
426
427    #[serde(rename = "c:ser", default)]
428    pub series: Vec<Series>,
429
430    #[serde(rename = "c:axId", default)]
431    pub ax_ids: Vec<UintVal>,
432}
433
434/// Stock chart definition.
435#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
436pub struct StockChart {
437    #[serde(rename = "c:ser", default)]
438    pub series: Vec<Series>,
439
440    #[serde(rename = "c:hiLowLines", skip_serializing_if = "Option::is_none")]
441    pub hi_low_lines: Option<HighLowLines>,
442
443    #[serde(rename = "c:upDownBars", skip_serializing_if = "Option::is_none")]
444    pub up_down_bars: Option<UpDownBars>,
445
446    #[serde(rename = "c:axId", default)]
447    pub ax_ids: Vec<UintVal>,
448}
449
450/// High-low lines for stock charts.
451#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
452pub struct HighLowLines {}
453
454/// Up/down bars for open-high-low-close stock charts.
455#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
456pub struct UpDownBars {}
457
458/// Surface chart definition.
459#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
460pub struct SurfaceChart {
461    #[serde(rename = "c:wireframe", skip_serializing_if = "Option::is_none")]
462    pub wireframe: Option<BoolVal>,
463
464    #[serde(rename = "c:ser", default)]
465    pub series: Vec<Series>,
466
467    #[serde(rename = "c:axId", default)]
468    pub ax_ids: Vec<UintVal>,
469}
470
471/// 3D surface chart definition.
472#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
473pub struct Surface3DChart {
474    #[serde(rename = "c:wireframe", skip_serializing_if = "Option::is_none")]
475    pub wireframe: Option<BoolVal>,
476
477    #[serde(rename = "c:ser", default)]
478    pub series: Vec<Series>,
479
480    #[serde(rename = "c:axId", default)]
481    pub ax_ids: Vec<UintVal>,
482}
483
484/// Of-pie chart definition (pie-of-pie or bar-of-pie).
485#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
486pub struct OfPieChart {
487    #[serde(rename = "c:ofPieType")]
488    pub of_pie_type: StringVal,
489
490    #[serde(rename = "c:ser", default)]
491    pub series: Vec<Series>,
492
493    #[serde(rename = "c:serLines", skip_serializing_if = "Option::is_none")]
494    pub ser_lines: Option<SerLines>,
495}
496
497/// Series lines marker element.
498#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
499pub struct SerLines {}
500
501/// A data series within a chart.
502#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
503pub struct Series {
504    #[serde(rename = "c:idx")]
505    pub idx: UintVal,
506
507    #[serde(rename = "c:order")]
508    pub order: UintVal,
509
510    #[serde(rename = "c:tx", skip_serializing_if = "Option::is_none")]
511    pub tx: Option<SeriesText>,
512
513    #[serde(rename = "c:cat", skip_serializing_if = "Option::is_none")]
514    pub cat: Option<CategoryRef>,
515
516    #[serde(rename = "c:val", skip_serializing_if = "Option::is_none")]
517    pub val: Option<ValueRef>,
518}
519
520/// Series text (name) reference.
521#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
522pub struct SeriesText {
523    #[serde(rename = "c:strRef", skip_serializing_if = "Option::is_none")]
524    pub str_ref: Option<StrRef>,
525
526    #[serde(rename = "c:v", skip_serializing_if = "Option::is_none")]
527    pub v: Option<String>,
528}
529
530/// String reference (a formula to a cell range).
531#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
532pub struct StrRef {
533    #[serde(rename = "c:f")]
534    pub f: String,
535}
536
537/// Category axis data reference.
538#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
539pub struct CategoryRef {
540    #[serde(rename = "c:strRef", skip_serializing_if = "Option::is_none")]
541    pub str_ref: Option<StrRef>,
542
543    #[serde(rename = "c:numRef", skip_serializing_if = "Option::is_none")]
544    pub num_ref: Option<NumRef>,
545}
546
547/// Value axis data reference.
548#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
549pub struct ValueRef {
550    #[serde(rename = "c:numRef", skip_serializing_if = "Option::is_none")]
551    pub num_ref: Option<NumRef>,
552}
553
554/// Numeric reference (a formula to a numeric cell range).
555#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
556pub struct NumRef {
557    #[serde(rename = "c:f")]
558    pub f: String,
559}
560
561/// Chart legend.
562#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
563pub struct Legend {
564    #[serde(rename = "c:legendPos")]
565    pub legend_pos: StringVal,
566}
567
568/// Category axis.
569#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
570pub struct CatAx {
571    #[serde(rename = "c:axId")]
572    pub ax_id: UintVal,
573
574    #[serde(rename = "c:scaling")]
575    pub scaling: Scaling,
576
577    #[serde(rename = "c:delete")]
578    pub delete: BoolVal,
579
580    #[serde(rename = "c:axPos")]
581    pub ax_pos: StringVal,
582
583    #[serde(rename = "c:crossAx")]
584    pub cross_ax: UintVal,
585}
586
587/// Value axis.
588#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
589pub struct ValAx {
590    #[serde(rename = "c:axId", alias = "axId")]
591    pub ax_id: UintVal,
592
593    #[serde(rename = "c:scaling", alias = "scaling")]
594    pub scaling: Scaling,
595
596    #[serde(rename = "c:delete", alias = "delete")]
597    pub delete: BoolVal,
598
599    #[serde(rename = "c:axPos", alias = "axPos")]
600    pub ax_pos: StringVal,
601
602    #[serde(rename = "c:crossAx", alias = "crossAx")]
603    pub cross_ax: UintVal,
604}
605
606/// Series axis (used by surface and some 3D charts).
607#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
608pub struct SerAx {
609    #[serde(rename = "c:axId")]
610    pub ax_id: UintVal,
611
612    #[serde(rename = "c:scaling")]
613    pub scaling: Scaling,
614
615    #[serde(rename = "c:delete")]
616    pub delete: BoolVal,
617
618    #[serde(rename = "c:axPos")]
619    pub ax_pos: StringVal,
620
621    #[serde(rename = "c:crossAx")]
622    pub cross_ax: UintVal,
623}
624
625/// 3D view settings.
626#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
627pub struct View3D {
628    #[serde(rename = "c:rotX", skip_serializing_if = "Option::is_none")]
629    pub rot_x: Option<IntVal>,
630
631    #[serde(rename = "c:rotY", skip_serializing_if = "Option::is_none")]
632    pub rot_y: Option<IntVal>,
633
634    #[serde(rename = "c:depthPercent", skip_serializing_if = "Option::is_none")]
635    pub depth_percent: Option<UintVal>,
636
637    #[serde(rename = "c:rAngAx", skip_serializing_if = "Option::is_none")]
638    pub r_ang_ax: Option<BoolVal>,
639
640    #[serde(rename = "c:perspective", skip_serializing_if = "Option::is_none")]
641    pub perspective: Option<UintVal>,
642}
643
644/// Axis scaling (orientation).
645#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
646pub struct Scaling {
647    #[serde(rename = "c:orientation", alias = "orientation")]
648    pub orientation: StringVal,
649}
650
651/// A wrapper for a string `val` attribute.
652#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
653pub struct StringVal {
654    #[serde(rename = "@val")]
655    pub val: String,
656}
657
658/// A wrapper for an unsigned integer `val` attribute.
659#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
660pub struct UintVal {
661    #[serde(rename = "@val")]
662    pub val: u32,
663}
664
665/// A wrapper for a signed integer `val` attribute.
666#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
667pub struct IntVal {
668    #[serde(rename = "@val")]
669    pub val: i32,
670}
671
672/// A wrapper for a boolean `val` attribute.
673#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
674pub struct BoolVal {
675    #[serde(rename = "@val")]
676    pub val: bool,
677}
678
679fn default_chart_ns() -> String {
680    namespaces::DRAWING_ML_CHART.to_string()
681}
682
683fn default_drawing_ml_ns() -> String {
684    namespaces::DRAWING_ML.to_string()
685}
686
687#[cfg(test)]
688mod tests {
689    use super::*;
690
691    #[test]
692    fn test_chart_space_default() {
693        let cs = ChartSpace::default();
694        assert_eq!(cs.xmlns_c, namespaces::DRAWING_ML_CHART);
695        assert_eq!(cs.xmlns_a, namespaces::DRAWING_ML);
696        assert_eq!(cs.xmlns_r, namespaces::RELATIONSHIPS);
697    }
698
699    #[test]
700    fn test_string_val_serialize() {
701        let sv = StringVal {
702            val: "col".to_string(),
703        };
704        let xml = quick_xml::se::to_string(&sv).unwrap();
705        assert!(xml.contains("val=\"col\""));
706    }
707
708    #[test]
709    fn test_uint_val_serialize() {
710        let uv = UintVal { val: 42 };
711        let xml = quick_xml::se::to_string(&uv).unwrap();
712        assert!(xml.contains("val=\"42\""));
713    }
714
715    #[test]
716    fn test_int_val_serialize() {
717        let iv = IntVal { val: -15 };
718        let xml = quick_xml::se::to_string(&iv).unwrap();
719        assert!(xml.contains("val=\"-15\""));
720    }
721
722    #[test]
723    fn test_bool_val_serialize() {
724        let bv = BoolVal { val: true };
725        let xml = quick_xml::se::to_string(&bv).unwrap();
726        assert!(xml.contains("val=\"true\""));
727    }
728
729    #[test]
730    fn test_series_serialize() {
731        let series = Series {
732            idx: UintVal { val: 0 },
733            order: UintVal { val: 0 },
734            tx: Some(SeriesText {
735                str_ref: None,
736                v: Some("Sales".to_string()),
737            }),
738            cat: Some(CategoryRef {
739                str_ref: Some(StrRef {
740                    f: "Sheet1!$A$2:$A$6".to_string(),
741                }),
742                num_ref: None,
743            }),
744            val: Some(ValueRef {
745                num_ref: Some(NumRef {
746                    f: "Sheet1!$B$2:$B$6".to_string(),
747                }),
748            }),
749        };
750        let xml = quick_xml::se::to_string(&series).unwrap();
751        assert!(xml.contains("Sheet1!$A$2:$A$6"));
752        assert!(xml.contains("Sheet1!$B$2:$B$6"));
753    }
754
755    #[test]
756    fn test_bar_chart_serialize() {
757        let bar = BarChart {
758            bar_dir: StringVal {
759                val: "col".to_string(),
760            },
761            grouping: StringVal {
762                val: "clustered".to_string(),
763            },
764            series: vec![],
765            ax_ids: vec![UintVal { val: 1 }, UintVal { val: 2 }],
766        };
767        let xml = quick_xml::se::to_string(&bar).unwrap();
768        assert!(xml.contains("col"));
769        assert!(xml.contains("clustered"));
770    }
771
772    #[test]
773    fn test_bar_3d_chart_serialize() {
774        let bar = Bar3DChart {
775            bar_dir: StringVal {
776                val: "col".to_string(),
777            },
778            grouping: StringVal {
779                val: "clustered".to_string(),
780            },
781            series: vec![],
782            shape: None,
783            ax_ids: vec![UintVal { val: 1 }, UintVal { val: 2 }],
784        };
785        let xml = quick_xml::se::to_string(&bar).unwrap();
786        assert!(xml.contains("col"));
787        assert!(xml.contains("clustered"));
788    }
789
790    #[test]
791    fn test_area_chart_serialize() {
792        let area = AreaChart {
793            grouping: StringVal {
794                val: "standard".to_string(),
795            },
796            series: vec![],
797            ax_ids: vec![UintVal { val: 1 }, UintVal { val: 2 }],
798        };
799        let xml = quick_xml::se::to_string(&area).unwrap();
800        assert!(xml.contains("standard"));
801    }
802
803    #[test]
804    fn test_scatter_chart_serialize() {
805        let scatter = ScatterChart {
806            scatter_style: StringVal {
807                val: "lineMarker".to_string(),
808            },
809            series: vec![ScatterSeries {
810                idx: UintVal { val: 0 },
811                order: UintVal { val: 0 },
812                tx: None,
813                x_val: Some(CategoryRef {
814                    str_ref: None,
815                    num_ref: Some(NumRef {
816                        f: "Sheet1!$A$2:$A$6".to_string(),
817                    }),
818                }),
819                y_val: Some(ValueRef {
820                    num_ref: Some(NumRef {
821                        f: "Sheet1!$B$2:$B$6".to_string(),
822                    }),
823                }),
824            }],
825            ax_ids: vec![UintVal { val: 1 }, UintVal { val: 2 }],
826        };
827        let xml = quick_xml::se::to_string(&scatter).unwrap();
828        assert!(xml.contains("lineMarker"));
829        assert!(xml.contains("Sheet1!$A$2:$A$6"));
830        assert!(xml.contains("Sheet1!$B$2:$B$6"));
831    }
832
833    #[test]
834    fn test_bubble_chart_serialize() {
835        let bubble = BubbleChart {
836            series: vec![BubbleSeries {
837                idx: UintVal { val: 0 },
838                order: UintVal { val: 0 },
839                tx: None,
840                x_val: None,
841                y_val: None,
842                bubble_size: Some(ValueRef {
843                    num_ref: Some(NumRef {
844                        f: "Sheet1!$C$2:$C$6".to_string(),
845                    }),
846                }),
847                bubble_3d: None,
848            }],
849            ax_ids: vec![UintVal { val: 1 }, UintVal { val: 2 }],
850        };
851        let xml = quick_xml::se::to_string(&bubble).unwrap();
852        assert!(xml.contains("Sheet1!$C$2:$C$6"));
853    }
854
855    #[test]
856    fn test_radar_chart_serialize() {
857        let radar = RadarChart {
858            radar_style: StringVal {
859                val: "marker".to_string(),
860            },
861            series: vec![],
862            ax_ids: vec![UintVal { val: 1 }, UintVal { val: 2 }],
863        };
864        let xml = quick_xml::se::to_string(&radar).unwrap();
865        assert!(xml.contains("marker"));
866    }
867
868    #[test]
869    fn test_surface_chart_serialize() {
870        let surface = SurfaceChart {
871            wireframe: Some(BoolVal { val: true }),
872            series: vec![],
873            ax_ids: vec![UintVal { val: 1 }, UintVal { val: 2 }, UintVal { val: 3 }],
874        };
875        let xml = quick_xml::se::to_string(&surface).unwrap();
876        assert!(xml.contains("val=\"true\""));
877    }
878
879    #[test]
880    fn test_view_3d_serialize() {
881        let view = View3D {
882            rot_x: Some(IntVal { val: 15 }),
883            rot_y: Some(IntVal { val: 20 }),
884            depth_percent: Some(UintVal { val: 150 }),
885            r_ang_ax: Some(BoolVal { val: true }),
886            perspective: Some(UintVal { val: 30 }),
887        };
888        let xml = quick_xml::se::to_string(&view).unwrap();
889        assert!(xml.contains("val=\"15\""));
890        assert!(xml.contains("val=\"20\""));
891        assert!(xml.contains("val=\"150\""));
892    }
893
894    #[test]
895    fn test_ser_ax_serialize() {
896        let ser_ax = SerAx {
897            ax_id: UintVal { val: 3 },
898            scaling: Scaling {
899                orientation: StringVal {
900                    val: "minMax".to_string(),
901                },
902            },
903            delete: BoolVal { val: false },
904            ax_pos: StringVal {
905                val: "b".to_string(),
906            },
907            cross_ax: UintVal { val: 1 },
908        };
909        let xml = quick_xml::se::to_string(&ser_ax).unwrap();
910        assert!(xml.contains("val=\"3\""));
911        assert!(xml.contains("minMax"));
912    }
913
914    #[test]
915    fn test_plot_area_roundtrips_multiple_value_axes() {
916        let axis = |id: u32, position: &str| ValAx {
917            ax_id: UintVal { val: id },
918            scaling: Scaling {
919                orientation: StringVal {
920                    val: "minMax".to_string(),
921                },
922            },
923            delete: BoolVal { val: false },
924            ax_pos: StringVal {
925                val: position.to_string(),
926            },
927            cross_ax: UintVal { val: 1 },
928        };
929        let plot_area = PlotArea {
930            val_ax: Some(axis(2, "l")),
931            additional_val_axes: vec![axis(3, "r")],
932            ..PlotArea::default()
933        };
934
935        let xml = quick_xml::se::to_string(&plot_area).unwrap();
936        assert_eq!(xml.matches("valAx").count(), 4);
937        let inner = xml
938            .strip_prefix("<PlotArea>")
939            .and_then(|xml| xml.strip_suffix("</PlotArea>"))
940            .unwrap();
941        let xml = format!(
942            "<c:plotArea xmlns:c=\"http://schemas.openxmlformats.org/drawingml/2006/chart\">{inner}</c:plotArea>"
943        );
944        let parsed: PlotArea = quick_xml::de::from_str(&xml).unwrap();
945        assert_eq!(parsed.val_ax.as_ref().map(|axis| axis.ax_id.val), Some(2));
946        assert_eq!(parsed.additional_val_axes[0].ax_id.val, 3);
947    }
948
949    #[test]
950    fn test_legend_serialize() {
951        let legend = Legend {
952            legend_pos: StringVal {
953                val: "b".to_string(),
954            },
955        };
956        let xml = quick_xml::se::to_string(&legend).unwrap();
957        assert!(xml.contains("val=\"b\""));
958    }
959
960    #[test]
961    fn test_chart_title_serialize() {
962        let title = ChartTitle {
963            tx: TitleTx {
964                rich: RichText {
965                    body_pr: BodyPr {},
966                    paragraphs: vec![Paragraph {
967                        runs: vec![Run {
968                            t: "My Chart".to_string(),
969                        }],
970                    }],
971                },
972            },
973        };
974        let xml = quick_xml::se::to_string(&title).unwrap();
975        assert!(xml.contains("My Chart"));
976    }
977
978    #[test]
979    fn test_num_ref_serialize() {
980        let num_ref = NumRef {
981            f: "Sheet1!$B$1:$B$5".to_string(),
982        };
983        let xml = quick_xml::se::to_string(&num_ref).unwrap();
984        assert!(xml.contains("Sheet1!$B$1:$B$5"));
985    }
986
987    #[test]
988    fn test_str_ref_serialize() {
989        let str_ref = StrRef {
990            f: "Sheet1!$A$1".to_string(),
991        };
992        let xml = quick_xml::se::to_string(&str_ref).unwrap();
993        assert!(xml.contains("Sheet1!$A$1"));
994    }
995
996    #[test]
997    fn test_plot_area_default_all_none() {
998        let pa = PlotArea::default();
999        assert!(pa.layout.is_none());
1000        assert!(pa.bar_chart.is_none());
1001        assert!(pa.bar_3d_chart.is_none());
1002        assert!(pa.line_chart.is_none());
1003        assert!(pa.line_3d_chart.is_none());
1004        assert!(pa.pie_chart.is_none());
1005        assert!(pa.pie_3d_chart.is_none());
1006        assert!(pa.doughnut_chart.is_none());
1007        assert!(pa.area_chart.is_none());
1008        assert!(pa.area_3d_chart.is_none());
1009        assert!(pa.scatter_chart.is_none());
1010        assert!(pa.bubble_chart.is_none());
1011        assert!(pa.radar_chart.is_none());
1012        assert!(pa.stock_chart.is_none());
1013        assert!(pa.surface_chart.is_none());
1014        assert!(pa.surface_3d_chart.is_none());
1015        assert!(pa.of_pie_chart.is_none());
1016        assert!(pa.cat_ax.is_none());
1017        assert!(pa.val_ax.is_none());
1018        assert!(pa.ser_ax.is_none());
1019    }
1020
1021    #[test]
1022    fn test_chart_with_view_3d() {
1023        let chart = Chart {
1024            title: None,
1025            view_3d: Some(View3D {
1026                rot_x: Some(IntVal { val: 15 }),
1027                rot_y: Some(IntVal { val: 20 }),
1028                depth_percent: None,
1029                r_ang_ax: Some(BoolVal { val: true }),
1030                perspective: None,
1031            }),
1032            plot_area: PlotArea::default(),
1033            legend: None,
1034            plot_vis_only: None,
1035        };
1036        let xml = quick_xml::se::to_string(&chart).unwrap();
1037        assert!(xml.contains("val=\"15\""));
1038        assert!(xml.contains("val=\"20\""));
1039    }
1040
1041    #[test]
1042    fn test_doughnut_chart_serialize() {
1043        let doughnut = DoughnutChart {
1044            series: vec![],
1045            hole_size: Some(UintVal { val: 50 }),
1046        };
1047        let xml = quick_xml::se::to_string(&doughnut).unwrap();
1048        assert!(xml.contains("val=\"50\""));
1049    }
1050
1051    #[test]
1052    fn test_bar_3d_chart_with_shape() {
1053        let bar = Bar3DChart {
1054            bar_dir: StringVal {
1055                val: "col".to_string(),
1056            },
1057            grouping: StringVal {
1058                val: "clustered".to_string(),
1059            },
1060            series: vec![],
1061            shape: Some(StringVal {
1062                val: "cone".to_string(),
1063            }),
1064            ax_ids: vec![UintVal { val: 1 }, UintVal { val: 2 }],
1065        };
1066        let xml = quick_xml::se::to_string(&bar).unwrap();
1067        assert!(xml.contains("cone"));
1068    }
1069
1070    #[test]
1071    fn test_of_pie_chart_serialize() {
1072        let of_pie = OfPieChart {
1073            of_pie_type: StringVal {
1074                val: "pie".to_string(),
1075            },
1076            series: vec![Series {
1077                idx: UintVal { val: 0 },
1078                order: UintVal { val: 0 },
1079                tx: None,
1080                cat: None,
1081                val: None,
1082            }],
1083            ser_lines: Some(SerLines {}),
1084        };
1085        let xml = quick_xml::se::to_string(&of_pie).unwrap();
1086        assert!(xml.contains("val=\"pie\""));
1087        assert!(xml.contains("serLines"));
1088    }
1089
1090    #[test]
1091    fn test_bubble_series_with_bubble_3d() {
1092        let bs = BubbleSeries {
1093            idx: UintVal { val: 0 },
1094            order: UintVal { val: 0 },
1095            tx: None,
1096            x_val: None,
1097            y_val: None,
1098            bubble_size: None,
1099            bubble_3d: Some(BoolVal { val: true }),
1100        };
1101        let xml = quick_xml::se::to_string(&bs).unwrap();
1102        assert!(xml.contains("bubble3D"));
1103        assert!(xml.contains("val=\"true\""));
1104    }
1105}