-
Notifications
You must be signed in to change notification settings - Fork 89
Introduce type parameter for plot points #76
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,10 @@ | ||
| use std::collections::HashMap; | ||
| use std::ops::RangeInclusive; | ||
| use std::{f64::consts::TAU, sync::Arc}; | ||
|
|
||
| use egui::mutex::Mutex; | ||
| use egui::{ | ||
| Checkbox, Color32, ComboBox, NumExt as _, Pos2, Response, ScrollArea, Stroke, TextWrapMode, | ||
| Checkbox, Color32, ComboBox, Id, NumExt as _, Pos2, Response, ScrollArea, Stroke, TextWrapMode, | ||
| Vec2b, WidgetInfo, WidgetType, remap, vec2, | ||
| }; | ||
|
|
||
|
|
@@ -24,6 +26,7 @@ enum Panel { | |
| Interaction, | ||
| CustomAxes, | ||
| LinkedAxes, | ||
| Userdata, | ||
| } | ||
|
|
||
| impl Default for Panel { | ||
|
|
@@ -44,6 +47,7 @@ pub struct PlotDemo { | |
| interaction_demo: InteractionDemo, | ||
| custom_axes_demo: CustomAxesDemo, | ||
| linked_axes_demo: LinkedAxesDemo, | ||
| userdata_demo: UserdataDemo, | ||
| open_panel: Panel, | ||
| } | ||
|
|
||
|
|
@@ -124,6 +128,7 @@ impl PlotDemo { | |
| ui.selectable_value(&mut self.open_panel, Panel::Interaction, "Interaction"); | ||
| ui.selectable_value(&mut self.open_panel, Panel::CustomAxes, "Custom Axes"); | ||
| ui.selectable_value(&mut self.open_panel, Panel::LinkedAxes, "Linked Axes"); | ||
| ui.selectable_value(&mut self.open_panel, Panel::Userdata, "Userdata"); | ||
| }); | ||
| ui.separator(); | ||
|
|
||
|
|
@@ -152,6 +157,9 @@ impl PlotDemo { | |
| Panel::LinkedAxes => { | ||
| self.linked_axes_demo.ui(ui); | ||
| } | ||
| Panel::Userdata => { | ||
| self.userdata_demo.ui(ui); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -642,7 +650,7 @@ impl CustomAxesDemo { | |
| } | ||
| }; | ||
|
|
||
| let label_fmt = |_s: &str, val: &PlotPoint| { | ||
| let label_fmt = |_s: &str, val: &PlotPoint, _| { | ||
| format!( | ||
| "Day {d}, {h}:{m:02}\n{p:.2}%", | ||
| d = day(val.x), | ||
|
|
@@ -1197,6 +1205,145 @@ impl ChartsDemo { | |
| } | ||
| } | ||
|
|
||
| #[derive(PartialEq, serde::Deserialize, serde::Serialize, Default)] | ||
| struct UserdataDemo {} | ||
|
|
||
| #[derive(Clone)] | ||
| struct DemoPoint { | ||
| x: f64, | ||
| y: f64, | ||
| custom_label: String, | ||
| importance: f32, | ||
| } | ||
|
|
||
| impl UserdataDemo { | ||
| #[expect(clippy::unused_self, clippy::significant_drop_tightening)] | ||
| fn ui(&self, ui: &mut egui::Ui) -> Response { | ||
| ui.label( | ||
| "This demo shows how to attach custom data to plot items and display it in tooltips.", | ||
| ); | ||
| ui.separator(); | ||
|
|
||
| // Create multiple datasets with custom metadata | ||
| let sine_points = (0..=500) | ||
| .map(|i| { | ||
| let x = i as f64 / 100.0; | ||
| DemoPoint { | ||
| x, | ||
| y: x.sin(), | ||
| custom_label: format!("Sine #{i}"), | ||
| importance: (i % 100) as f32 / 100.0, | ||
| } | ||
| }) | ||
| .collect::<Vec<_>>(); | ||
|
|
||
| let cosine_points = (0..=500) | ||
| .map(|i| { | ||
| let x = i as f64 / 100.0; | ||
| DemoPoint { | ||
| x, | ||
| y: x.cos(), | ||
| custom_label: format!("Cosine #{i}"), | ||
| importance: (1.0 - (i % 100) as f32 / 100.0), | ||
| } | ||
| }) | ||
| .collect::<Vec<_>>(); | ||
|
|
||
| let damped_points = (0..=500) | ||
| .map(|i| { | ||
| let x = i as f64 / 100.0; | ||
| DemoPoint { | ||
| x, | ||
| y: (-x * 0.5).exp() * (2.0 * x).sin(), | ||
| custom_label: format!("Damped #{i}"), | ||
| importance: if i % 50 == 0 { 1.0 } else { 0.3 }, | ||
| } | ||
| }) | ||
| .collect::<Vec<_>>(); | ||
|
|
||
| // Store custom data in a shared map | ||
| let custom_data = Arc::new(Mutex::new(HashMap::<Id, Vec<DemoPoint>>::new())); | ||
|
|
||
| let custom_data_ = custom_data.clone(); | ||
| Plot::new("Userdata Plot Demo") | ||
| .legend(Legend::default().position(Corner::LeftTop)) | ||
| .label_formatter(move |name, value, item| { | ||
| if let Some((id, index)) = item { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe |
||
| let lock = custom_data_.lock(); | ||
| if let Some(points) = lock.get(&id) { | ||
| if let Some(point) = points.get(index) { | ||
| return format!( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When I run this demo, this branch is never executed. Might need some debugging here. I haven't parsed it deeply yet, but I can see that the intention is that the But I'm looking at this, and thinking about how I would debug it, and thinking it will be unpleasant. And that's usually not a good sign. I'm going to keep reading and wrap my head around the full (intended) flow here. And hopefully I can come up with a more constructive suggestion for an alternative design here. |
||
| "{}\nPosition: ({:.3}, {:.3})\nLabel: {}\nImportance: {:.1}%", | ||
| name, | ||
| value.x, | ||
| value.y, | ||
| point.custom_label, | ||
| point.importance * 100.0 | ||
| ); | ||
| } | ||
| } | ||
| } | ||
| format!("{}\n({:.3}, {:.3})", name, value.x, value.y) | ||
| }) | ||
| .show(ui, |plot_ui| { | ||
| let mut lock = custom_data.lock(); | ||
|
|
||
| // Sine wave with custom data | ||
| let sine_id = Id::new("sine_wave"); | ||
| lock.insert(sine_id, sine_points.clone()); | ||
| plot_ui.line( | ||
| Line::new( | ||
| "sin(x)", | ||
| sine_points.iter().map(|p| [p.x, p.y]).collect::<Vec<_>>(), | ||
| ) | ||
| .id(sine_id) | ||
| .color(Color32::from_rgb(200, 100, 100)), | ||
| ); | ||
|
|
||
| // Cosine wave with custom data | ||
| let cosine_id = Id::new("cosine_wave"); | ||
| lock.insert(cosine_id, cosine_points.clone()); | ||
| plot_ui.line( | ||
| Line::new( | ||
| "cos(x)", | ||
| cosine_points.iter().map(|p| [p.x, p.y]).collect::<Vec<_>>(), | ||
| ) | ||
| .id(cosine_id) | ||
| .color(Color32::from_rgb(100, 200, 100)), | ||
| ); | ||
|
|
||
| // Damped sine wave with custom data | ||
| let damped_id = Id::new("damped_wave"); | ||
| lock.insert(damped_id, damped_points.clone()); | ||
| plot_ui.line( | ||
| Line::new( | ||
| "e^(-0.5x) · sin(2x)", | ||
| damped_points.iter().map(|p| [p.x, p.y]).collect::<Vec<_>>(), | ||
| ) | ||
| .id(damped_id) | ||
| .color(Color32::from_rgb(100, 100, 200)), | ||
| ); | ||
|
|
||
| // Add some points with high importance as markers | ||
| let important_points: Vec<_> = damped_points | ||
| .iter() | ||
| .filter(|p| p.importance > 0.9) | ||
| .map(|p| [p.x, p.y]) | ||
| .collect(); | ||
|
|
||
| if !important_points.is_empty() { | ||
| plot_ui.points( | ||
| Points::new("Important Points", important_points) | ||
| .color(Color32::from_rgb(255, 150, 0)) | ||
| .radius(4.0) | ||
| .shape(MarkerShape::Diamond), | ||
| ); | ||
| } | ||
| }) | ||
| .response | ||
| } | ||
| } | ||
|
|
||
| fn is_approx_zero(val: f64) -> bool { | ||
| val.abs() < 1e-6 | ||
| } | ||
|
|
||

Uh oh!
There was an error while loading. Please reload this page.