oxidize-pdf 2.5.1

A pure Rust PDF generation and manipulation library with zero external dependencies
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
//! Dashboard Templates - Pre-built Dashboard Layouts
//!
//! This module provides ready-to-use dashboard templates for common use cases.
//! Templates provide a quick way to create professional dashboards by simply
//! providing data, without needing to configure layout and components manually.
//!
//! # Examples
//!
//! ```rust,ignore
//! use oxidize_pdf::dashboard::templates::{SalesDashboardTemplate, TemplateData};
//!
//! let data = TemplateData::new()
//!     .with_kpi("revenue", "$2.5M", 12.5)
//!     .with_kpi("orders", "1,247", 8.3)
//!     .with_chart_data("monthly_sales", vec![100.0, 150.0, 200.0]);
//!
//! let dashboard = SalesDashboardTemplate::new()
//!     .title("Q4 2024 Sales Report")
//!     .build(data)?;
//! ```

use super::{
    Dashboard, DashboardBuilder, HeatMap, HeatMapData, KpiCard, KpiCardBuilder, PivotTable,
    TrendDirection,
};
use crate::charts::{
    BarChartBuilder, DashboardBarChart, DashboardLineChart, DashboardPieChart, DataSeries,
    LineChartBuilder, PieChartBuilder, PieSegment,
};
use crate::error::PdfError;
use crate::graphics::Color;
use std::collections::HashMap;

/// Template data container for building dashboards
#[derive(Debug, Clone, Default)]
pub struct TemplateData {
    /// KPI metrics with name, value, and trend
    pub kpis: Vec<KpiData>,
    /// Chart data series
    pub charts: HashMap<String, ChartData>,
    /// Tabular data for pivot tables
    pub tables: HashMap<String, Vec<HashMap<String, String>>>,
    /// Additional metadata
    pub metadata: HashMap<String, String>,
}

/// KPI metric data
#[derive(Debug, Clone)]
pub struct KpiData {
    pub name: String,
    pub value: String,
    pub subtitle: Option<String>,
    pub trend_value: Option<f64>,
    pub trend_direction: Option<TrendDirection>,
    pub color: Option<Color>,
    pub sparkline: Option<Vec<f64>>,
}

/// Chart data for templates
#[derive(Debug, Clone)]
pub enum ChartData {
    /// Bar chart data with labels
    Bar {
        labels: Vec<String>,
        values: Vec<f64>,
        colors: Option<Vec<Color>>,
    },
    /// Line chart with multiple series
    Line { series: Vec<SeriesData> },
    /// Pie chart segments
    Pie { segments: Vec<PieSegmentData> },
    /// Heatmap data
    HeatMap {
        values: Vec<Vec<f64>>,
        row_labels: Vec<String>,
        column_labels: Vec<String>,
    },
}

/// Line chart series data
#[derive(Debug, Clone)]
pub struct SeriesData {
    pub name: String,
    pub data: Vec<(f64, f64)>,
    pub color: Color,
}

/// Pie chart segment data
#[derive(Debug, Clone)]
pub struct PieSegmentData {
    pub label: String,
    pub value: f64,
    pub color: Color,
}

impl TemplateData {
    /// Create new empty template data
    pub fn new() -> Self {
        Self::default()
    }

    /// Add a KPI metric
    pub fn with_kpi(
        mut self,
        name: impl Into<String>,
        value: impl Into<String>,
        trend: f64,
    ) -> Self {
        self.kpis.push(KpiData {
            name: name.into(),
            value: value.into(),
            subtitle: None,
            trend_value: Some(trend),
            trend_direction: Some(if trend >= 0.0 {
                TrendDirection::Up
            } else {
                TrendDirection::Down
            }),
            color: None,
            sparkline: None,
        });
        self
    }

    /// Add KPI with full configuration
    pub fn add_kpi(mut self, kpi: KpiData) -> Self {
        self.kpis.push(kpi);
        self
    }

    /// Add chart data
    pub fn with_chart(mut self, name: impl Into<String>, chart_data: ChartData) -> Self {
        self.charts.insert(name.into(), chart_data);
        self
    }

