Skip to content

Add optional legend title #105

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions demo/src/plot_demo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ impl LineDemo {
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())
.legend(Legend::default().title("Lines"))
.show_axes(self.show_axes)
.show_grid(self.show_grid);
if self.square {
Expand Down Expand Up @@ -417,7 +417,7 @@ impl MarkerDemo {

let markers_plot = Plot::new("markers_demo")
.data_aspect(1.0)
.legend(Legend::default());
.legend(Legend::default().title("Markers"));
markers_plot
.show(ui, |plot_ui| {
for marker in self.markers() {
Expand Down Expand Up @@ -805,7 +805,11 @@ impl ItemsDemo {
);

let plot = Plot::new("items_demo")
.legend(Legend::default().position(Corner::RightBottom))
.legend(
Legend::default()
.position(Corner::RightBottom)
.title("Items"),
)
.show_x(false)
.show_y(false)
.data_aspect(1.0);
Expand Down
2 changes: 1 addition & 1 deletion demo/tests/plot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fn test_demos() {
let mut errors = Vec::new();

for name in demo_names {
harness.get_by_label(&name).click();
harness.get_by_role_and_label(Role::Button, &name).click();
harness.run();

if let Err(error) = harness.try_snapshot(&format!("demos/{name}")) {
Expand Down
4 changes: 2 additions & 2 deletions demo/tests/snapshots/demos/Items.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions demo/tests/snapshots/demos/Lines.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions demo/tests/snapshots/demos/Markers.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions demo/tests/snapshots/light_mode.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions demo/tests/snapshots/scale_0.50.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions demo/tests/snapshots/scale_1.00.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions demo/tests/snapshots/scale_1.39.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions demo/tests/snapshots/scale_2.00.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions egui_plot/src/legend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ pub struct Legend {
pub text_style: TextStyle,
pub background_alpha: f32,
pub position: Corner,
pub title: Option<String>,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could use WidgetText instead for more customizability

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doesn't work as WidgetText does not implement serde - we could do this here too but i dont know if that is good


follow_insertion_order: bool,
color_conflict_handling: ColorConflictHandling,
Expand All @@ -60,6 +61,7 @@ impl Default for Legend {
text_style: TextStyle::Body,
background_alpha: 0.75,
position: Corner::RightTop,
title: None,
follow_insertion_order: false,
color_conflict_handling: ColorConflictHandling::RemoveColor,
hidden_items: None,
Expand Down Expand Up @@ -89,6 +91,13 @@ impl Legend {
self
}

/// Set the title of the legend. Default: `None`.
#[inline]
pub fn title(mut self, title: &str) -> Self {
self.title = Some(title.to_owned());
self
}

/// Specifies hidden items in the legend configuration to override the existing ones. This
/// allows the legend traces' visibility to be controlled from the application code.
#[inline]
Expand Down Expand Up @@ -329,6 +338,12 @@ impl Widget for &mut LegendWidget {
.multiply_with_opacity(config.background_alpha);
background_frame
.show(ui, |ui| {
// always show on top of the legend - so we need to use a new scope
if main_dir == Direction::TopDown {
if let Some(title) = &config.title {
ui.heading(title);
}
}
let mut focus_on_item = None;

let response_union = entries
Expand All @@ -348,6 +363,12 @@ impl Widget for &mut LegendWidget {
.reduce(|r1, r2| r1.union(r2))
.expect("No entries in the legend");

if main_dir == Direction::BottomUp {
if let Some(title) = &config.title {
ui.heading(title);
}
}

if let Some(focus_on_item) = focus_on_item {
handle_focus_on_legend_item(&focus_on_item, entries);
}
Expand Down
Loading