Skip to content

Commit 1e7fd71

Browse files
authored
Make demo a bit more mobile friendly (#44)
1. Change `ui.horizontal()` to `ui.horizontal_wrapped()` for the `PlotDemo` selector so it doesn't go off screen. 2. Wrap several of the demo's "settings" UI's in a `ScrollArea::horizontal`s so they don't go off screen and make the plots go off screen as well. 3. Wrap the plots in the LinkedAxes demo in a `ScrollArea::horizontal` so they don't go off screen. # Before and after screenshots ## Lines Before: <img width="392" alt="Screenshot 2024-09-09 at 7 15 24 PM" src="https://github.com/user-attachments/assets/23063cfe-04bd-4e76-9158-7b6cbaf4f2bf"> After: <img width="398" alt="Screenshot 2024-09-09 at 7 17 13 PM" src="https://github.com/user-attachments/assets/7285dcea-4434-4b5b-b9bc-7cae330de703"> ## Legend Before: <img width="319" alt="Screenshot 2024-09-09 at 7 43 05 PM" src="https://github.com/user-attachments/assets/84a6b78a-dc9e-42df-be3b-cf26d54af848"> After: <img width="315" alt="Screenshot 2024-09-09 at 7 22 37 PM" src="https://github.com/user-attachments/assets/d2225e97-d321-4af0-8f60-4d2591aea17b"> ## Charts Before: <img width="320" alt="Screenshot 2024-09-09 at 7 43 12 PM" src="https://github.com/user-attachments/assets/90b1c287-6b44-42c3-b8f1-417d5c367a4a"> After: <img width="318" alt="Screenshot 2024-09-09 at 7 25 15 PM" src="https://github.com/user-attachments/assets/31793d79-dbb5-42c8-bfe7-435d5b4f7686"> ## Linked Axes Before: <img width="321" alt="Screenshot 2024-09-09 at 7 43 29 PM" src="https://github.com/user-attachments/assets/4622c4f0-c9af-4c85-a5e3-a106ae5ea2f5"> After: <img width="323" alt="Screenshot 2024-09-09 at 7 29 50 PM" src="https://github.com/user-attachments/assets/d008fdcc-2a3c-4f6b-8624-6afa0e38da63">
1 parent d00fa85 commit 1e7fd71

1 file changed

Lines changed: 46 additions & 21 deletions

File tree

demo/src/plot_demo.rs

Lines changed: 46 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
use std::f64::consts::TAU;
22
use std::ops::RangeInclusive;
33

4-
use egui::{remap, vec2, Color32, ComboBox, NumExt, Pos2, Response, Stroke, TextWrapMode, Vec2b};
4+
use egui::{
5+
remap, vec2, Color32, ComboBox, NumExt, Pos2, Response, ScrollArea, Stroke, TextWrapMode, Vec2b,
6+
};
57

68
use egui_plot::{
79
Arrows, AxisHints, Bar, BarChart, BoxElem, BoxPlot, BoxSpread, CoordinatesFormatter, Corner,
@@ -63,7 +65,7 @@ impl PlotDemo {
6365
ui.add(crate::egui_github_link_file!());
6466
});
6567
ui.separator();
66-
ui.horizontal(|ui| {
68+
ui.horizontal_wrapped(|ui| {
6769
ui.selectable_value(&mut self.open_panel, Panel::Lines, "Lines");
6870
ui.selectable_value(&mut self.open_panel, Panel::Markers, "Markers");
6971
ui.selectable_value(&mut self.open_panel, Panel::Legend, "Legend");
@@ -254,7 +256,9 @@ impl LineDemo {
254256

255257
impl LineDemo {
256258
fn ui(&mut self, ui: &mut egui::Ui) -> Response {
257-
self.options_ui(ui);
259+
ScrollArea::horizontal().show(ui, |ui| {
260+
self.options_ui(ui);
261+
});
258262

259263
if self.animate {
260264
ui.ctx().request_repaint();
@@ -392,8 +396,27 @@ impl LegendDemo {
392396
}
393397

394398
fn ui(&mut self, ui: &mut egui::Ui) -> Response {
399+
ScrollArea::horizontal().show(ui, |ui| {
400+
self.settings_ui(ui);
401+
});
402+
395403
let Self { config } = self;
404+
let legend_plot = Plot::new("legend_demo")
405+
.legend(config.clone())
406+
.data_aspect(1.0);
407+
legend_plot
408+
.show(ui, |plot_ui| {
409+
plot_ui.line(Self::line_with_slope(0.5).name("lines"));
410+
plot_ui.line(Self::line_with_slope(1.0).name("lines"));
411+
plot_ui.line(Self::line_with_slope(2.0).name("lines"));
412+
plot_ui.line(Self::sin().name("sin(x)"));
413+
plot_ui.line(Self::cos().name("cos(x)"));
414+
})
415+
.response
416+
}
396417

418+
fn settings_ui(&mut self, ui: &mut egui::Ui) {
419+
let Self { config } = self;
397420
egui::Grid::new("settings").show(ui, |ui| {
398421
ui.label("Text style:");
399422
ui.horizontal(|ui| {
@@ -420,18 +443,6 @@ impl LegendDemo {
420443
);
421444
ui.end_row();
422445
});
423-
let legend_plot = Plot::new("legend_demo")
424-
.legend(config.clone())
425-
.data_aspect(1.0);
426-
legend_plot
427-
.show(ui, |plot_ui| {
428-
plot_ui.line(Self::line_with_slope(0.5).name("lines"));
429-
plot_ui.line(Self::line_with_slope(1.0).name("lines"));
430-
plot_ui.line(Self::line_with_slope(2.0).name("lines"));
431-
plot_ui.line(Self::sin().name("sin(x)"));
432-
plot_ui.line(Self::cos().name("cos(x)"));
433-
})
434-
.response
435446
}
436447
}
437448

@@ -642,6 +653,13 @@ impl LinkedAxesDemo {
642653
ui.checkbox(&mut self.link_cursor_y, "Y");
643654
});
644655

656+
ScrollArea::horizontal()
657+
.show(ui, |ui| self.plots_ui(ui))
658+
.inner
659+
}
660+
661+
fn plots_ui(&self, ui: &mut egui::Ui) -> Response {
662+
ui.style_mut().wrap_mode = Some(TextWrapMode::Extend);
645663
let link_group_id = ui.id().with("linked_demo");
646664
ui.horizontal(|ui| {
647665
Plot::new("left-top")
@@ -877,6 +895,17 @@ impl Default for ChartsDemo {
877895

878896
impl ChartsDemo {
879897
fn ui(&mut self, ui: &mut egui::Ui) -> Response {
898+
ScrollArea::horizontal().show(ui, |ui| {
899+
self.options_ui(ui);
900+
});
901+
match self.chart {
902+
Chart::GaussBars => self.bar_gauss(ui),
903+
Chart::StackedBars => self.bar_stacked(ui),
904+
Chart::BoxPlot => self.box_plot(ui),
905+
}
906+
}
907+
908+
fn options_ui(&mut self, ui: &mut egui::Ui) -> Response {
880909
ui.horizontal(|ui| {
881910
ui.vertical(|ui| {
882911
ui.label("Type:");
@@ -912,12 +941,8 @@ impl ChartsDemo {
912941
});
913942
});
914943
});
915-
});
916-
match self.chart {
917-
Chart::GaussBars => self.bar_gauss(ui),
918-
Chart::StackedBars => self.bar_stacked(ui),
919-
Chart::BoxPlot => self.box_plot(ui),
920-
}
944+
})
945+
.response
921946
}
922947

923948
fn bar_gauss(&self, ui: &mut egui::Ui) -> Response {

0 commit comments

Comments
 (0)