egui_plotter/charts/
timedata.rs

1//! Animatable chart with data on the Y and time on the X axis
2
3use egui::Ui;
4use plotters::style::{RGBAColor, ShapeStyle};
5
6use crate::charts::XyTimeData;
7
8/// Animatable chart with time on the X axis and data on the Y axis.
9///
10/// ## Usage
11/// **Ensure the `timechart` feature is enabled to use this type.**
12///
13/// Creating the chart is very simple. You only need to provide 4 parameters,
14/// 3 of which are just strings.
15///
16///  * `points`: A slice of tuples, arranged so that the first float is the time
17///  and the second is the data.
18///  * `unit`: String describing the data on the Y axis.
19///  * `caption`: String to be shown as the caption of the chart.
20///
21/// This will create a basic line chart with nothing fancy, which you can easily
22/// add to your egui project. You can also animate this chart with `.toggle_playback()`
23/// and adjust various parameters with the many `.set_` functions included.
24pub struct TimeData {
25    chart: XyTimeData,
26}
27
28impl TimeData {
29    /// Create a new TimeData chart. See [Usage](#usage).
30    pub fn new(points: &[(f32, f32)], unit: &str, caption: &str) -> Self {
31        let points: Vec<(f32, f32, f32)> = points
32            .into_iter()
33            .map(|(data, time)| (*data, *time, *time))
34            .collect();
35
36        let chart = XyTimeData::new(&points, "seconds", unit, caption);
37
38        Self { chart }
39    }
40
41    /// Set the time to resume playback at. Time is in seconds.
42    #[inline]
43    pub fn set_time(&mut self, time: f32) {
44        self.chart.set_time(time)
45    }
46
47    /// Set the time to resume playback at. Time is in seconds. Consumes self.
48    #[inline]
49    pub fn time(mut self, time: f32) -> Self {
50        self.set_time(time);
51
52        self
53    }
54
55    /// Set the playback speed. 1.0 is normal speed, 2.0 is double, & 0.5 is half.
56    #[inline]
57    pub fn set_playback_speed(&mut self, speed: f32) {
58        self.chart.set_playback_speed(speed)
59    }
60
61    /// Set the playback speed. 1.0 is normal speed, 2.0 is double, & 0.5 is half. Consumes self.
62    #[inline]
63    pub fn playback_speed(mut self, speed: f32) -> Self {
64        self.set_playback_speed(speed);
65
66        self
67    }
68
69    #[inline]
70    /// Set the style of the plotted line.
71    pub fn set_line_style(&mut self, line_style: ShapeStyle) {
72        self.chart.set_line_style(line_style)
73    }
74
75    #[inline]
76    /// Set the style of the plotted line. Consumes self.
77    pub fn line_style(mut self, line_style: ShapeStyle) -> Self {
78        self.set_line_style(line_style);
79
80        self
81    }
82
83    #[inline]
84    /// Set the style of the grid.
85    pub fn set_grid_style(&mut self, grid_style: ShapeStyle) {
86        self.chart.set_grid_style(grid_style)
87    }
88
89    #[inline]
90    /// Set the style of the grid. Consumes self.
91    pub fn grid_style(mut self, grid_style: ShapeStyle) -> Self {
92        self.set_grid_style(grid_style);
93
94        self
95    }
96
97    #[inline]
98    /// Set the style of the subgrid.
99    pub fn set_subgrid_style(&mut self, subgrid_style: ShapeStyle) {
100        self.chart.set_subgrid_style(subgrid_style)
101    }
102
103    #[inline]
104    /// Set the style of the subgrid. Consumes self.
105    pub fn subgrid_style(mut self, subgrid_style: ShapeStyle) -> Self {
106        self.set_subgrid_style(subgrid_style);
107
108        self
109    }
110
111    #[inline]
112    /// Set the style of the axes.
113    pub fn set_axes_style(&mut self, axes_style: ShapeStyle) {
114        self.chart.set_axes_style(axes_style)
115    }
116
117    #[inline]
118    /// Set the style of the plotted line. Consumes self.
119    pub fn axes_style(mut self, axes_style: ShapeStyle) -> Self {
120        self.set_axes_style(axes_style);
121
122        self
123    }
124
125    #[inline]
126    /// Set the text color of the chart.
127    pub fn set_text_color<T>(&mut self, color: T)
128    where
129        T: Into<RGBAColor>,
130    {
131        self.chart.set_text_color(color)
132    }
133
134    #[inline]
135    /// Set the text color of the chart. Consumes self.
136    pub fn text_color<T>(mut self, color: T) -> Self
137    where
138        T: Into<RGBAColor>,
139    {
140        self.set_text_color(color);
141
142        self
143    }
144
145    #[inline]
146    /// Set the background color of the chart.
147    pub fn set_background_color<T>(&mut self, color: T)
148    where
149        T: Into<RGBAColor>,
150    {
151        self.chart.set_background_color(color);
152    }
153
154    #[inline]
155    /// Set the background color of the chart. Consumes self.
156    pub fn background_color<T>(mut self, color: T) -> Self
157    where
158        T: Into<RGBAColor>,
159    {
160        self.set_background_color(color);
161
162        self
163    }
164
165    #[inline]
166    /// Set the ratio between X and Y values, default being 1 x unit to 1 y unit.
167    pub fn set_ratio(&mut self, ratio: f32) {
168        self.chart.set_ratio(ratio);
169    }
170
171    #[inline]
172    /// Set the ratio between X and Y values, default being 1 x unit to 1 y unit. Consumes self.
173    pub fn ratio(mut self, ratio: f32) -> Self {
174        self.set_ratio(ratio);
175
176        self
177    }
178
179    /// Draw the chart to a Ui. Will also proceed to animate the chart if playback is currently
180    /// enabled.
181    pub fn draw(&mut self, ui: &Ui) {
182        self.chart.draw(ui)
183    }
184
185    /// Start/enable playback of the chart.
186    #[inline]
187    pub fn start_playback(&mut self) {
188        self.chart.start_playback()
189    }
190
191    /// Stop/disable playback of the chart.
192    #[inline]
193    pub fn stop_playback(&mut self) {
194        self.chart.stop_playback()
195    }
196
197    /// Toggle playback of the chart.
198    pub fn toggle_playback(&mut self) {
199        self.chart.toggle_playback()
200    }
201
202    /// Return true if playback is currently enabled & underway.
203    #[inline]
204    pub fn is_playing(&self) -> bool {
205        self.chart.is_playing()
206    }
207
208    /// Return the time the chart starts at when playback is enabled.
209    #[inline]
210    pub fn start_time(&self) -> f32 {
211        self.chart.start_time()
212    }
213
214    /// Return the current time to be animated when playback is enabled.
215    #[inline]
216    pub fn current_time(&mut self) -> f32 {
217        self.chart.current_time()
218    }
219
220    /// Return the time the chart finished animating at when playback is enabled.
221    #[inline]
222    pub fn end_time(&self) -> f32 {
223        self.chart.end_time()
224    }
225
226    /// Return the speed the chart is animated at.
227    #[inline]
228    pub fn get_playback_speed(&self) -> f32 {
229        self.chart.get_playback_speed()
230    }
231}