Skip to main content

plotlars_core/components/
header.rs

1use crate::components::Rgb;
2
3/// A structure representing header formatting for tables.
4///
5/// The `Header` struct allows customization of table headers including custom values,
6/// height, alignment, font, and fill color.
7///
8/// # Example
9///
10/// ```rust
11/// use plotlars::{Table, Header, Plot, Text, Rgb};
12/// use polars::prelude::*;
13///
14/// let dataset = df![
15///     "name" => &["Alice", "Bob", "Charlie"],
16///     "age" => &[25, 30, 35],
17///     "city" => &["New York", "London", "Tokyo"]
18/// ]
19/// .unwrap();
20///
21/// let header = Header::new()
22///     .values(vec!["Full Name", "Years", "Location"])
23///     .height(40.0)
24///     .align("center")
25///     .font("Arial")
26///     .fill(Rgb(200, 200, 200));
27///
28/// Table::builder()
29///     .data(&dataset)
30///     .columns(vec!["name", "age", "city"])
31///     .header(&header)
32///     .plot_title(Text::from("Employee Information"))
33///     .build()
34///     .plot();
35/// ```
36///
37/// ![Example](https://imgur.com/J2XhcUt.png)
38#[derive(Clone, Default)]
39pub struct Header {
40    pub values: Option<Vec<String>>,
41    pub height: Option<f64>,
42    pub align: Option<String>,
43    pub font: Option<String>,
44    pub fill: Option<Rgb>,
45}
46
47impl Header {
48    /// Creates a new `Header` instance with default values.
49    pub fn new() -> Self {
50        Self::default()
51    }
52
53    /// Sets custom header values.
54    ///
55    /// # Argument
56    ///
57    /// * `values` - A vector of string slices representing custom header names.
58    pub fn values(mut self, values: Vec<&str>) -> Self {
59        self.values = Some(values.into_iter().map(|s| s.to_string()).collect());
60        self
61    }
62
63    /// Sets the height of the header.
64    ///
65    /// # Argument
66    ///
67    /// * `height` - A `f64` value specifying the header height.
68    pub fn height(mut self, height: f64) -> Self {
69        self.height = Some(height);
70        self
71    }
72
73    /// Sets the alignment of the header text.
74    ///
75    /// # Argument
76    ///
77    /// * `align` - A string specifying the alignment (left, center, right).
78    pub fn align(mut self, align: impl Into<String>) -> Self {
79        self.align = Some(align.into());
80        self
81    }
82
83    /// Sets the font family of the header text.
84    ///
85    /// # Argument
86    ///
87    /// * `font` - A string slice specifying the font family name.
88    pub fn font(mut self, font: &str) -> Self {
89        self.font = Some(font.to_string());
90        self
91    }
92
93    /// Sets the fill color of the header.
94    ///
95    /// # Argument
96    ///
97    /// * `fill` - An `Rgb` value specifying the background color.
98    pub fn fill(mut self, fill: Rgb) -> Self {
99        self.fill = Some(fill);
100        self
101    }
102}
103
104#[cfg(test)]
105mod tests {
106    use super::*;
107
108    #[test]
109    fn test_default() {
110        let header = Header::new();
111        assert!(header.values.is_none());
112        assert!(header.height.is_none());
113        assert!(header.align.is_none());
114        assert!(header.font.is_none());
115        assert!(header.fill.is_none());
116    }
117
118    #[test]
119    fn test_values() {
120        let header = Header::new().values(vec!["A", "B"]);
121        let values = header.values.unwrap();
122        assert_eq!(values, vec!["A".to_string(), "B".to_string()]);
123    }
124
125    #[test]
126    fn test_height() {
127        let header = Header::new().height(30.0);
128        assert!((header.height.unwrap() - 30.0).abs() < 1e-6);
129    }
130
131    #[test]
132    fn test_align() {
133        let header = Header::new().align("left");
134        assert_eq!(header.align, Some("left".to_string()));
135    }
136
137    #[test]
138    fn test_fill() {
139        let header = Header::new().fill(Rgb(200, 200, 200));
140        let fill = header.fill.unwrap();
141        assert_eq!(fill.0, 200);
142        assert_eq!(fill.1, 200);
143        assert_eq!(fill.2, 200);
144    }
145}