dear_imgui/widget/
plot.rs

1#![allow(
2    clippy::cast_possible_truncation,
3    clippy::cast_sign_loss,
4    clippy::as_conversions
5)]
6use crate::sys;
7use crate::ui::Ui;
8
9/// # Plot Widgets
10impl Ui {
11    /// Creates a plot lines widget
12    #[doc(alias = "PlotLines")]
13    pub fn plot_lines(&self, label: impl AsRef<str>, values: &[f32]) {
14        self.plot_lines_config(label, values).build()
15    }
16
17    /// Creates a plot histogram widget
18    #[doc(alias = "PlotHistogram")]
19    pub fn plot_histogram(&self, label: impl AsRef<str>, values: &[f32]) {
20        self.plot_histogram_config(label, values).build()
21    }
22
23    /// Creates a plot lines builder
24    pub fn plot_lines_config<'p>(
25        &self,
26        label: impl AsRef<str>,
27        values: &'p [f32],
28    ) -> PlotLines<'_, 'p> {
29        PlotLines::new(self, label, values)
30    }
31
32    /// Creates a plot histogram builder
33    pub fn plot_histogram_config<'p>(
34        &self,
35        label: impl AsRef<str>,
36        values: &'p [f32],
37    ) -> PlotHistogram<'_, 'p> {
38        PlotHistogram::new(self, label, values)
39    }
40}
41
42/// Builder for a plot lines widget
43#[derive(Debug)]
44#[must_use]
45pub struct PlotLines<'ui, 'p> {
46    ui: &'ui Ui,
47    label: String,
48    values: &'p [f32],
49    values_offset: i32,
50    overlay_text: Option<String>,
51    scale_min: f32,
52    scale_max: f32,
53    graph_size: [f32; 2],
54}
55
56impl<'ui, 'p> PlotLines<'ui, 'p> {
57    /// Creates a new plot lines builder
58    pub fn new(ui: &'ui Ui, label: impl AsRef<str>, values: &'p [f32]) -> Self {
59        Self {
60            ui,
61            label: label.as_ref().to_string(),
62            values,
63            values_offset: 0,
64            overlay_text: None,
65            scale_min: f32::MAX,
66            scale_max: f32::MAX,
67            graph_size: [0.0, 0.0],
68        }
69    }
70
71    /// Sets the offset for the values array
72    pub fn values_offset(mut self, offset: i32) -> Self {
73        self.values_offset = offset;
74        self
75    }
76
77    /// Sets the overlay text
78    pub fn overlay_text(mut self, text: impl Into<String>) -> Self {
79        self.overlay_text = Some(text.into());
80        self
81    }
82
83    /// Sets the scale minimum value
84    pub fn scale_min(mut self, scale_min: f32) -> Self {
85        self.scale_min = scale_min;
86        self
87    }
88
89    /// Sets the scale maximum value
90    pub fn scale_max(mut self, scale_max: f32) -> Self {
91        self.scale_max = scale_max;
92        self
93    }
94
95    /// Sets the graph size
96    pub fn graph_size(mut self, size: [f32; 2]) -> Self {
97        self.graph_size = size;
98        self
99    }
100
101    /// Builds the plot lines widget
102    pub fn build(self) {
103        let label_ptr = self.ui.scratch_txt(&self.label);
104        let overlay_ptr = self.ui.scratch_txt_opt(self.overlay_text.as_ref());
105        let graph_size_vec: sys::ImVec2 = self.graph_size.into();
106
107        unsafe {
108            sys::igPlotLines_FloatPtr(
109                label_ptr,
110                self.values.as_ptr(),
111                self.values.len() as i32,
112                self.values_offset,
113                overlay_ptr,
114                self.scale_min,
115                self.scale_max,
116                graph_size_vec,
117                std::mem::size_of::<f32>() as i32,
118            );
119        }
120    }
121}
122
123/// Builder for a plot histogram widget
124#[derive(Debug)]
125#[must_use]
126pub struct PlotHistogram<'ui, 'p> {
127    ui: &'ui Ui,
128    label: String,
129    values: &'p [f32],
130    values_offset: i32,
131    overlay_text: Option<String>,
132    scale_min: f32,
133    scale_max: f32,
134    graph_size: [f32; 2],
135}
136
137impl<'ui, 'p> PlotHistogram<'ui, 'p> {
138    /// Creates a new plot histogram builder
139    pub fn new(ui: &'ui Ui, label: impl AsRef<str>, values: &'p [f32]) -> Self {
140        Self {
141            ui,
142            label: label.as_ref().to_string(),
143            values,
144            values_offset: 0,
145            overlay_text: None,
146            scale_min: f32::MAX,
147            scale_max: f32::MAX,
148            graph_size: [0.0, 0.0],
149        }
150    }
151
152    /// Sets the offset for the values array
153    pub fn values_offset(mut self, offset: i32) -> Self {
154        self.values_offset = offset;
155        self
156    }
157
158    /// Sets the overlay text
159    pub fn overlay_text(mut self, text: impl Into<String>) -> Self {
160        self.overlay_text = Some(text.into());
161        self
162    }
163
164    /// Sets the scale minimum value
165    pub fn scale_min(mut self, scale_min: f32) -> Self {
166        self.scale_min = scale_min;
167        self
168    }
169
170    /// Sets the scale maximum value
171    pub fn scale_max(mut self, scale_max: f32) -> Self {
172        self.scale_max = scale_max;
173        self
174    }
175
176    /// Sets the graph size
177    pub fn graph_size(mut self, size: [f32; 2]) -> Self {
178        self.graph_size = size;
179        self
180    }
181
182    /// Builds the plot histogram widget
183    pub fn build(self) {
184        let label_ptr = self.ui.scratch_txt(&self.label);
185        let overlay_ptr = self.ui.scratch_txt_opt(self.overlay_text.as_ref());
186        let graph_size_vec: sys::ImVec2 = self.graph_size.into();
187
188        unsafe {
189            sys::igPlotHistogram_FloatPtr(
190                label_ptr,
191                self.values.as_ptr(),
192                self.values.len() as i32,
193                self.values_offset,
194                overlay_ptr,
195                self.scale_min,
196                self.scale_max,
197                graph_size_vec,
198                std::mem::size_of::<f32>() as i32,
199            );
200        }
201    }
202}