|
| 1 | +use eframe::egui; |
| 2 | +use eframe::egui::Response; |
| 3 | +use egui_plot::Legend; |
| 4 | +use egui_plot::Line; |
| 5 | +use egui_plot::Plot; |
| 6 | + |
| 7 | +use egui::mutex::Mutex; |
| 8 | +use egui::{Color32, Id}; |
| 9 | +use egui_plot::{Corner, MarkerShape, Points}; |
| 10 | +use std::collections::HashMap; |
| 11 | +use std::sync::Arc; |
| 12 | + |
| 13 | +pub struct UserdataPointsExample { |
| 14 | + sine_points: Vec<DemoPoint>, |
| 15 | + cosine_points: Vec<DemoPoint>, |
| 16 | + damped_points: Vec<DemoPoint>, |
| 17 | +} |
| 18 | + |
| 19 | +#[derive(Clone)] |
| 20 | +struct DemoPoint { |
| 21 | + x: f64, |
| 22 | + y: f64, |
| 23 | + custom_label: String, |
| 24 | + importance: f32, |
| 25 | +} |
| 26 | + |
| 27 | +impl Default for UserdataPointsExample { |
| 28 | + fn default() -> Self { |
| 29 | + // Create multiple datasets with custom metadata |
| 30 | + let sine_points = (0..=500) |
| 31 | + .map(|i| { |
| 32 | + let x = i as f64 / 100.0; |
| 33 | + DemoPoint { |
| 34 | + x, |
| 35 | + y: x.sin(), |
| 36 | + custom_label: format!("Sine #{i}"), |
| 37 | + importance: (i % 100) as f32 / 100.0, |
| 38 | + } |
| 39 | + }) |
| 40 | + .collect::<Vec<_>>(); |
| 41 | + let cosine_points = (0..=500) |
| 42 | + .map(|i| { |
| 43 | + let x = i as f64 / 100.0; |
| 44 | + DemoPoint { |
| 45 | + x, |
| 46 | + y: x.cos(), |
| 47 | + custom_label: format!("Cosine #{i}"), |
| 48 | + importance: (1.0 - (i % 100) as f32 / 100.0), |
| 49 | + } |
| 50 | + }) |
| 51 | + .collect::<Vec<_>>(); |
| 52 | + |
| 53 | + let damped_points = (0..=500) |
| 54 | + .map(|i| { |
| 55 | + let x = i as f64 / 100.0; |
| 56 | + DemoPoint { |
| 57 | + x, |
| 58 | + y: (-x * 0.5).exp() * (2.0 * x).sin(), |
| 59 | + custom_label: format!("Damped #{i}"), |
| 60 | + importance: if i % 50 == 0 { 1.0 } else { 0.3 }, |
| 61 | + } |
| 62 | + }) |
| 63 | + .collect::<Vec<_>>(); |
| 64 | + Self { |
| 65 | + sine_points, |
| 66 | + cosine_points, |
| 67 | + damped_points, |
| 68 | + } |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +impl UserdataPointsExample { |
| 73 | + pub fn show_plot(&self, ui: &mut egui::Ui) -> Response { |
| 74 | + let custom_data = Arc::new(Mutex::new(HashMap::<Id, Vec<DemoPoint>>::new())); |
| 75 | + |
| 76 | + let custom_data_ = custom_data.clone(); |
| 77 | + Plot::new("Userdata Plot Demo") |
| 78 | + .legend(Legend::default().position(Corner::LeftTop)) |
| 79 | + .label_formatter(move |name, value, item| { |
| 80 | + if let Some((id, index)) = item { |
| 81 | + let lock = custom_data_.lock(); |
| 82 | + if let Some(points) = lock.get(&id) { |
| 83 | + if let Some(point) = points.get(index) { |
| 84 | + return format!( |
| 85 | + "{}\nPosition: ({:.3}, {:.3})\nLabel: {}\nImportance: {:.1}%", |
| 86 | + name, |
| 87 | + value.x, |
| 88 | + value.y, |
| 89 | + point.custom_label, |
| 90 | + point.importance * 100.0 |
| 91 | + ); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + format!("{}\n({:.3}, {:.3})", name, value.x, value.y) |
| 96 | + }) |
| 97 | + .show(ui, |plot_ui| { |
| 98 | + let mut lock = custom_data.lock(); |
| 99 | + |
| 100 | + // Sine wave with custom data |
| 101 | + let sine_id = Id::new("sine_wave"); |
| 102 | + lock.insert(sine_id, self.sine_points.clone()); |
| 103 | + plot_ui.line( |
| 104 | + Line::new( |
| 105 | + "sin(x)", |
| 106 | + self.sine_points.iter().map(|p| [p.x, p.y]).collect::<Vec<_>>(), |
| 107 | + ) |
| 108 | + .id(sine_id) |
| 109 | + .color(Color32::from_rgb(200, 100, 100)), |
| 110 | + ); |
| 111 | + |
| 112 | + // Cosine wave with custom data |
| 113 | + let cosine_id = Id::new("cosine_wave"); |
| 114 | + lock.insert(cosine_id, self.cosine_points.clone()); |
| 115 | + plot_ui.line( |
| 116 | + Line::new( |
| 117 | + "cos(x)", |
| 118 | + self.cosine_points.iter().map(|p| [p.x, p.y]).collect::<Vec<_>>(), |
| 119 | + ) |
| 120 | + .id(cosine_id) |
| 121 | + .color(Color32::from_rgb(100, 200, 100)), |
| 122 | + ); |
| 123 | + |
| 124 | + // Damped sine wave with custom data |
| 125 | + let damped_id = Id::new("damped_wave"); |
| 126 | + lock.insert(damped_id, self.damped_points.clone()); |
| 127 | + plot_ui.line( |
| 128 | + Line::new( |
| 129 | + "e^(-0.5x) · sin(2x)", |
| 130 | + self.damped_points.iter().map(|p| [p.x, p.y]).collect::<Vec<_>>(), |
| 131 | + ) |
| 132 | + .id(damped_id) |
| 133 | + .color(Color32::from_rgb(100, 100, 200)), |
| 134 | + ); |
| 135 | + |
| 136 | + // Add some points with high importance as markers |
| 137 | + let important_points: Vec<_> = self |
| 138 | + .damped_points |
| 139 | + .iter() |
| 140 | + .filter(|p| p.importance > 0.9) |
| 141 | + .map(|p| [p.x, p.y]) |
| 142 | + .collect(); |
| 143 | + |
| 144 | + if !important_points.is_empty() { |
| 145 | + plot_ui.points( |
| 146 | + Points::new("Important Points", important_points) |
| 147 | + .color(Color32::from_rgb(255, 150, 0)) |
| 148 | + .radius(4.0) |
| 149 | + .shape(MarkerShape::Diamond), |
| 150 | + ); |
| 151 | + } |
| 152 | + }) |
| 153 | + .response |
| 154 | + } |
| 155 | + |
| 156 | + #[expect(clippy::unused_self, reason = "required by the example template")] |
| 157 | + pub fn show_controls(&self, ui: &mut egui::Ui) -> Response { |
| 158 | + ui.scope(|_ui| {}).response |
| 159 | + } |
| 160 | +} |
0 commit comments