Skip to main content

ppt_rs/generator/charts/
data.rs

1//! Chart data structures
2
3use super::types::ChartType;
4
5/// Chart data series
6#[derive(Clone, Debug)]
7pub struct ChartSeries {
8    pub name: String,
9    pub values: Vec<f64>,
10}
11
12impl ChartSeries {
13    /// Create a new chart series
14    pub fn new(name: &str, values: Vec<f64>) -> Self {
15        ChartSeries {
16            name: name.to_string(),
17            values,
18        }
19    }
20
21    /// Get the number of data points
22    pub fn len(&self) -> usize {
23        self.values.len()
24    }
25
26    /// Check if series is empty
27    pub fn is_empty(&self) -> bool {
28        self.values.is_empty()
29    }
30}
31
32/// Chart definition
33#[derive(Clone, Debug)]
34pub struct Chart {
35    pub title: String,
36    pub chart_type: ChartType,
37    pub categories: Vec<String>,
38    pub series: Vec<ChartSeries>,
39    pub x: u32,      // Position X in EMU
40    pub y: u32,      // Position Y in EMU
41    pub width: u32,  // Width in EMU
42    pub height: u32, // Height in EMU
43}
44
45impl Chart {
46    /// Create a new chart
47    pub fn new(
48        title: &str,
49        chart_type: ChartType,
50        categories: Vec<String>,
51        x: u32,
52        y: u32,
53        width: u32,
54        height: u32,
55    ) -> Self {
56        Chart {
57            title: title.to_string(),
58            chart_type,
59            categories,
60            series: Vec::new(),
61            x,
62            y,
63            width,
64            height,
65        }
66    }
67
68    /// Add a data series
69    pub fn add_series(mut self, series: ChartSeries) -> Self {
70        self.series.push(series);
71        self
72    }
73
74    /// Get number of categories
75    pub fn category_count(&self) -> usize {
76        self.categories.len()
77    }
78
79    /// Get number of series
80    pub fn series_count(&self) -> usize {
81        self.series.len()
82    }
83}
84
85#[cfg(test)]
86mod tests {
87    use super::*;
88
89    #[test]
90    fn test_chart_series() {
91        let series = ChartSeries::new("Sales", vec![10.0, 20.0, 30.0]);
92        assert_eq!(series.name, "Sales");
93        assert_eq!(series.len(), 3);
94        assert!(!series.is_empty());
95    }
96
97    #[test]
98    fn test_chart_add_series() {
99        let chart = Chart::new("Test", ChartType::Pie, vec!["A".to_string()], 0, 0, 1000000, 1000000)
100            .add_series(ChartSeries::new("Data", vec![50.0]));
101
102        assert_eq!(chart.series_count(), 1);
103    }
104}