-
Notifications
You must be signed in to change notification settings - Fork 54
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 |
---|---|---|
|
@@ -2,7 +2,7 @@ use std::f64::consts::TAU; | |
use std::ops::RangeInclusive; | ||
|
||
use egui::{ | ||
remap, vec2, Color32, ComboBox, NumExt, Pos2, Response, ScrollArea, Stroke, TextWrapMode, | ||
remap, vec2, Color32, ComboBox, Id, NumExt, Pos2, Response, ScrollArea, Stroke, TextWrapMode, | ||
Vec2b, WidgetInfo, WidgetType, | ||
}; | ||
|
||
|
@@ -24,6 +24,7 @@ enum Panel { | |
Interaction, | ||
CustomAxes, | ||
LinkedAxes, | ||
Userdata, | ||
} | ||
|
||
impl Default for Panel { | ||
|
@@ -44,6 +45,7 @@ pub struct PlotDemo { | |
interaction_demo: InteractionDemo, | ||
custom_axes_demo: CustomAxesDemo, | ||
linked_axes_demo: LinkedAxesDemo, | ||
userdata_demo: UserdataDemo, | ||
open_panel: Panel, | ||
} | ||
|
||
|
@@ -88,6 +90,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(); | ||
|
@@ -117,6 +120,9 @@ impl PlotDemo { | |
Panel::LinkedAxes => { | ||
self.linked_axes_demo.ui(ui); | ||
} | ||
Panel::Userdata => { | ||
self.userdata_demo.ui(ui); | ||
} | ||
} | ||
} | ||
} | ||
|
@@ -565,7 +571,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), | ||
|
@@ -1101,6 +1107,61 @@ impl ChartsDemo { | |
} | ||
} | ||
|
||
#[derive(PartialEq, serde::Deserialize, serde::Serialize, Default)] | ||
struct UserdataDemo {} | ||
|
||
struct DemoPoint { | ||
x: f64, | ||
y: f64, | ||
custom_thing: bool, | ||
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. This makes it sounds like some demo points has a custom thing, other don't. I think this feature deserves a more realistic example |
||
} | ||
|
||
impl UserdataDemo { | ||
#[allow(clippy::unused_self, clippy::significant_drop_tightening)] | ||
fn ui(&self, ui: &mut egui::Ui) -> Response { | ||
let points = (1..=1000) | ||
.map(|i| DemoPoint { | ||
x: i as f64 / 1000.0, | ||
y: ((i as f64) / 1000.0 * std::f64::consts::PI * 2.0).sin(), | ||
custom_thing: i % 2 == 0, | ||
}) | ||
.collect::<Vec<_>>(); | ||
|
||
let custom_things = | ||
std::sync::Arc::new(egui::mutex::Mutex::new(std::collections::HashMap::< | ||
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. Add some |
||
Id, | ||
Comment on lines
+1131
to
+1132
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. Nit: this could use |
||
Vec<bool>, | ||
>::new())); | ||
|
||
let custom_things_ = custom_things.clone(); | ||
Plot::new("Userdata Plot Demo") | ||
.legend(Legend::default()) | ||
.label_formatter(|_, _, item| { | ||
format!( | ||
"item: {:?}\ncustom_thing: {:?}", | ||
item, | ||
item.and_then(|(id, index)| custom_things_ | ||
.lock() | ||
.get(&id) | ||
.and_then(|vec| vec.get(index).copied())) | ||
) | ||
}) | ||
.show(ui, |plot_ui| { | ||
let id = Id::new(1234); | ||
let mut lock = custom_things.lock(); | ||
let entry = lock.entry(id).or_default(); | ||
|
||
for p in &points { | ||
entry.push(p.custom_thing); | ||
} | ||
|
||
plot_ui | ||
.line(Line::new(points.iter().map(|p| [p.x, p.y]).collect::<Vec<_>>()).id(id)); | ||
}) | ||
.response | ||
} | ||
} | ||
|
||
fn is_approx_zero(val: f64) -> bool { | ||
val.abs() < 1e-6 | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,7 +41,7 @@ use axis::AxisWidget; | |
use items::{horizontal_line, rulers_color, vertical_line}; | ||
use legend::LegendWidget; | ||
|
||
type LabelFormatterFn<'a> = dyn Fn(&str, &PlotPoint) -> String + 'a; | ||
type LabelFormatterFn<'a> = dyn Fn(&str, &PlotPoint, Option<(Id, usize)>) -> String + 'a; | ||
pub type LabelFormatter<'a> = Option<Box<LabelFormatterFn<'a>>>; | ||
|
||
type GridSpacerFn<'a> = dyn Fn(GridInput) -> Vec<GridMark> + 'a; | ||
|
@@ -397,7 +397,7 @@ impl<'a> Plot<'a> { | |
/// }).collect(); | ||
/// let line = Line::new(sin); | ||
/// Plot::new("my_plot").view_aspect(2.0) | ||
/// .label_formatter(|name, value| { | ||
/// .label_formatter(|name, value, _| { | ||
/// if !name.is_empty() { | ||
/// format!("{}: {:.*}%", name, 1, value.y) | ||
/// } else { | ||
|
@@ -409,7 +409,7 @@ impl<'a> Plot<'a> { | |
/// ``` | ||
pub fn label_formatter( | ||
mut self, | ||
label_formatter: impl Fn(&str, &PlotPoint) -> String + 'a, | ||
label_formatter: impl Fn(&str, &PlotPoint, Option<(Id, usize)>) -> String + 'a, | ||
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. The docstring should explain what the third parameter is here |
||
) -> Self { | ||
self.label_formatter = Some(Box::new(label_formatter)); | ||
self | ||
|
@@ -1716,6 +1716,7 @@ impl<'a> PreparedPlot<'a> { | |
items::rulers_at_value( | ||
pointer, | ||
value, | ||
None, | ||
"", | ||
&plot, | ||
shapes, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's soon time to break up this file into several smaller files (not in this PR though!)