    /// Add table data
    pub fn with_table(
        mut self,
        name: impl Into<String>,
        data: Vec<HashMap<String, String>>,
    ) -> Self {
        self.tables.insert(name.into(), data);
        self
    }

    /// Add metadata
    pub fn with_metadata(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.metadata.insert(key.into(), value.into());
        self
    }
}

/// Sales Dashboard Template
///
/// Pre-configured dashboard for sales reporting with:
/// - KPI cards for key metrics
/// - Monthly sales trend chart
/// - Product/category breakdown
/// - Regional performance heatmap
#[derive(Debug, Clone)]
pub struct SalesDashboardTemplate {
    title: String,
    subtitle: Option<String>,
    theme: String,
}

impl SalesDashboardTemplate {
    /// Create new sales dashboard template
    pub fn new() -> Self {
        Self {
            title: "Sales Dashboard".to_string(),
            subtitle: None,
            theme: "corporate".to_string(),
        }
    }

    /// Set dashboard title
    pub fn title(mut self, title: impl Into<String>) -> Self {
        self.title = title.into();
        self
    }

    /// Set dashboard subtitle
    pub fn subtitle(mut self, subtitle: impl Into<String>) -> Self {
        self.subtitle = Some(subtitle.into());
        self
    }

    /// Set theme
    pub fn theme(mut self, theme: impl Into<String>) -> Self {
        self.theme = theme.into();
        self
    }

    /// Build dashboard from template data
    pub fn build(self, data: TemplateData) -> Result<Dashboard, PdfError> {
        let mut builder = DashboardBuilder::new()
            .title(self.title)
            .theme_by_name(&self.theme);

        if let Some(subtitle) = self.subtitle {
            builder = builder.subtitle(subtitle);
        }

        // Add KPI cards
        if !data.kpis.is_empty() {
            let kpi_cards: Vec<KpiCard> = data
                .kpis
                .into_iter()
                .map(|kpi| {
                    let mut card_builder = KpiCardBuilder::new(&kpi.name, &kpi.value);

                    if let Some(subtitle) = kpi.subtitle {
                        card_builder = card_builder.subtitle(&subtitle);
                    }

                    if let (Some(trend_value), Some(trend_dir)) =
                        (kpi.trend_value, kpi.trend_direction)
                    {
                        card_builder = card_builder.trend(trend_value, trend_dir);
                    }

                    if let Some(color) = kpi.color {
                        card_builder = card_builder.color(color);
                    }

                    if let Some(sparkline) = kpi.sparkline {
                        card_builder = card_builder.sparkline(sparkline);
                    }

                    card_builder.build()
                })
                .collect();

            builder = builder.add_kpi_row(kpi_cards);
        }

        // Add charts
        builder = builder.start_row();

        // Monthly sales chart (bar or line)
        if let Some(ChartData::Bar {
            labels,
            values,
            colors,
        }) = data.charts.get("monthly_sales")
        {
            let labeled_values: Vec<(&str, f64)> = labels
                .iter()
                .map(|s| s.as_str())
                .zip(values.iter().copied())
                .collect();

            let mut chart_builder = BarChartBuilder::new()
                .title("Monthly Sales")
                .labeled_data(labeled_values)
                .show_grid(true)
                .show_values(true);

            if let Some(color_list) = colors {
                chart_builder = chart_builder.colors(color_list.clone());
            }

            let chart = chart_builder.build();
            builder = builder.add_to_row(Box::new(DashboardBarChart::new(chart).span(6)));
        }

        // Product breakdown (pie chart)
        if let Some(ChartData::Pie { segments }) = data.charts.get("product_breakdown") {
            let mut chart_builder = PieChartBuilder::new().title("Product Distribution");

            for segment in segments {
                chart_builder = chart_builder.add_segment(PieSegment::new(
                    &segment.label,
                    segment.value,
                    segment.color,
                ));
            }

            let chart = chart_builder.show_percentages(true).build();
            builder = builder.add_to_row(Box::new(DashboardPieChart::new(chart).span(6)));
        }

        builder = builder.finish_row();

        // Regional heatmap
        if let Some(ChartData::HeatMap {
            values,
            row_labels,
            column_labels,
        }) = data.charts.get("regional_performance")
        {
            let heatmap_data = HeatMapData {
                values: values.clone(),
                row_labels: row_labels.clone(),
                column_labels: column_labels.clone(),
            };
            builder = builder.add_component(Box::new(HeatMap::new(heatmap_data)));
        }

        // Data table
        if let Some(table_data) = data.tables.get("sales_detail") {
            let pivot = PivotTable::new(table_data.clone());
            builder = builder.add_component(Box::new(pivot));
        }

        builder.build()
    }
}

