plotpy/
super_title_params.rs

1use std::fmt::Write;
2
3/// Holds parameters for the SuperTitle
4#[derive(Clone)]
5pub struct SuperTitleParams {
6    /// The x location of the text in figure coordinates (default = 0.5)
7    x: Option<f64>,
8
9    /// The y location of the text in figure coordinates (default = 0.98)
10    y: Option<f64>,
11
12    /// The horizontal alignment of the text relative to (x, y) (default = "center")
13    ///
14    /// Options: "center", "left", "right"
15    align_horizontal: String,
16
17    /// The vertical alignment of the text relative to (x, y) (default = "top")
18    ///
19    /// Options: "top", "center", "bottom", "baseline"
20    align_vertical: String,
21
22    /// The font size of the text
23    fontsize: f64,
24
25    /// The font weight of the text
26    fontweight: f64,
27}
28
29impl SuperTitleParams {
30    /// Allocates a new instance
31    pub fn new() -> Self {
32        SuperTitleParams {
33            x: None,
34            y: None,
35            align_horizontal: String::new(),
36            align_vertical: String::new(),
37            fontsize: 0.0,
38            fontweight: 0.0,
39        }
40    }
41
42    /// Sets the x location of the text in figure coordinates (default = 0.5)
43    pub fn set_x(&mut self, value: f64) -> &mut Self {
44        self.x = Some(value);
45        self
46    }
47
48    /// Sets the y location of the text in figure coordinates (default = 0.98)
49    pub fn set_y(&mut self, value: f64) -> &mut Self {
50        self.y = Some(value);
51        self
52    }
53
54    /// Sets the horizontal alignment of the text relative to (x, y) (default = "center")
55    ///
56    /// Options: "center", "left", "right"
57    pub fn set_align_horizontal(&mut self, value: &str) -> &mut Self {
58        self.align_horizontal = String::from(value);
59        self
60    }
61
62    /// Sets the vertical alignment of the text relative to (x, y) (default = "top")
63    ///
64    /// Options: "top", "center", "bottom", "baseline"
65    pub fn set_align_vertical(&mut self, value: &str) -> &mut Self {
66        self.align_vertical = String::from(value);
67        self
68    }
69
70    /// Sets the font size of the text
71    pub fn set_fontsize(&mut self, value: f64) -> &mut Self {
72        self.fontsize = value;
73        self
74    }
75
76    /// Sets the font weight of the text
77    pub fn set_fontweight(&mut self, value: f64) -> &mut Self {
78        self.fontweight = value;
79        self
80    }
81
82    /// Returns options for SuperTitle
83    pub(crate) fn options(&self) -> String {
84        let mut opt = String::new();
85        if let Some(v) = self.x {
86            write!(&mut opt, ",x={}", v).unwrap()
87        }
88        if let Some(v) = self.y {
89            write!(&mut opt, ",y={}", v).unwrap()
90        }
91        if self.align_horizontal != "" {
92            write!(&mut opt, ",ha='{}'", self.align_horizontal).unwrap()
93        }
94        if self.align_vertical != "" {
95            write!(&mut opt, ",va='{}'", self.align_vertical).unwrap()
96        }
97        if self.fontsize > 0.0 {
98            write!(&mut opt, ",fontsize={}", self.fontsize).unwrap()
99        }
100        if self.fontweight > 0.0 {
101            write!(&mut opt, ",fontweight={}", self.fontweight).unwrap()
102        }
103        opt
104    }
105}
106
107////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
108
109#[cfg(test)]
110mod tests {
111    use super::SuperTitleParams;
112
113    #[test]
114    fn new_works() {
115        let params = SuperTitleParams::new();
116        assert_eq!(params.x, None);
117        assert_eq!(params.y, None);
118        assert_eq!(params.align_horizontal.len(), 0);
119        assert_eq!(params.align_vertical.len(), 0);
120        assert_eq!(params.fontsize, 0.0);
121        assert_eq!(params.fontweight, 0.0);
122    }
123
124    #[test]
125    fn clone_works() {
126        let params = SuperTitleParams::new();
127        let clone = params.clone();
128        assert_eq!(clone.fontsize, 0.0);
129    }
130
131    #[test]
132    fn options_works() {
133        let mut params = SuperTitleParams::new();
134        params
135            .set_x(0.6)
136            .set_y(0.8)
137            .set_align_horizontal("center")
138            .set_align_vertical("center")
139            .set_fontsize(8.0)
140            .set_fontweight(10.0);
141        let opt = params.options();
142        assert_eq!(
143            opt,
144            ",x=0.6\
145             ,y=0.8\
146             ,ha='center'\
147             ,va='center'\
148             ,fontsize=8\
149             ,fontweight=10"
150        );
151    }
152}