Skip to content

Commit

Permalink
Add a public API for overriding plot legend traces' visibilities (#3534)
Browse files Browse the repository at this point in the history
Added a public API in `egui_plot -> legend` to allow `hidden_items` to
be overridden in the plot legend widget. This allows convenient control
of traces' visibilities the selection of traces from the application
code.

### Example
```rust
    let legend_config = if plot_selection_changed {
        let hidden_items = match plot_selection {
            PlotSelection::SelectAll => Vec::new(),
            PlotSelection::DeselectAll => all_trace_names,
        };

        Legend::default()
            .position(Corner::RightTop)
            .hidden_items(hidden_items) // Overrides `hidden_items`
    } else {
        Legend::default().position(Corner::RightTop)
    };

    Plot::new(id)
        .legend(legend_config)
        .show(ui, draw_plot);
```

Closes <emilk/egui#3533>.

---------

Co-authored-by: Emil Ernerfeldt <[email protected]>
  • Loading branch information
jayzhudev and emilk authored Jan 6, 2024
1 parent ce00949 commit b304c6c
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 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,11 @@ 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 = config.hidden_items.as_ref().unwrap_or(hidden_items);

// 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

0 comments on commit b304c6c

Please sign in to comment.