impl Default for SalesDashboardTemplate {
    fn default() -> Self {
        Self::new()
    }
}

/// Financial Report Template
///
/// Designed for financial reporting with:
/// - Financial KPIs (revenue, profit, margins)
/// - Revenue trend over time
/// - Expense breakdown
/// - Financial ratios table
#[derive(Debug, Clone)]
pub struct FinancialReportTemplate {
    title: String,
    subtitle: Option<String>,
    theme: String,
}

impl FinancialReportTemplate {
    /// Create new financial report template
    pub fn new() -> Self {
        Self {
            title: "Financial Report".to_string(),
            subtitle: None,
            theme: "corporate".to_string(),
        }
    }

    /// Set dashboard title
    pub fn title(mut self, title: impl Into<String>) -> Self {
        self.title = title.into();
        self
    }

    /// Set dashboard subtitle
    pub fn subtitle(mut self, subtitle: impl Into<String>) -> Self {
        self.subtitle = Some(subtitle.into());
        self
    }

    /// Set theme
    pub fn theme(mut self, theme: impl Into<String>) -> Self {
        self.theme = theme.into();
        self
    }

    /// Build dashboard from template data
    pub fn build(self, data: TemplateData) -> Result<Dashboard, PdfError> {
        let mut builder = DashboardBuilder::new()
            .title(self.title)
            .theme_by_name(&self.theme);

        if let Some(subtitle) = self.subtitle {
            builder = builder.subtitle(subtitle);
        }

        // Add financial KPIs
        if !data.kpis.is_empty() {
            let kpi_cards: Vec<KpiCard> = data
                .kpis
                .into_iter()
                .map(|kpi| {
                    let mut card_builder = KpiCardBuilder::new(&kpi.name, &kpi.value);

                    if let Some(subtitle) = kpi.subtitle {
                        card_builder = card_builder.subtitle(&subtitle);
                    }

                    if let (Some(trend_value), Some(trend_dir)) =
                        (kpi.trend_value, kpi.trend_direction)
                    {
                        card_builder = card_builder.trend(trend_value, trend_dir);
                    }

                    if let Some(color) = kpi.color {
                        card_builder = card_builder.color(color);
                    }

                    card_builder.build()
                })
                .collect();

            builder = builder.add_kpi_row(kpi_cards);
        }

        // Revenue trend (line chart)
        if let Some(ChartData::Line { series }) = data.charts.get("revenue_trend") {
            let mut chart_builder = LineChartBuilder::new()
                .title("Revenue Trend")
                .axis_labels("Period", "Revenue")
                .grid(true, Color::gray(0.8), 5);

            for series_data in series {
                let data_series = DataSeries::new(&series_data.name, series_data.color)
                    .xy_data(series_data.data.clone());
                chart_builder = chart_builder.add_series(data_series);
            }

            let chart = chart_builder.build();
            builder = builder.add_component(Box::new(DashboardLineChart::new(chart).span(12)));
        }

        // Expense breakdown
        builder = builder.start_row();

        if let Some(ChartData::Pie { segments }) = data.charts.get("expense_breakdown") {
            let mut chart_builder = PieChartBuilder::new().title("Expense Breakdown");

            for segment in segments {
                chart_builder = chart_builder.add_segment(PieSegment::new(
                    &segment.label,
                    segment.value,
                    segment.color,
                ));
            }

            let chart = chart_builder.show_percentages(true).build();
            builder = builder.add_to_row(Box::new(DashboardPieChart::new(chart).span(6)));
        }

        // Cost structure
        if let Some(ChartData::Bar {
            labels,
            values,
            colors,
        }) = data.charts.get("cost_structure")
        {
            let labeled_values: Vec<(&str, f64)> = labels
                .iter()
                .map(|s| s.as_str())
                .zip(values.iter().copied())
                .collect();

            let mut chart_builder = BarChartBuilder::new()
                .title("Cost Structure")
                .labeled_data(labeled_values)
                .show_grid(true)
                .show_values(true);

            if let Some(color_list) = colors {
                chart_builder = chart_builder.colors(color_list.clone());
            }

            let chart = chart_builder.build();
            builder = builder.add_to_row(Box::new(DashboardBarChart::new(chart).span(6)));
        }

        builder = builder.finish_row();

        // Financial ratios table
        if let Some(table_data) = data.tables.get("financial_ratios") {
            let pivot = PivotTable::new(table_data.clone());
            builder = builder.add_component(Box::new(pivot));
        }

        builder.build()
    }
}

