Add methods for optionally displaying the background/axes of a `Plot`
* Add methods for optionally displaying the background/axes of a `Plot`
These are particularly useful when using the `Plot` widget as an overlay
over an existing grid or some other content.
* Allow for showing each axis of a `Plot` individually
@@ -71,6 +71,8 @@ pub struct Plot {
show_x: bool,
show_y: bool,
legend_config: Option<Legend>,
+ show_background: bool,
+ show_axes: [bool; 2],
}
impl Plot {
@@ -98,6 +100,8 @@ impl Plot {
show_x: true,
show_y: true,
legend_config: None,
+ show_background: true,
+ show_axes: [true; 2],
}
}
@@ -315,6 +319,22 @@ impl Plot {
self.legend_config = Some(legend);
self
}
+
+ /// Whether or not to show the background `Rect`.
+ /// Can be useful to disable if the plot is overlaid over existing content.
+ /// Default: `true`.
+ pub fn show_background(mut self, show: bool) -> Self {
+ self.show_background = show;
+ self
+ }
+
+ /// Show the axes.
+ /// Can be useful to disable if the plot is overlaid over an existing grid or content.
+ /// Default: `[true; 2]`.
+ pub fn show_axes(mut self, show: [bool; 2]) -> Self {
+ self.show_axes = show;
+ self
+ }
}
impl Widget for Plot {
@@ -337,6 +357,8 @@ impl Widget for Plot {
mut show_x,
mut show_y,
legend_config,
+ show_background,
+ show_axes,
} = self;
let plot_id = ui.make_persistent_id(id_source);
@@ -400,16 +422,19 @@ impl Widget for Plot {
let plot_painter = ui.painter().sub_region(rect);
// Background
+ if show_background {
plot_painter.add(Shape::Rect {
rect,
corner_radius: 2.0,
fill: ui.visuals().extreme_bg_color,
stroke: ui.visuals().widgets.noninteractive.bg_stroke,
});
+ }
// Legend
let legend = legend_config
.and_then(|config| LegendWidget::try_new(rect, config, &items, &hidden_items));
+
// Don't show hover cursor when hovering over legend.
if hovered_entry.is_some() {
show_x = false;
@@ -495,6 +520,7 @@ impl Widget for Plot {
items,
show_x,
show_y,
+ show_axes,
transform,
};
prepared.ui(ui, &response);
@@ -528,6 +554,7 @@ struct Prepared {
items: Vec<Box<dyn PlotItem>>,
show_x: bool,
show_y: bool,
+ show_axes: [bool; 2],
transform: ScreenTransform,
}
@@ -536,8 +563,10 @@ impl Prepared {
let mut shapes = Vec::new();
for d in 0..2 {
+ if self.show_axes[d] {
self.paint_axis(ui, d, &mut shapes);
}
+ }
let transform = &self.transform;
