dear_implot/plots/
line.rs

1//! Line plot implementation
2
3use super::{Plot, PlotError, validate_data_lengths, with_plot_str_or_empty};
4use crate::{LineFlags, sys};
5
6/// Builder for line plots with extensive customization options
7pub struct LinePlot<'a> {
8    label: &'a str,
9    x_data: &'a [f64],
10    y_data: &'a [f64],
11    flags: LineFlags,
12    offset: i32,
13    stride: i32,
14}
15
16impl<'a> LinePlot<'a> {
17    /// Create a new line plot with the given label and data
18    pub fn new(label: &'a str, x_data: &'a [f64], y_data: &'a [f64]) -> Self {
19        Self {
20            label,
21            x_data,
22            y_data,
23            flags: LineFlags::NONE,
24            offset: 0,
25            stride: std::mem::size_of::<f64>() as i32,
26        }
27    }
28
29    /// Set line flags for customization
30    pub fn with_flags(mut self, flags: LineFlags) -> Self {
31        self.flags = flags;
32        self
33    }
34
35    /// Set data offset for partial plotting
36    pub fn with_offset(mut self, offset: i32) -> Self {
37        self.offset = offset;
38        self
39    }
40
41    /// Set data stride for non-contiguous data
42    pub fn with_stride(mut self, stride: i32) -> Self {
43        self.stride = stride;
44        self
45    }
46
47    /// Validate the plot data
48    pub fn validate(&self) -> Result<(), PlotError> {
49        validate_data_lengths(self.x_data, self.y_data)
50    }
51}
52
53impl<'a> Plot for LinePlot<'a> {
54    fn plot(&self) {
55        if self.validate().is_err() {
56            return; // Skip plotting if data is invalid
57        }
58        let Ok(count) = i32::try_from(self.x_data.len()) else {
59            return;
60        };
61
62        with_plot_str_or_empty(self.label, |label_ptr| unsafe {
63            sys::ImPlot_PlotLine_doublePtrdoublePtr(
64                label_ptr,
65                self.x_data.as_ptr(),
66                self.y_data.as_ptr(),
67                count,
68                self.flags.bits() as i32,
69                self.offset,
70                self.stride,
71            );
72        })
73    }
74
75    fn label(&self) -> &str {
76        self.label
77    }
78}
79
80/// Simple line plot for quick plotting without builder pattern
81pub struct SimpleLinePlot<'a> {
82    label: &'a str,
83    values: &'a [f64],
84    x_scale: f64,
85    x_start: f64,
86}
87
88impl<'a> SimpleLinePlot<'a> {
89    /// Create a simple line plot with Y values only (X will be indices)
90    pub fn new(label: &'a str, values: &'a [f64]) -> Self {
91        Self {
92            label,
93            values,
94            x_scale: 1.0,
95            x_start: 0.0,
96        }
97    }
98
99    /// Set X scale factor
100    pub fn with_x_scale(mut self, scale: f64) -> Self {
101        self.x_scale = scale;
102        self
103    }
104
105    /// Set X start value
106    pub fn with_x_start(mut self, start: f64) -> Self {
107        self.x_start = start;
108        self
109    }
110}
111
112impl<'a> Plot for SimpleLinePlot<'a> {
113    fn plot(&self) {
114        if self.values.is_empty() {
115            return;
116        }
117        let Ok(count) = i32::try_from(self.values.len()) else {
118            return;
119        };
120
121        // For simple line plots, we use the single-array version
122        // This would require a wrapper function in the sys crate
123        // For now, we'll create temporary X data
124        let x_data: Vec<f64> = (0..self.values.len())
125            .map(|i| self.x_start + i as f64 * self.x_scale)
126            .collect();
127
128        with_plot_str_or_empty(self.label, |label_ptr| unsafe {
129            sys::ImPlot_PlotLine_doublePtrdoublePtr(
130                label_ptr,
131                x_data.as_ptr(),
132                self.values.as_ptr(),
133                count,
134                0,
135                0,
136                std::mem::size_of::<f64>() as i32,
137            );
138        })
139    }
140
141    fn label(&self) -> &str {
142        self.label
143    }
144}
145
146/// Convenience functions for quick line plotting
147impl<'ui> crate::PlotUi<'ui> {
148    /// Plot a line with X and Y data
149    pub fn line_plot(&self, label: &str, x_data: &[f64], y_data: &[f64]) -> Result<(), PlotError> {
150        let plot = LinePlot::new(label, x_data, y_data);
151        plot.validate()?;
152        plot.plot();
153        Ok(())
154    }
155
156    /// Plot a simple line with Y values only (X will be indices)
157    pub fn simple_line_plot(&self, label: &str, values: &[f64]) -> Result<(), PlotError> {
158        if values.is_empty() {
159            return Err(PlotError::EmptyData);
160        }
161        let plot = SimpleLinePlot::new(label, values);
162        plot.plot();
163        Ok(())
164    }
165}
166
167#[cfg(test)]
168mod tests {
169    use super::*;
170
171    #[test]
172    fn test_line_plot_creation() {
173        let x_data = [1.0, 2.0, 3.0, 4.0];
174        let y_data = [1.0, 4.0, 2.0, 3.0];
175
176        let plot = LinePlot::new("test", &x_data, &y_data);
177        assert_eq!(plot.label(), "test");
178        assert!(plot.validate().is_ok());
179    }
180
181    #[test]
182    fn test_line_plot_validation() {
183        let x_data = [1.0, 2.0, 3.0];
184        let y_data = [1.0, 4.0]; // Different length
185
186        let plot = LinePlot::new("test", &x_data, &y_data);
187        assert!(plot.validate().is_err());
188    }
189
190    #[test]
191    fn test_simple_line_plot() {
192        let values = [1.0, 2.0, 3.0, 4.0];
193        let plot = SimpleLinePlot::new("test", &values);
194        assert_eq!(plot.label(), "test");
195    }
196}