impl Default for FinancialReportTemplate {
    fn default() -> Self {
        Self::new()
    }
}

/// Analytics Dashboard Template
///
/// For data analytics and metrics with:
/// - Key performance indicators
/// - Trend analysis over time
/// - Comparison charts
/// - Data distribution heatmap
#[derive(Debug, Clone)]
pub struct AnalyticsDashboardTemplate {
    title: String,
    subtitle: Option<String>,
    theme: String,
}

impl AnalyticsDashboardTemplate {
    /// Create new analytics dashboard template
    pub fn new() -> Self {
        Self {
            title: "Analytics Dashboard".to_string(),
            subtitle: None,
            theme: "colorful".to_string(),
        }
    }

    /// Set dashboard title
    pub fn title(mut self, title: impl Into<String>) -> Self {
        self.title = title.into();
        self
    }

    /// Set dashboard subtitle
    pub fn subtitle(mut self, subtitle: impl Into<String>) -> Self {
        self.subtitle = Some(subtitle.into());
        self
    }

    /// Set theme
    pub fn theme(mut self, theme: impl Into<String>) -> Self {
        self.theme = theme.into();
        self
    }

    /// Build dashboard from template data
    pub fn build(self, data: TemplateData) -> Result<Dashboard, PdfError> {
        let mut builder = DashboardBuilder::new()
            .title(self.title)
            .theme_by_name(&self.theme);

        if let Some(subtitle) = self.subtitle {
            builder = builder.subtitle(subtitle);
        }

        // Add KPIs
        if !data.kpis.is_empty() {
            let kpi_cards: Vec<KpiCard> = data
                .kpis
                .into_iter()
                .map(|kpi| {
                    let mut card_builder = KpiCardBuilder::new(&kpi.name, &kpi.value);

                    if let Some(subtitle) = kpi.subtitle {
                        card_builder = card_builder.subtitle(&subtitle);
                    }

                    if let (Some(trend_value), Some(trend_dir)) =
                        (kpi.trend_value, kpi.trend_direction)
                    {
                        card_builder = card_builder.trend(trend_value, trend_dir);
                    }

                    if let Some(sparkline) = kpi.sparkline {
                        card_builder = card_builder.sparkline(sparkline);
                    }

                    card_builder.build()
                })
                .collect();

            builder = builder.add_kpi_row(kpi_cards);
        }

        // Trend analysis (line chart - full width)
        if let Some(ChartData::Line { series }) = data.charts.get("trends") {
            let mut chart_builder = LineChartBuilder::new()
                .title("Trend Analysis")
                .axis_labels("Time", "Value")
                .grid(true, Color::gray(0.8), 5);

            for series_data in series {
                let data_series = DataSeries::new(&series_data.name, series_data.color)
                    .xy_data(series_data.data.clone());
                chart_builder = chart_builder.add_series(data_series);
            }

            let chart = chart_builder.build();
            builder = builder.add_component(Box::new(DashboardLineChart::new(chart).span(12)));
        }

        // Comparison charts
        builder = builder.start_row();

        if let Some(ChartData::Bar {
            labels,
            values,
            colors,
        }) = data.charts.get("comparison")
        {
            let labeled_values: Vec<(&str, f64)> = labels
                .iter()
                .map(|s| s.as_str())
                .zip(values.iter().copied())
                .collect();

            let mut chart_builder = BarChartBuilder::new()
                .title("Comparison")
                .labeled_data(labeled_values)
                .show_grid(true)
                .show_values(true);

            if let Some(color_list) = colors {
                chart_builder = chart_builder.colors(color_list.clone());
            }

            let chart = chart_builder.build();
            builder = builder.add_to_row(Box::new(DashboardBarChart::new(chart).span(6)));
        }

        if let Some(ChartData::Pie { segments }) = data.charts.get("distribution") {
            let mut chart_builder = PieChartBuilder::new().title("Distribution");

            for segment in segments {
                chart_builder = chart_builder.add_segment(PieSegment::new(
                    &segment.label,
                    segment.value,
                    segment.color,
                ));
            }

            let chart = chart_builder.show_percentages(true).build();
            builder = builder.add_to_row(Box::new(DashboardPieChart::new(chart).span(6)));
        }

        builder = builder.finish_row();

        // Heatmap
        if let Some(ChartData::HeatMap {
            values,
            row_labels,
            column_labels,
        }) = data.charts.get("heatmap")
        {
            let heatmap_data = HeatMapData {
                values: values.clone(),
                row_labels: row_labels.clone(),
                column_labels: column_labels.clone(),
            };
            builder = builder.add_component(Box::new(HeatMap::new(heatmap_data)));
        }

        builder.build()
    }
}

