dear_imgui_rs/widget/
plot.rs

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