Skip to content

Add a "show in legend" toggle for items to hide them from the legend #85

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
2 changes: 1 addition & 1 deletion .github/pull_request_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Please read the "Making a PR" section of [`CONTRIBUTING.md`](https://github.com/
* Keep your PR:s small and focused.
* The PR title is what ends up in the changelog, so make it descriptive!
* If applicable, add a screenshot or gif.
* If it is a non-trivial addition, consider adding a demo f or a new example.
* If it is a non-trivial addition, consider adding a demo for a new example.
* Do NOT open PR:s from your `master` or `main` branch, as that makes it hard for maintainers to test and add commits to your PR.
* Remember to run `cargo fmt` and `cargo clippy`.
* Open the PR as a draft until you have self-reviewed it and run `./scripts/check.sh`.
Expand Down
11 changes: 11 additions & 0 deletions egui_plot/src/items/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,20 @@ pub struct PlotItemBase {
id: Id,
highlight: bool,
allow_hover: bool,
pub(crate) show_in_legend: bool,
}

impl PlotItemBase {
/// The given name will show up in the legend and serves to track the visibility of items.
/// Multiple items may share the same name, in which case they also share a legend entry.
Comment on lines +41 to +42
Copy link
Collaborator

Choose a reason for hiding this comment

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

well that's not entirely true though, it's the id that tracks all this. It's just that by default the id is created from that name.

Suggested change
/// The given name will show up in the legend and serves to track the visibility of items.
/// Multiple items may share the same name, in which case they also share a legend entry.
/// The given name is used to create the item's id and will show up in the legend.
///
/// Multiple items may share the same name (and id), in which case they also share a legend entry.
/// The item's id is used to for visibility tracking and to refer to items in the plot's response.

should probably expand doc on the id setter as well.

pub fn new(name: String) -> Self {
let id = Id::new(&name);
Self {
name,
id,
highlight: false,
allow_hover: true,
show_in_legend: true,
}
}
}
Expand Down Expand Up @@ -84,6 +88,13 @@ macro_rules! builder_methods_for_base {
self.base_mut().id = id.into();
self
}

/// Whether to show the item in the legend. Default: `true`.
Copy link
Collaborator

Choose a reason for hiding this comment

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

should check if empty names show up and update doc accordingly

#[inline]
pub fn show_in_legend(mut self, show: bool) -> Self {
self.base_mut().show_in_legend = show;
self
}
};
}

Expand Down
1 change: 1 addition & 0 deletions egui_plot/src/legend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ impl LegendWidget {
items
.iter()
.filter(|item| !item.name().is_empty())
.filter(|item| item.base().show_in_legend)
.for_each(|item| {
let next_entry = entries.len();
let key = if config.follow_insertion_order {
Expand Down
Loading