MCPcopy Create free account
hub / github.com/emilk/egui_plot / show_plot

Method show_plot

examples/save_plot/src/app.rs:14–67  ·  view source on GitHub ↗
(&mut self, ui: &mut egui::Ui)

Source from the content-addressed store, hash-verified

12
13impl SavePlotExample {
14 pub fn show_plot(&mut self, ui: &mut egui::Ui) -> Response {
15 let my_plot = Plot::new("My Plot").legend(Legend::default());
16
17 // let's create a dummy line in the plot
18 let graph: Vec<[f64; 2]> = vec![[0.0, 1.0], [2.0, 3.0], [3.0, 2.0]];
19 let inner = my_plot.show(ui, |plot_ui| {
20 plot_ui.line(Line::new("curve", PlotPoints::from(graph)));
21 });
22 // Remember the position of the plot
23 self.plot_rect = Some(inner.response.rect);
24
25 #[cfg(not(target_arch = "wasm32"))]
26 {
27 // Check for returned screenshot:
28 let ctx = ui.ctx();
29 let screenshot = ctx.input(|i| {
30 for event in &i.raw.events {
31 if let egui::Event::Screenshot { image, .. } = event {
32 return Some(std::sync::Arc::clone(image));
33 }
34 }
35 None
36 });
37 if let (Some(screenshot), Some(plot_location)) = (screenshot, self.plot_rect)
38 && let Some(mut path) = rfd::FileDialog::new().save_file()
39 {
40 path.set_extension("png");
41
42 // for a full size application, we should put this in a different thread,
43 // so that the GUI doesn't lag during saving
44
45 let pixels_per_point = ctx.pixels_per_point();
46 let plot = screenshot.region(&plot_location, Some(pixels_per_point));
47 // save the plot to png
48 let result = image::save_buffer(
49 &path,
50 plot.as_raw(),
51 plot.width() as u32,
52 plot.height() as u32,
53 image::ColorType::Rgba8,
54 );
55 match result {
56 Ok(()) => eprintln!("Image saved to {}", path.display()),
57 Err(err) => eprintln!("Failed to save image to {}: {err}", path.display()),
58 }
59 }
60 }
61 #[cfg(target_arch = "wasm32")]
62 {
63 eprintln!("File saving is not supported on WASM targets");
64 }
65
66 inner.response
67 }
68
69 #[expect(clippy::unused_self, reason = "required by the example template")]
70 pub fn show_controls(&self, ui: &mut egui::Ui) -> Response {

Callers 1

show_uiMethod · 0.45

Calls 6

legendMethod · 0.80
showMethod · 0.80
lineMethod · 0.80
ctxMethod · 0.80
widthMethod · 0.45
heightMethod · 0.45

Tested by

no test coverage detected