From e1816b312e5b5aeaf6155928b957ab5ee4890864 Mon Sep 17 00:00:00 2001 From: JohannesProgrammiert Date: Fri, 26 May 2023 12:12:57 +0200 Subject: [PATCH] plot-axis: Expose more conveniece functions to public API. Add axis labels to demo app --- crates/egui/src/widgets/plot/axis.rs | 5 +-- crates/egui/src/widgets/plot/mod.rs | 50 +++++++++++++++++----- crates/egui_demo_lib/src/demo/plot_demo.rs | 27 ++++++++---- 3 files changed, 61 insertions(+), 21 deletions(-) diff --git a/crates/egui/src/widgets/plot/axis.rs b/crates/egui/src/widgets/plot/axis.rs index a4de812a5..56b856921 100644 --- a/crates/egui/src/widgets/plot/axis.rs +++ b/crates/egui/src/widgets/plot/axis.rs @@ -43,7 +43,7 @@ pub(super) type YAxisWidget = AxisWidget; pub struct AxisHints { pub(super) label: WidgetText, pub(super) formatter: AxisFormatterFn, - digits: usize, + pub(super) digits: usize, pub(super) placement: Placement, } @@ -104,8 +104,7 @@ impl AxisHints { /// Specify maximum number of digits for ticks. /// - /// This is considered by the default tick formatter - /// and affects the width of the internal y-axis widget + /// This is considered by the default tick formatter and affects the width of the y-axis pub fn max_digits(mut self, digits: usize) -> Self { self.digits = digits; self diff --git a/crates/egui/src/widgets/plot/mod.rs b/crates/egui/src/widgets/plot/mod.rs index 76eed0acd..1283cde01 100644 --- a/crates/egui/src/widgets/plot/mod.rs +++ b/crates/egui/src/widgets/plot/mod.rs @@ -565,22 +565,32 @@ impl Plot { self } - /// Set the x axis label of the bottom x-axis + /// Set the x axis label of the main x-axis + /// + /// If no x-axis has been specified so far a new one will be created. pub fn x_axis_label(mut self, label: impl Into) -> Self { - if !self.x_axes.is_empty() { + if self.x_axes.is_empty() { + self.x_axes.push(XAxisHints::default().label(label)) + } else { self.x_axes[0].label = label.into(); } self } - /// Set the y axis label of the left y-axis + /// Set the y axis label of the main y-axis + /// + /// If no y-axis has been specified so far a new one will be created. pub fn y_axis_label(mut self, label: impl Into) -> Self { - if !self.y_axes.is_empty() { + if self.y_axes.is_empty() { + self.y_axes.push(YAxisHints::default().label(label)) + } else { self.y_axes[0].label = label.into(); } self } - /// Set the x-axis position + /// Set the position of the main x-axis + /// + /// > Note: This requires an x-axis to exist, either by [`Self::custom_x_axes`] or [`Self::x_axis_label`] pub fn x_axis_position(mut self, placement: axis::Placement) -> Self { if !self.x_axes.is_empty() { self.x_axes[0].placement = placement; @@ -588,7 +598,9 @@ impl Plot { self } - /// Set the y-axis position + /// Set the position of the main y-axis + /// + /// > Note: This requires a y-axis to exist, either by [`Self::custom_y_axes`] or [`Self::y_axis_label`] pub fn y_axis_position(mut self, placement: axis::Placement) -> Self { if !self.y_axes.is_empty() { self.y_axes[0].placement = placement; @@ -596,7 +608,9 @@ impl Plot { self } - /// Specify custom formatter for ticks on x-axis + /// Specify custom formatter for ticks on the main x-axis + /// + /// > Note: This requires an x-axis to exist, either by [`Self::custom_x_axes`] or [`Self::x_axis_label`] /// /// The first parameter of `fmt` is the raw tick value as `f64`. /// The second paramter is the maximum requested number of characters per tick label. @@ -608,7 +622,9 @@ impl Plot { self } - /// Specify custom formatter for ticks on y-axis + /// Specify custom formatter for ticks on the main y-axis + /// + /// > Note: This requires a y-axis to exist, either by [`Self::custom_y_axes`] or [`Self::y_axis_label`] /// /// The first parameter of `formatter` is the raw tick value as `f64`. /// The second paramter is the maximum requested number of characters per tick label. @@ -620,9 +636,23 @@ impl Plot { self } + /// Set the main y-axis-width by number of digits + /// + /// The default is 5 digits. + /// + /// > Note: This requires a y-axis to exist, either by [`Self::custom_y_axes`] or [`Self::y_axis_label`] + /// + /// > Todo: This is experimental. Changing the font size might break this. + pub fn y_axis_width(mut self, digits: usize) -> Self { + if !self.y_axes.is_empty() { + self.y_axes[0].digits = digits; + } + self + } + /// Set custom configuration for bottom x-axis /// - /// More than one axis may be specified. + /// More than one axis may be specified. The first specified axis is considered the main axis. pub fn custom_x_axes(mut self, hints: Vec) -> Self { self.x_axes = hints; self @@ -630,7 +660,7 @@ impl Plot { /// Set custom configuration for left y-axis /// - /// More than one axis may be specified. + /// More than one axis may be specified. The first specified axis is considered the main axis. pub fn custom_y_axes(mut self, hints: Vec) -> Self { self.y_axes = hints; self diff --git a/crates/egui_demo_lib/src/demo/plot_demo.rs b/crates/egui_demo_lib/src/demo/plot_demo.rs index f10a03ca0..b627e4aaf 100644 --- a/crates/egui_demo_lib/src/demo/plot_demo.rs +++ b/crates/egui_demo_lib/src/demo/plot_demo.rs @@ -264,7 +264,11 @@ impl LineDemo { ui.ctx().request_repaint(); self.time += ui.input(|i| i.unstable_dt).at_most(1.0 / 30.0) as f64; }; - let mut plot = Plot::new("lines_demo").legend(Legend::default()); + let mut plot = Plot::new("lines_demo") + .legend(Legend::default()) + .x_axis_label("") + .y_axis_label("") + .y_axis_width(4); if self.square { plot = plot.view_aspect(1.0); } @@ -421,8 +425,10 @@ impl LegendDemo { ); ui.end_row(); }); - let legend_plot = Plot::new("legend_demo") + .x_axis_label("") + .y_axis_label("") + .y_axis_width(2) .legend(config.clone()) .data_aspect(1.0); legend_plot @@ -550,18 +556,16 @@ impl CustomAxisDemo { ui.label("Zoom in on the X-axis to see hours and minutes"); let x_axes = vec![ - XAxisHints::default() - .label("Time".to_owned()) - .formatter(x_fmt), - XAxisHints::default().label("Value".to_owned()), + XAxisHints::default().label("Time").formatter(x_fmt), + XAxisHints::default().label("Value"), ]; let y_axes = vec![ YAxisHints::default() - .label("Percent".to_owned()) + .label("Percent") .formatter(y_fmt) .max_digits(4), YAxisHints::default() - .label("Absolute".to_owned()) + .label("Absolute") .placement(Placement::Opposite), ]; Plot::new("custom_axes") @@ -660,6 +664,9 @@ impl LinkedAxisDemo { .data_aspect(2.0) .width(150.0) .height(250.0) + .y_axis_label("y") + .y_axis_position(Placement::Opposite) + .y_axis_width(3) .link_axis(link_group_id, self.link_x, self.link_y) .link_cursor(link_group_id, self.link_cursor_x, self.link_cursor_y) .show(ui, LinkedAxisDemo::configure_plot); @@ -668,6 +675,7 @@ impl LinkedAxisDemo { .data_aspect(0.5) .width(250.0) .height(150.0) + .x_axis_label("x") .link_axis(link_group_id, self.link_x, self.link_y) .link_cursor(link_group_id, self.link_cursor_x, self.link_cursor_y) .show(ui, LinkedAxisDemo::configure_plot) @@ -899,6 +907,9 @@ impl ChartsDemo { Plot::new("Normal Distribution Demo") .legend(Legend::default()) .clamp_grid(true) + .x_axis_label("") + .y_axis_label("") + .y_axis_width(3) .allow_zoom(self.allow_zoom) .allow_drag(self.allow_drag) .show(ui, |plot_ui| plot_ui.bar_chart(chart))