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