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 { self.color = c; self }
11    /// Sets the step line width.
12    pub fn width(&mut self, w: f64) -> &mut Self { self.width = w; self }
13    /// Sets the step alignment mode.
14    pub fn where_step(&mut self, w: crate::artist::StepWhere) -> &mut Self { self.where_step = w; self }
15    /// Sets the legend label.
16    pub fn label(&mut self, l: &str) -> &mut Self { self.label = Some(l.to_string()); self }
17    /// Sets the opacity.
18    pub fn alpha(&mut self, a: f64) -> &mut Self { self.alpha = a.clamp(0.0, 1.0); self }
19}
20
21#[cfg(test)] mod tests { use super::*; use crate::artist::StepWhere; use crate::series::Series;
22    const T: f64 = 1e-12; fn eq(a:f64,b:f64)->bool{(a-b).abs()<T}
23    fn s()->StepArtist{StepArtist{x:Series::new(vec![1.0,2.0,3.0,4.0]),y:Series::new(vec![1.0,3.0,2.0,4.0]),color:Color::TAB_BLUE,width:1.5,where_step:StepWhere::Pre,label:None,alpha:1.0}}
24    #[test]fn c(){let mut a=s();a.color(Color::TAB_RED);assert_eq!(a.color,Color::TAB_RED);}
25    #[test]fn w(){let mut a=s();a.width(3.0);assert!(eq(a.width,3.0));}
26    #[test]fn wp(){let mut a=s();a.where_step(StepWhere::Pre);assert!(matches!(a.where_step,StepWhere::Pre));}
27    #[test]fn wpo(){let mut a=s();a.where_step(StepWhere::Post);assert!(matches!(a.where_step,StepWhere::Post));}
28    #[test]fn wm(){let mut a=s();a.where_step(StepWhere::Mid);assert!(matches!(a.where_step,StepWhere::Mid));}
29    #[test]fn l(){let mut a=s();assert!(a.label.is_none());a.label("x");assert_eq!(a.label.as_deref(),Some("x"));}
30    #[test]fn lo(){let mut a=s();a.label("a");a.label("b");assert_eq!(a.label.as_deref(),Some("b"));}
31    #[test]fn ac(){let mut a=s();a.alpha(0.5);assert!(eq(a.alpha,0.5));a.alpha(-1.0);assert!(eq(a.alpha,0.0));a.alpha(2.0);assert!(eq(a.alpha,1.0));}
32    #[test]fn ab(){let mut a=s();a.alpha(0.0);assert!(eq(a.alpha,0.0));a.alpha(1.0);assert!(eq(a.alpha,1.0));}
33    #[test]fn ch(){let mut a=s();a.color(Color::TAB_GREEN).width(2.0).where_step(StepWhere::Post).label("T").alpha(0.8);assert_eq!(a.color,Color::TAB_GREEN);assert!(eq(a.width,2.0));assert!(matches!(a.where_step,StepWhere::Post));assert_eq!(a.label.as_deref(),Some("T"));assert!(eq(a.alpha,0.8));}
34    #[test]fn dp(){let a=s();assert!(matches!(a.where_step,StepWhere::Pre));}
35}