Skip to main content

astroimsim_data/
plotting.rs

1use egui::Key::F;
2use egui_plot::{Heatmap, Line, Plot, PlotPoints};
3use astroimsim_geometry::grid2d::PlotPoint;
4
5pub fn run(plots:Vec<Vec<Vec<f64>>>){
6    let native_options = eframe::NativeOptions::default();
7    eframe::run_native("My egui App", native_options, Box::new(|cc| Ok(Box::new(PlotWindow::new(cc,plots)))));
8}
9
10
11pub fn display(matrix:Vec<Vec<f64>>){
12    let native_options = eframe::NativeOptions::default();
13    eframe::run_native("My egui App", native_options, Box::new(|cc| Ok(Box::new(DetectorWindow::new(cc,matrix)))));
14
15
16}
17
18
19
20#[derive(Default)]
21pub struct PlotWindow {
22    plots: Vec<Vec<Vec<f64>>>
23}
24
25pub struct DetectorWindow{
26    matrix:Vec<Vec<f64>>
27}
28
29
30
31impl PlotWindow {
32    fn new(cc: &eframe::CreationContext<'_>,plots:Vec<Vec<Vec<f64>>>) -> Self {
33        // Customize egui here with cc.egui_ctx.set_fonts and cc.egui_ctx.set_global_style.
34        // Restore app state using cc.storage (requires the "persistence" feature).
35        // Use the cc.gl (a glow::Context) to create graphics shaders and buffers that you can use
36        // for e.g. egui::PaintCallback.
37        PlotWindow{
38            plots
39        }
40    }
41}
42
43impl DetectorWindow{
44    fn new(cc: &eframe::CreationContext<'_>,matrix:Vec<Vec<f64>>) -> Self {
45        // Customize egui here with cc.egui_ctx.set_fonts and cc.egui_ctx.set_global_style.
46        // Restore app state using cc.storage (requires the "persistence" feature).
47        // Use the cc.gl (a glow::Context) to create graphics shaders and buffers that you can use
48        // for e.g. egui::PaintCallback.
49        DetectorWindow{
50            matrix
51        }
52    }
53
54}
55
56
57impl eframe::App for DetectorWindow {
58    fn ui(&mut self, ui: &mut egui::Ui, frame: &mut eframe::Frame) {
59        egui::CentralPanel::default().show_inside(ui, |ui| {
60
61
62            let cols = self.matrix[0].len();
63
64
65            let heatmap = Heatmap::new(self.matrix.iter().flatten().map(|a|*a).collect(), cols)
66                .show_labels(false);
67
68
69            Plot::new("Heatmap Demo")
70                .allow_zoom(false)
71                .allow_scroll(false)
72                .allow_drag(false)
73                .allow_axis_zoom_drag(false)
74                .allow_boxed_zoom(false)
75                .show(ui, |plot_ui| {
76                    plot_ui.heatmap(heatmap);
77                })
78                .response
79
80
81
82
83
84        });
85    }
86}
87
88
89impl eframe::App for PlotWindow {
90    fn ui(&mut self, ui: &mut egui::Ui, frame: &mut eframe::Frame) {
91        egui::CentralPanel::default().show_inside(ui, |ui| {
92            let height = ui.available_height();
93
94            for plot in &self.plots {
95                ui.horizontal(|ui|{
96                    let graph: PlotPoints = plot.iter()
97                        .map(|i| {
98                            [i[0] as f64,i[1] as f64]
99                        })
100                        .collect();
101                    let line = Line::new("sin", graph);
102                    Plot::new("my_plot")
103                        .view_aspect(2.0)
104                        .height(height as f32/(self.plots.len() as f32))
105                        .allow_scroll(false)
106                        .show(ui, |plot_ui| plot_ui.line(line));
107
108                });
109
110            }
111
112
113
114
115
116        });
117    }
118}