impl Default for AnalyticsDashboardTemplate {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_template_data_builder() {
        let data = TemplateData::new()
            .with_kpi("Revenue", "$1.5M", 12.5)
            .with_kpi("Orders", "1,234", 8.0)
            .with_metadata("period", "Q4 2024");

        assert_eq!(data.kpis.len(), 2);
        assert_eq!(data.metadata.get("period"), Some(&"Q4 2024".to_string()));
    }

    #[test]
    fn test_sales_dashboard_template_creation() {
        let template = SalesDashboardTemplate::new()
            .title("Test Sales Dashboard")
            .subtitle("Test Subtitle")
            .theme("corporate");

        assert_eq!(template.title, "Test Sales Dashboard");
        assert_eq!(template.subtitle, Some("Test Subtitle".to_string()));
    }

    #[test]
    fn test_financial_report_template_creation() {
        let template = FinancialReportTemplate::new()
            .title("Q4 Financial Report")
            .theme("minimal");

        assert_eq!(template.title, "Q4 Financial Report");
        assert_eq!(template.theme, "minimal");
    }

    #[test]
    fn test_analytics_dashboard_template_creation() {
        let template = AnalyticsDashboardTemplate::new().title("Analytics Overview");

        assert_eq!(template.title, "Analytics Overview");
        assert_eq!(template.theme, "colorful");
    }

    #[test]
    fn test_sales_dashboard_build_with_kpis() {
        let data = TemplateData::new()
            .with_kpi("Revenue", "$2.5M", 12.5)
            .with_kpi("Orders", "1,247", 8.3);

        let dashboard = SalesDashboardTemplate::new()
            .title("Test Dashboard")
            .build(data);

        assert!(dashboard.is_ok());
        let dashboard = dashboard.unwrap();
        assert_eq!(dashboard.title, "Test Dashboard");
        assert!(dashboard.components.len() >= 2); // At least 2 KPI cards
    }

    #[test]
    fn test_chart_data_variants() {
        let bar_data = ChartData::Bar {
            labels: vec!["A".to_string(), "B".to_string()],
            values: vec![100.0, 200.0],
            colors: None,
        };

        let line_data = ChartData::Line {
            series: vec![SeriesData {
                name: "Series 1".to_string(),
                data: vec![(0.0, 100.0), (1.0, 200.0)],
                color: Color::blue(),
            }],
        };

        let pie_data = ChartData::Pie {
            segments: vec![PieSegmentData {
                label: "Segment A".to_string(),
                value: 50.0,
                color: Color::red(),
            }],
        };

        // Just verify they can be created
        match bar_data {
            ChartData::Bar { .. } => (),
            _ => panic!("Expected Bar variant"),
        }

        match line_data {
            ChartData::Line { .. } => (),
            _ => panic!("Expected Line variant"),
        }

        match pie_data {
            ChartData::Pie { .. } => (),
            _ => panic!("Expected Pie variant"),
        }
    }
}