From fe27f60957f3064726f1c14aba143f0454b36254 Mon Sep 17 00:00:00 2001 From: Nicolas Date: Tue, 7 Jan 2025 21:22:21 +0100 Subject: [PATCH] add example --- Cargo.lock | 9 +++++ examples/legend_sort/Cargo.toml | 19 +++++++++++ examples/legend_sort/README.md | 5 +++ examples/legend_sort/src/main.rs | 58 ++++++++++++++++++++++++++++++++ 4 files changed, 91 insertions(+) create mode 100644 examples/legend_sort/Cargo.toml create mode 100644 examples/legend_sort/README.md create mode 100644 examples/legend_sort/src/main.rs diff --git a/Cargo.lock b/Cargo.lock index ec0d82c..97ba072 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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" diff --git a/examples/legend_sort/Cargo.toml b/examples/legend_sort/Cargo.toml new file mode 100644 index 0000000..d5e9914 --- /dev/null +++ b/examples/legend_sort/Cargo.toml @@ -0,0 +1,19 @@ +[package] +name = "legend_sort" +version = "0.1.0" +authors = ["hacknus "] +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", +] } diff --git a/examples/legend_sort/README.md b/examples/legend_sort/README.md new file mode 100644 index 0000000..4d458e6 --- /dev/null +++ b/examples/legend_sort/README.md @@ -0,0 +1,5 @@ +This example shows how to sort the legend entries. + +```sh +cargo run -p legend_sort +``` diff --git a/examples/legend_sort/src/main.rs b/examples/legend_sort/src/main.rs new file mode 100644 index 0000000..ca8358a --- /dev/null +++ b/examples/legend_sort/src/main.rs @@ -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 + }); + } +}