iced_plot/
reference_lines.rs

1use crate::{Color, LineStyle};
2
3/// A vertical line at a fixed x-coordinate.
4#[derive(Debug, Clone)]
5pub struct VLine {
6    /// The x-coordinate where the vertical line is drawn.
7    pub x: f64,
8    /// Optional label for the line (appears in legend if provided).
9    pub label: Option<String>,
10    /// Color of the line.
11    pub color: Color,
12    /// Line width in pixels.
13    pub width: f32,
14    /// Line style (solid, dashed, dotted).
15    pub line_style: LineStyle,
16}
17
18impl VLine {
19    /// Create a new vertical line at the given x-coordinate.
20    pub fn new(x: f64) -> Self {
21        Self {
22            x,
23            label: None,
24            color: Color::from_rgb(0.5, 0.5, 0.5),
25            width: 1.0,
26            line_style: LineStyle::Solid,
27        }
28    }
29
30    /// Set the label for this line (will appear in legend).
31    pub fn with_label(mut self, label: impl Into<String>) -> Self {
32        let l = label.into();
33        if !l.is_empty() {
34            self.label = Some(l);
35        }
36        self
37    }
38
39    /// Set the color of the line.
40    pub fn with_color(mut self, color: Color) -> Self {
41        self.color = color;
42        self
43    }
44
45    /// Set the line width in pixels.
46    pub fn with_width(mut self, width: f32) -> Self {
47        self.width = width.max(0.5);
48        self
49    }
50
51    /// Set the line style.
52    pub fn with_style(mut self, style: LineStyle) -> Self {
53        self.line_style = style;
54        self
55    }
56}
57
58/// A horizontal line at a fixed y-coordinate.
59#[derive(Debug, Clone)]
60pub struct HLine {
61    /// The y-coordinate where the horizontal line is drawn.
62    pub y: f64,
63    /// Optional label for the line (appears in legend if provided).
64    pub label: Option<String>,
65    /// Color of the line.
66    pub color: Color,
67    /// Line width in pixels.
68    pub width: f32,
69    /// Line style (solid, dashed, dotted).
70    pub line_style: LineStyle,
71}
72
73impl HLine {
74    /// Create a new horizontal line at the given y-coordinate.
75    pub fn new(y: f64) -> Self {
76        Self {
77            y,
78            label: None,
79            color: Color::from_rgb(0.5, 0.5, 0.5),
80            width: 1.0,
81            line_style: LineStyle::Solid,
82        }
83    }
84
85    /// Set the label for this line (will appear in legend).
86    pub fn with_label(mut self, label: impl Into<String>) -> Self {
87        let l = label.into();
88        if !l.is_empty() {
89            self.label = Some(l);
90        }
91        self
92    }
93
94    /// Set the color of the line.
95    pub fn with_color(mut self, color: Color) -> Self {
96        self.color = color;
97        self
98    }
99
100    /// Set the line width in pixels.
101    pub fn with_width(mut self, width: f32) -> Self {
102        self.width = width.max(0.5);
103        self
104    }
105
106    /// Set the line style.
107    pub fn with_style(mut self, style: LineStyle) -> Self {
108        self.line_style = style;
109        self
110    }
111}