dear_implot/plots/
inf_lines.rs1use super::{Plot, PlotError, safe_cstring};
4use crate::{InfLinesFlags, sys};
5
6pub struct InfLinesPlot<'a> {
8 label: &'a str,
9 positions: &'a [f64],
10 flags: InfLinesFlags,
11 offset: i32,
12 stride: i32,
13}
14
15impl<'a> InfLinesPlot<'a> {
16 pub fn new(label: &'a str, positions: &'a [f64]) -> Self {
18 Self {
19 label,
20 positions,
21 flags: InfLinesFlags::NONE,
22 offset: 0,
23 stride: std::mem::size_of::<f64>() as i32,
24 }
25 }
26
27 pub fn horizontal(mut self) -> Self {
29 self.flags |= InfLinesFlags::HORIZONTAL;
30 self
31 }
32
33 pub fn with_offset(mut self, offset: i32) -> Self {
35 self.offset = offset;
36 self
37 }
38
39 pub fn with_stride(mut self, stride: i32) -> Self {
41 self.stride = stride;
42 self
43 }
44
45 pub fn validate(&self) -> Result<(), PlotError> {
47 if self.positions.is_empty() {
48 return Err(PlotError::EmptyData);
49 }
50 Ok(())
51 }
52}
53
54impl<'a> Plot for InfLinesPlot<'a> {
55 fn plot(&self) {
56 if self.validate().is_err() {
57 return;
58 }
59 let label_c = safe_cstring(self.label);
60 unsafe {
61 sys::ImPlot_PlotInfLines_doublePtr(
62 label_c.as_ptr(),
63 self.positions.as_ptr(),
64 self.positions.len() as i32,
65 self.flags.bits() as i32,
66 self.offset,
67 self.stride,
68 );
69 }
70 }
71
72 fn label(&self) -> &str {
73 self.label
74 }
75}
76
77impl<'ui> crate::PlotUi<'ui> {
79 pub fn inf_lines_vertical(&self, label: &str, xs: &[f64]) -> Result<(), PlotError> {
81 let plot = InfLinesPlot::new(label, xs);
82 plot.validate()?;
83 plot.plot();
84 Ok(())
85 }
86
87 pub fn inf_lines_horizontal(&self, label: &str, ys: &[f64]) -> Result<(), PlotError> {
89 let plot = InfLinesPlot::new(label, ys).horizontal();
90 plot.validate()?;
91 plot.plot();
92 Ok(())
93 }
94}