Skip to content

Commit 67b578c

Browse files
committed
Add option to display multiple plots, and resize the plot panel
This adds the ability to display multiple plots, where each column can be toggled individually per plot. Additionally, it adds interactive resize using the seperator between the plots and the serial monitor. The resize code might not be the most pretty implementation, but I was unable to find any example of this in the egui examples. Therefore this commit also includes a small refactor of how heights are compted. One thing left out of this commit, is the ability to save plots of any but the first plot.
1 parent 55c2309 commit 67b578c

File tree

1 file changed

+70
-29
lines changed

1 file changed

+70
-29
lines changed

src/gui.rs

Lines changed: 70 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use eframe::egui::panel::Side;
99
use eframe::egui::plot::{log_grid_spacer, Legend, Line, Plot, PlotPoint, PlotPoints};
1010
use eframe::egui::{
1111
global_dark_light_mode_buttons, Align2, ColorImage, FontFamily, FontId, KeyboardShortcut, Pos2,
12-
Vec2, Visuals,
12+
Sense, Vec2, Visuals,
1313
};
1414
use eframe::{egui, Storage};
1515
use preferences::Preferences;
@@ -20,7 +20,7 @@ use crate::data::{DataContainer, SerialDirection};
2020
use crate::serial::{save_serial_settings, Device, SerialDevices};
2121
use crate::toggle::toggle;
2222
use crate::FileOptions;
23-
use crate::{vec2, APP_INFO, PREFS_KEY};
23+
use crate::{APP_INFO, PREFS_KEY};
2424

2525
const MAX_FPS: f64 = 60.0;
2626

