Skip to content

Commit 75ed22b

Browse files
authored
Merge pull request #83 from oeb25/main
Add option to display multiple plots, and resize the plot panel
2 parents 55c2309 + 8cd5fec commit 75ed22b

File tree

2 files changed

+81
-29
lines changed

2 files changed

+81
-29
lines changed

src/gui.rs

Lines changed: 79 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,7 @@ pub struct MyApp {
174174
device_idx: usize,
175175
serial_devices: SerialDevices,
176176
plotting_range: usize,
177+
plot_serial_display_ratio: f32,
177178
console: Vec<Print>,
178179
picked_path: PathBuf,
179180
plot_location: egui::Rect,
@@ -236,6 +237,7 @@ impl MyApp {
236237
send_tx,
237238
clear_tx,
238239
plotting_range: usize::MAX,
240+
plot_serial_display_ratio: 0.45,
239241
command: "".to_string(),
240242
show_sent_cmds: true,
241243
show_timestamps: true,
@@ -301,9 +303,13 @@ impl MyApp {
301303

302304
fn draw_central_panel(&mut self, ctx: &egui::Context) {
303305
egui::CentralPanel::default().show(ctx, |ui| {
304-
let height = ui.available_size().y * 0.45;
305306
let border = 10.0;
306-
let spacing = (ui.available_size().y - 2.0 * height) / 3.5 - border;
307+
308+
let panel_height = ui.available_size().y;
309+
let height = ui.available_size().y * self.plot_serial_display_ratio;
310+
let plots_height = height;
311+
let plot_height = plots_height / (self.serial_devices.number_of_plots as f32);
312+
let spacing = 5.0;
307313
let width = ui.available_size().x - 2.0 * border - RIGHT_PANEL_WIDTH;
308314

309315
ui.add_space(spacing);
@@ -331,32 +337,59 @@ impl MyApp {
331337
}
332338

333339
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-
);
340+
341+
let plots_ui = ui.vertical(|ui| {
342+
for graph_idx in 0..self.serial_devices.number_of_plots {
343+
if graph_idx != 0 {
344+
ui.separator();
351345
}
346+
347+
let signal_plot = Plot::new(format!("data-{graph_idx}"))
348+
.height(plot_height)
349+
.width(width)
350+
.legend(Legend::default())
351+
.x_grid_spacer(log_grid_spacer(10))
352+
.y_grid_spacer(log_grid_spacer(10))
353+
.x_axis_formatter(t_fmt);
354+
355+
let plot_inner = signal_plot.show(ui, |signal_plot_ui| {
356+
for (i, graph) in graphs.iter().enumerate() {
357+
// this check needs to be here for when we change devices (not very elegant)
358+
if i < self.serial_devices.labels[self.device_idx].len() {
359+
signal_plot_ui.line(
360+
Line::new(PlotPoints::Owned(graph.to_vec())).name(
361+
&self.serial_devices.labels[self.device_idx][i],
362+
),
363+
);
364+
}
365+
}
366+
});
367+
368+
self.plot_location = plot_inner.response.rect;
352369
}
353-
});
370+
let separator_response = ui.separator();
371+
let separator = ui
372+
.interact(
373+
separator_response.rect,
374+
separator_response.id,
375+
Sense::click_and_drag(),
376+
)
377+
.on_hover_cursor(egui::CursorIcon::ResizeVertical);
378+
379+
let resize_y = separator.drag_delta().y;
380+
381+
if separator.double_clicked() {
382+
self.plot_serial_display_ratio = 0.45;
383+
}
384+
self.plot_serial_display_ratio = (self.plot_serial_display_ratio
385+
+ resize_y / panel_height)
386+
.clamp(0.1, 0.9);
354387

355-
self.plot_location = plot_inner.response.rect;
388+
ui.add_space(spacing);
389+
});
356390

357-
ui.add_space(spacing);
358-
ui.separator();
359-
ui.add_space(spacing);
391+
let serial_height =
392+
panel_height - plots_ui.response.rect.height() - border * 2.0 - spacing;
360393

361394
let num_rows = self.data.raw_traffic.len();
362395
let row_height = ui.text_style_height(&egui::TextStyle::Body);
@@ -372,8 +405,8 @@ impl MyApp {
372405
.auto_shrink([false; 2])
373406
.stick_to_bottom(true)
374407
.enable_scrolling(true)
375-
.max_height(height - spacing)
376-
.min_scrolled_height(height - spacing)
408+
.max_height(serial_height - spacing)
409+
.min_scrolled_height(serial_height - spacing)
377410
.max_width(width)
378411
.show_rows(ui, row_height, num_rows, |ui, row_range| {
379412
let content: String = row_range
@@ -644,6 +677,23 @@ impl MyApp {
644677
self.plotting_range = usize::MAX;
645678
}
646679
});
680+
ui.end_row();
681+
ui.label("Number of plots [#]: ");
682+
683+
ui.horizontal(|ui| {
684+
if ui.button("<").clicked() {
685+
self.serial_devices.number_of_plots =
686+
(self.serial_devices.number_of_plots - 1).clamp(1, 10);
687+
}
688+
ui.add(egui::DragValue::new(&mut self.serial_devices.number_of_plots)
689+
.clamp_range(1..=10))
690+
.on_hover_text("Select the number of plots to be shown.");
691+
if ui.button(">").clicked() {
692+
self.serial_devices.number_of_plots =
693+
(self.serial_devices.number_of_plots + 1).clamp(1, 10);
694+
}
695+
});
696+
647697
ui.end_row();
648698
ui.label("Show Sent Commands");
649699
ui.add(toggle(&mut self.show_sent_cmds))
@@ -662,7 +712,7 @@ impl MyApp {
662712
ui.end_row();
663713
ui.end_row();
664714

665-
if ui.button("Save CSV")
715+
if ui.button("Save CSV")
666716
.on_hover_text("Save Plot Data to CSV.")
667717
.clicked() || ui.input_mut(|i| i.consume_shortcut(&SAVE_FILE_SHORTCUT))
668718
{

src/serial.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@ use crate::{print_to_console, Packet, Print, APP_INFO, PREFS_KEY_SERIAL};
1414
pub struct SerialDevices {
1515
pub devices: Vec<Device>,
1616
pub labels: Vec<Vec<String>>,
17+
pub number_of_plots: usize,
1718
}
1819

1920
impl Default for SerialDevices {
2021
fn default() -> Self {
2122
SerialDevices {
2223
devices: vec![Device::default()],
2324
labels: vec![vec!["Column 0".to_string()]],
25+
number_of_plots: 1,
2426
}
2527
}
2628
}

0 commit comments

Comments
 (0)