Skip to main content

iced_plot/
reference_lines.rs

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