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

Added an example to save plot to image #2769

Merged
merged 22 commits into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
Changes from 14 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
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions examples/save_plot/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "save_plot"
version = "0.1.0"
authors = ["hacknus <l_stoeckli@bluewin.ch>"]
license = "MIT OR Apache-2.0"
edition = "2021"
rust-version = "1.65"
publish = false

[dependencies]
eframe = { path = "../../crates/eframe", features = [
"__screenshot", # __screenshot is so we can dump a screenshot using EFRAME_SCREENSHOT_TO
] }
tracing-subscriber = "0.3"
rfd = "0.11.0"
image = { version = "0.24", default-features = false, features = ["png"] }
5 changes: 5 additions & 0 deletions examples/save_plot/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
This example shows that you can save a plot in egui as a png.

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

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

fn main() -> Result<(), eframe::Error> {
// Log to stdout (if you run with `RUST_LOG=debug`).
tracing_subscriber::fmt::init();

let options = eframe::NativeOptions {
initial_window_size: Some(egui::vec2(350.0, 400.0)),
..Default::default()
};
eframe::run_native(
"My egui App with a plot",
options,
Box::new(|_cc| Box::new(MyApp::default())),
)
}

struct MyApp {
/// Unit: fraction of full screen
plot_location: egui::Rect,
screenshot: Option<ColorImage>,
}

impl Default for MyApp {
fn default() -> Self {
Self {
plot_location: egui::Rect::EVERYTHING,
screenshot: None,
}
}
}

impl eframe::App for MyApp {
fn update(&mut self, ctx: &egui::Context, frame: &mut eframe::Frame) {
egui::CentralPanel::default().show(ctx, |ui| {
// these are just some dummy variables for the example
let height = 200.0;
let border_x = 11.0;
let border_y = 18.0;
let width = 300.0;

// get the size of the window
let window_width = ui.available_size().x;
let window_height = ui.available_size().y;

ui.heading("My egui Application");

// add some whitespace in y direction
ui.add_space(border_y);

if ui.button("Save Plot").clicked() {
frame.request_screenshot();
}

// add some whitespace in y direction
ui.add_space(border_y);

// this needs to be outside of the ui.horizontal()
let plot_location_y = window_height - ui.available_size().y;
ui.horizontal(|ui| {
// add some whitespace in x direction
ui.add_space(border_x);

// obviously this needs to be after the last ui.add_space
let plot_location_x = window_width - ui.available_size().x;

// lets set the relative plot location for plot saving purposes
self.plot_location = egui::Rect::from_two_pos(
egui::Pos2 {
x: plot_location_x / window_width,
y: plot_location_y / window_height,
},
egui::Pos2 {
x: (plot_location_x + width) / window_width,
y: (plot_location_y + height) / window_height,
},
);
let my_plot = Plot::new("My Plot")
.height(height)
.width(width)
.legend(Legend::default());

// let's create a dummy line in the plot
let graph: Vec<[f64; 2]> = vec![[0.0, 1.0], [2.0, 3.0], [3.0, 2.0]];
my_plot.show(ui, |plot_ui| {
plot_ui.line(Line::new(PlotPoints::from(graph)).name("curve"));
});
});

// add some whitespace in y direction
ui.add_space(border_y);
});

if let Some(screenshot) = self.screenshot.take() {
if let Some(mut path) = rfd::FileDialog::new().save_file() {
path.set_extension("png");

// for a full size application, we should put this in a different thread,
// so that the GUI doesn't lag during saving

// we need to use relative coordinates since the plot location comes
// in relative coordinates.
let screenshot_width = screenshot.size[0] as f32;
let screenshot_height = screenshot.size[1] as f32;
let region = egui::Rect::from_two_pos(
egui::Pos2 {
x: self.plot_location.min.x * screenshot_width,
y: self.plot_location.min.y * screenshot_height,
},
egui::Pos2 {
x: self.plot_location.max.x * screenshot_width,
y: self.plot_location.max.y * screenshot_height,
},
);

// only select the plot region from the screenshot
// since we scale it by screenshot.size, we do not need pixels_per_point,
// thus i can be set to 1.0
let plot = screenshot.region(&region, Some(1.0));

// save the plot to png
image::save_buffer(
&path,
plot.as_raw(),
plot.width() as u32,
plot.height() as u32,
image::ColorType::Rgba8,
)
.unwrap();
}
}
}

fn post_rendering(&mut self, _screen_size_px: [u32; 2], frame: &eframe::Frame) {
// this is inspired by the Egui screenshot example
if let Some(screenshot) = frame.screenshot() {
self.screenshot = Some(screenshot);
}
}
}