Skip to main content

plotkit_core/charts/
step.rs

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