@@ -174,6 +174,8 @@ pub struct MyApp {
174174
device_idx: usize,
175175
serial_devices: SerialDevices,
176176
plotting_range: usize,
177+
number_of_plots: usize,
178+
plot_serial_display_ratio: f32,
177179
console: Vec<Print>,
178180
picked_path: PathBuf,
179181
plot_location: egui::Rect,
@@ -236,6 +238,8 @@ impl MyApp {
236238
send_tx,
237239
clear_tx,
238240
plotting_range: usize::MAX,
241+
number_of_plots: 1,
242+
plot_serial_display_ratio: 0.45,
239243
command: "".to_string(),
240244
show_sent_cmds: true,
241245
show_timestamps: true,
@@ -301,9 +305,13 @@ impl MyApp {
301305

302306
fn draw_central_panel(&mut self, ctx: &egui::Context) {
303307
egui::CentralPanel::default().show(ctx, |ui| {
304-
let height = ui.available_size().y * 0.45;
305308
let border = 10.0;
306-
let spacing = (ui.available_size().y - 2.0 * height) / 3.5 - border;
309+
310+
let panel_height = ui.available_size().y;
311+
let height = ui.available_size().y * self.plot_serial_display_ratio;
312+
let plots_height = height;
313+
let plot_height = plots_height / (self.number_of_plots as f32);
314+
let spacing = 5.0;
307315
let width = ui.available_size().x - 2.0 * border - RIGHT_PANEL_WIDTH;
308316

309317
ui.add_space(spacing);
@@ -331,32 +339,56 @@ impl MyApp {
331339
}
332340

333341
let t_fmt = |x, _range: &RangeInclusive<f64>| format!("{:4.2} s", x);
334-
let signal_plot = Plot::new("data")
335-
.height(height)
336-
.width(width)
337-
.legend(Legend::default())
338-
.x_grid_spacer(log_grid_spacer(10))
339-
.y_grid_spacer(log_grid_spacer(10))
340-
.x_axis_formatter(t_fmt)
341-
.min_size(vec2(50.0, 100.0));
342-
343-
let plot_inner = signal_plot.show(ui, |signal_plot_ui| {
344-
for (i, graph) in graphs.into_iter().enumerate() {
345-
// this check needs to be here for when we change devices (not very elegant)
346-
if i < self.serial_devices.labels[self.device_idx].len() {
347-
signal_plot_ui.line(
348-
Line::new(PlotPoints::Owned(graph))
349-
.name(&self.serial_devices.labels[self.device_idx][i]),
350-
);
342+
343+
let plots_ui = ui.vertical(|ui| {
344+
for graph_idx in 0..self.number_of_plots {
345+
if graph_idx != 0 {
346+
ui.separator();
351347
}
348+
349+
let signal_plot = Plot::new(format!("data-{graph_idx}"))
350+
.height(plot_height)
351+
.width(width)
352+
.legend(Legend::default())
353+
.x_grid_spacer(log_grid_spacer(10))
354+
.y_grid_spacer(log_grid_spacer(10))
355+
.x_axis_formatter(t_fmt);
356+
357+
let plot_inner = signal_plot.show(ui, |signal_plot_ui| {
358+
for (i, graph) in graphs.iter().enumerate() {
359+
// this check needs to be here for when we change devices (not very elegant)
360+
if i < self.serial_devices.labels[self.device_idx].len() {
361+
signal_plot_ui.line(
362+
Line::new(PlotPoints::Owned(graph.to_vec())).name(
363+
&self.serial_devices.labels[self.device_idx][i],
364+
),
365+
);
366+
}
367+
}
368+
});
369+
370+
self.plot_location = plot_inner.response.rect;
352371
}
372+
let separator_response = ui.separator();
373+
let resize_y = ui
374+
.interact(
375+
separator_response.rect,
376+
separator_response.id,
377+
Sense::click_and_drag(),
378+
)
379+
.on_hover_cursor(egui::CursorIcon::ResizeVertical)
380+
.drag_delta()
381+
.y;
382+
383+
self.plot_serial_display_ratio = (self.plot_serial_display_ratio
384+
+ resize_y / panel_height)
385+
.clamp(0.1, 0.9);
386+
387+
ui.add_space(spacing);
353388
});
354389

355-
self.plot_location = plot_inner.response.rect;
356-
357-
ui.add_space(spacing);
358-
ui.separator();
359-
ui.add_space(spacing);
390+
let serial_height =
391+
panel_height - plots_ui.response.rect.height() - border * 2.0 - spacing;
360392

361393
let num_rows = self.data.raw_traffic.len();
362394
let row_height = ui.text_style_height(&egui::TextStyle::Body);
@@ -372,8 +404,8 @@ impl MyApp {
372404
.auto_shrink([false; 2])
373405
.stick_to_bottom(true)
374406
.enable_scrolling(true)
375-
.max_height(height - spacing)
376-
.min_scrolled_height(height - spacing)
407+
.max_height(serial_height - spacing)
408+
.min_scrolled_height(serial_height - spacing)
377409
.max_width(width)
378410
.show_rows(ui, row_height, num_rows, |ui, row_range| {
379411
let content: String = row_range
@@ -645,6 +677,15 @@ impl MyApp {
645677
}
646678
});
647679
ui.end_row();
680+
ui.label("Number of plots [#]: ");
681+
682+
ui.horizontal(|ui| {
683+
ui.add(egui::DragValue::new(&mut self.number_of_plots)
684+
.clamp_range(1..=10))
685+
.on_hover_text("Select the number of plots to be shown.");
686+
687+
});
688+
ui.end_row();
648689
ui.label("Show Sent Commands");
649690
ui.add(toggle(&mut self.show_sent_cmds))
650691
.on_hover_text("Show sent commands in console.");
@@ -662,7 +703,7 @@ impl MyApp {
662703
ui.end_row();
663704
ui.end_row();
664705

665-
if ui.button("Save CSV")
706+
if ui.button("Save CSV")
666707
.on_hover_text("Save Plot Data to CSV.")
667708
.clicked() || ui.input_mut(|i| i.consume_shortcut(&SAVE_FILE_SHORTCUT))
668709
{

0 commit comments

Comments
 (0)