Skip to content

Commit

Permalink
add example
Browse files Browse the repository at this point in the history
  • Loading branch information
bircni committed Jan 7, 2025
1 parent 05456be commit fe27f60
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 0 deletions.
9 changes: 9 additions & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1781,6 +1781,15 @@ version = "3.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2db585e1d738fc771bf08a151420d3ed193d9d895a36df7f6f8a9456b911ddc"

[[package]]
name = "legend_sort"
version = "0.1.0"
dependencies = [
"eframe",
"egui_plot",
"env_logger",
]

[[package]]
name = "libc"
version = "0.2.167"
Expand Down
19 changes: 19 additions & 0 deletions examples/legend_sort/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[package]
name = "legend_sort"
version = "0.1.0"
authors = ["hacknus <[email protected]>"]
license = "MIT OR Apache-2.0"
edition = "2021"
rust-version = "1.80"
publish = false

[lints]
workspace = true

[dependencies]
eframe = { workspace = true, features = ["default"] }
egui_plot.workspace = true
env_logger = { workspace = true, default-features = false, features = [
"auto-color",
"humantime",
] }
5 changes: 5 additions & 0 deletions examples/legend_sort/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
This example shows how to sort the legend entries.

```sh
cargo run -p legend_sort
```
58 changes: 58 additions & 0 deletions examples/legend_sort/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release
#![allow(rustdoc::missing_crate_level_docs)] // it's an example

use eframe::egui;
use egui_plot::{Legend, Line, Plot, PlotPoints};

fn main() -> eframe::Result {
env_logger::init(); // Log to stderr (if you run with `RUST_LOG=debug`).

let options = eframe::NativeOptions {
viewport: egui::ViewportBuilder::default().with_inner_size([350.0, 200.0]),
..Default::default()
};
let graph: Vec<[f64; 2]> = vec![[0.0, 1.0], [2.0, 3.0], [3.0, 2.0]];
let graph2: Vec<[f64; 2]> = vec![[0.0, 2.0], [2.0, 4.0], [3.0, 3.0]];
let graph3: Vec<[f64; 2]> = vec![[0.0, 3.0], [2.0, 5.0], [3.0, 4.0]];

eframe::run_native(
"My egui App with a plot",
options,
Box::new(|_cc| {
Ok(Box::new(MyApp {
insert_order: false,
graph,
graph2,
graph3,
}))
}),
)
}

#[derive(Default)]
struct MyApp {
insert_order: bool,
graph: Vec<[f64; 2]>,
graph2: Vec<[f64; 2]>,
graph3: Vec<[f64; 2]>,
}

impl eframe::App for MyApp {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
egui::CentralPanel::default().show(ctx, |ui| {
ui.label("If checked the legend will follow the order as the curves are inserted");
ui.checkbox(&mut self.insert_order, "Insert order");

Plot::new("My Plot")
.legend(Legend::default().follow_insertion_order(self.insert_order))
.show(ui, |plot_ui| {
plot_ui
.line(Line::new(PlotPoints::from(self.graph3.clone())).name("3rd Curve"));
plot_ui.line(Line::new(PlotPoints::from(self.graph.clone())).name("1st Curve"));
plot_ui
.line(Line::new(PlotPoints::from(self.graph2.clone())).name("2nd Curve"));
});
// Remember the position of the plot
});
}
}

0 comments on commit fe27f60

Please sign in to comment.