Skip to content
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

Add a public API for overriding plot legend traces' visibilities #3534

Merged
merged 4 commits into from
Jan 6, 2024
Merged
Changes from 3 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
24 changes: 23 additions & 1 deletion crates/egui_plot/src/legend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ pub struct Legend {
pub text_style: TextStyle,
pub background_alpha: f32,
pub position: Corner,

/// Used for overriding the `hidden_items` set in [`LegendWidget`].
hidden_items: Option<ahash::HashSet<String>>,
}

impl Default for Legend {
Expand All @@ -40,6 +43,8 @@ impl Default for Legend {
text_style: TextStyle::Body,
background_alpha: 0.75,
position: Corner::RightTop,

hidden_items: None,
}
}
}
Expand All @@ -65,6 +70,17 @@ impl Legend {
self.position = corner;
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]
pub fn hidden_items<I>(mut self, hidden_items: I) -> Self
where
I: IntoIterator<Item = String>,
{
self.hidden_items = Some(hidden_items.into_iter().collect());
self
}
}

#[derive(Clone)]
Expand Down Expand Up @@ -167,8 +183,14 @@ impl LegendWidget {
rect: Rect,
config: Legend,
items: &[Box<dyn PlotItem>],
hidden_items: &ahash::HashSet<String>,
hidden_items: &ahash::HashSet<String>, // Existing hiddent items in the plot memory.
) -> Option<Self> {
// If `config.hidden_items` is not `None`, it is used.
let hidden_items = match &config.hidden_items {
Some(h) => h,
None => hidden_items,
};
emilk marked this conversation as resolved.
Show resolved Hide resolved

// Collect the legend entries. If multiple items have the same name, they share a
// checkbox. If their colors don't match, we pick a neutral color for the checkbox.
let mut entries: BTreeMap<String, LegendEntry> = BTreeMap::new();
Expand Down