Skip to main content

plotkit_core/charts/
stem.rs

1//! Stem chart builder methods.
2//!
3//! Provides a fluent builder API for configuring [`StemArtist`] instances.
4
5use crate::artist::StemArtist;
6use crate::primitives::Color;
7
8impl StemArtist {
9    /// Sets the stem line and marker color.
10    pub fn color(&mut self, c: Color) -> &mut Self {
11        self.color = c;
12        self
13    }
14    /// Sets the legend label.
15    pub fn label(&mut self, l: &str) -> &mut Self {
16        self.label = Some(l.to_string());
17        self
18    }
19    /// Sets the baseline y-value from which stems originate.
20    pub fn baseline(&mut self, b: f64) -> &mut Self {
21        self.baseline = b;
22        self
23    }
24    /// Sets the marker circle diameter in pixels.
25    pub fn marker_size(&mut self, s: f64) -> &mut Self {
26        self.marker_size = s;
27        self
28    }
29    /// Sets the stem line width in pixels.
30    pub fn width(&mut self, w: f64) -> &mut Self {
31        self.line_width = w;
32        self
33    }
34    /// Sets the opacity.
35    pub fn alpha(&mut self, a: f64) -> &mut Self {
36        self.alpha = a.clamp(0.0, 1.0);
37        self
38    }
39}
40
41#[cfg(test)]
42mod tests {
43    use super::*;
44    use crate::series::Series;
45    const T: f64 = 1e-12;
46    fn eq(a: f64, b: f64) -> bool {
47        (a - b).abs() < T
48    }
49    fn s() -> StemArtist {
50        StemArtist {
51            x: Series::new(vec![1.0, 2.0, 3.0, 4.0]),
52            y: Series::new(vec![1.0, 3.0, 2.0, 4.0]),
53            color: Color::TAB_BLUE,
54            line_width: 1.5,
55            marker_size: 6.0,
56            baseline: 0.0,
57            label: None,
58            alpha: 1.0,
59        }
60    }
61    #[test]
62    fn c() {
63        let mut a = s();
64        a.color(Color::TAB_RED);
65        assert_eq!(a.color, Color::TAB_RED);
66    }
67    #[test]
68    fn l() {
69        let mut a = s();
70        assert!(a.label.is_none());
71        a.label("x");
72        assert_eq!(a.label.as_deref(), Some("x"));
73    }
74    #[test]
75    fn lo() {
76        let mut a = s();
77        a.label("a");
78        a.label("b");
79        assert_eq!(a.label.as_deref(), Some("b"));
80    }
81    #[test]
82    fn bl() {
83        let mut a = s();
84        a.baseline(2.5);
85        assert!(eq(a.baseline, 2.5));
86    }
87    #[test]
88    fn bn() {
89        let mut a = s();
90        a.baseline(-1.0);
91        assert!(eq(a.baseline, -1.0));
92    }
93    #[test]
94    fn ms() {
95        let mut a = s();
96        a.marker_size(10.0);
97        assert!(eq(a.marker_size, 10.0));
98    }
99    #[test]
100    fn w() {
101        let mut a = s();
102        a.width(3.0);
103        assert!(eq(a.line_width, 3.0));
104    }
105    #[test]
106    fn ac() {
107        let mut a = s();
108        a.alpha(0.5);
109        assert!(eq(a.alpha, 0.5));
110        a.alpha(-1.0);
111        assert!(eq(a.alpha, 0.0));
112        a.alpha(2.0);
113        assert!(eq(a.alpha, 1.0));
114    }
115    #[test]
116    fn ab() {
117        let mut a = s();
118        a.alpha(0.0);
119        assert!(eq(a.alpha, 0.0));
120        a.alpha(1.0);
121        assert!(eq(a.alpha, 1.0));
122    }
123    #[test]
124    fn ch() {
125        let mut a = s();
126        a.color(Color::TAB_GREEN)
127            .baseline(1.0)
128            .marker_size(8.0)
129            .width(2.0)
130            .label("T")
131            .alpha(0.8);
132        assert_eq!(a.color, Color::TAB_GREEN);
133        assert!(eq(a.baseline, 1.0));
134        assert!(eq(a.marker_size, 8.0));
135        assert!(eq(a.line_width, 2.0));
136        assert_eq!(a.label.as_deref(), Some("T"));
137        assert!(eq(a.alpha, 0.8));
138    }
139    #[test]
140    fn db() {
141        let a = s();
142        assert!(eq(a.baseline, 0.0));
143    }
144    #[test]
145    fn dm() {
146        let a = s();
147        assert!(eq(a.marker_size, 6.0));
148    }
149}