Tidy up plot demo

This commit is contained in:
Emil Ernerfeldt 2021-06-24 15:20:31 +02:00
parent 6e3604ee4b
commit 182eb32b95

View file

@ -18,7 +18,7 @@ struct LineDemo {
impl Default for LineDemo { impl Default for LineDemo {
fn default() -> Self { fn default() -> Self {
Self { Self {
animate: true, animate: !cfg!(debug_assertions),
time: 0.0, time: 0.0,
circle_radius: 1.5, circle_radius: 1.5,
circle_center: Pos2::new(0.0, 0.0), circle_center: Pos2::new(0.0, 0.0),
@ -68,8 +68,10 @@ impl LineDemo {
ui.vertical(|ui| { ui.vertical(|ui| {
ui.style_mut().wrap = Some(false); ui.style_mut().wrap = Some(false);
ui.checkbox(animate, "animate"); ui.checkbox(animate, "animate");
ui.checkbox(square, "square view"); ui.checkbox(square, "square view")
ui.checkbox(proportional, "proportional data axes"); .on_hover_text("Always keep the viewport square.");
ui.checkbox(proportional, "Proportional data axes")
.on_hover_text("Tick are the same size on both axes.");
}); });
}); });
} }
@ -138,7 +140,7 @@ impl Widget for &mut LineDemo {
struct MarkerDemo { struct MarkerDemo {
fill_markers: bool, fill_markers: bool,
marker_radius: f32, marker_radius: f32,
custom_marker_color: bool, automatic_colors: bool,
marker_color: Color32, marker_color: Color32,
} }
@ -147,8 +149,8 @@ impl Default for MarkerDemo {
Self { Self {
fill_markers: true, fill_markers: true,
marker_radius: 5.0, marker_radius: 5.0,
custom_marker_color: false, automatic_colors: true,
marker_color: Color32::GRAY, marker_color: Color32::GREEN,
} }
} }
} }
@ -173,7 +175,7 @@ impl MarkerDemo {
.radius(self.marker_radius) .radius(self.marker_radius)
.shape(marker); .shape(marker);
if self.custom_marker_color { if !self.automatic_colors {
points = points.color(self.marker_color); points = points.color(self.marker_color);
} }
@ -186,15 +188,15 @@ impl MarkerDemo {
impl Widget for &mut MarkerDemo { impl Widget for &mut MarkerDemo {
fn ui(self, ui: &mut Ui) -> Response { fn ui(self, ui: &mut Ui) -> Response {
ui.horizontal(|ui| { ui.horizontal(|ui| {
ui.checkbox(&mut self.fill_markers, "fill markers"); ui.checkbox(&mut self.fill_markers, "Fill");
ui.add( ui.add(
egui::DragValue::new(&mut self.marker_radius) egui::DragValue::new(&mut self.marker_radius)
.speed(0.1) .speed(0.1)
.clamp_range(0.0..=f64::INFINITY) .clamp_range(0.0..=f64::INFINITY)
.prefix("marker radius: "), .prefix("Radius: "),
); );
ui.checkbox(&mut self.custom_marker_color, "custom marker color"); ui.checkbox(&mut self.automatic_colors, "Automatic colors");
if self.custom_marker_color { if !self.automatic_colors {
ui.color_edit_button_srgba(&mut self.marker_color); ui.color_edit_button_srgba(&mut self.marker_color);
} }
}); });
@ -238,24 +240,32 @@ impl Widget for &mut LegendDemo {
fn ui(self, ui: &mut Ui) -> Response { fn ui(self, ui: &mut Ui) -> Response {
let LegendDemo { config } = self; let LegendDemo { config } = self;
ui.label("Text Style:"); egui::Grid::new("settings").show(ui, |ui| {
ui.horizontal(|ui| { ui.label("Text style:");
TextStyle::all().for_each(|style| { ui.horizontal(|ui| {
ui.selectable_value(&mut config.text_style, style, format!("{:?}", style)); TextStyle::all().for_each(|style| {
ui.selectable_value(&mut config.text_style, style, format!("{:?}", style));
});
}); });
}); ui.end_row();
ui.label("Position:");
ui.horizontal(|ui| { ui.label("Position:");
Corner::all().for_each(|position| { ui.horizontal(|ui| {
ui.selectable_value(&mut config.position, position, format!("{:?}", position)); Corner::all().for_each(|position| {
ui.selectable_value(&mut config.position, position, format!("{:?}", position));
});
}); });
ui.end_row();
ui.label("Opacity:");
ui.add(
egui::DragValue::new(&mut config.background_alpha)
.speed(0.02)
.clamp_range(0.0..=1.0),
);
ui.end_row();
}); });
ui.label("Background alpha:");
ui.add(
egui::DragValue::new(&mut config.background_alpha)
.speed(0.02)
.clamp_range(0.0..=1.0),
);
let legend_plot = Plot::new("Legend Demo") let legend_plot = Plot::new("Legend Demo")
.line(LegendDemo::line_with_slope(0.5).name("lines")) .line(LegendDemo::line_with_slope(0.5).name("lines"))
.line(LegendDemo::line_with_slope(1.0).name("lines")) .line(LegendDemo::line_with_slope(1.0).name("lines"))
@ -377,18 +387,20 @@ impl super::Demo for PlotDemo {
impl super::View for PlotDemo { impl super::View for PlotDemo {
fn ui(&mut self, ui: &mut Ui) { fn ui(&mut self, ui: &mut Ui) {
ui.vertical_centered(|ui| { ui.horizontal(|ui| {
egui::reset_button(ui, self); egui::reset_button(ui, self);
ui.add(crate::__egui_github_link_file!()); ui.collapsing("Instructions", |ui| {
ui.label("Pan by dragging, or scroll (+ shift = horizontal)."); ui.label("Pan by dragging, or scroll (+ shift = horizontal).");
if cfg!(target_arch = "wasm32") { if cfg!(target_arch = "wasm32") {
ui.label("Zoom with ctrl / ⌘ + mouse wheel, or with pinch gesture."); ui.label("Zoom with ctrl / ⌘ + mouse wheel, or with pinch gesture.");
} else if cfg!(target_os = "macos") { } else if cfg!(target_os = "macos") {
ui.label("Zoom with ctrl / ⌘ + scroll."); ui.label("Zoom with ctrl / ⌘ + scroll.");
} else { } else {
ui.label("Zoom with ctrl + scroll."); ui.label("Zoom with ctrl + scroll.");
} }
ui.label("Reset view with double-click."); ui.label("Reset view with double-click.");
ui.add(crate::__egui_github_link_file!());
});
}); });
ui.separator(); ui.separator();
ui.horizontal(|ui| { ui.horizontal(|ui| {