1+ use std:: collections:: HashMap ;
12use std:: ops:: RangeInclusive ;
23use std:: { f64:: consts:: TAU , sync:: Arc } ;
34
5+ use egui:: mutex:: Mutex ;
46use egui:: {
5- Checkbox , Color32 , ComboBox , NumExt as _, Pos2 , Response , ScrollArea , Stroke , TextWrapMode ,
7+ Checkbox , Color32 , ComboBox , Id , NumExt as _, Pos2 , Response , ScrollArea , Stroke , TextWrapMode ,
68 Vec2b , WidgetInfo , WidgetType , remap, vec2,
79} ;
810
@@ -24,6 +26,7 @@ enum Panel {
2426 Interaction ,
2527 CustomAxes ,
2628 LinkedAxes ,
29+ Userdata ,
2730}
2831
2932impl Default for Panel {
@@ -44,6 +47,7 @@ pub struct PlotDemo {
4447 interaction_demo : InteractionDemo ,
4548 custom_axes_demo : CustomAxesDemo ,
4649 linked_axes_demo : LinkedAxesDemo ,
50+ userdata_demo : UserdataDemo ,
4751 open_panel : Panel ,
4852}
4953
@@ -131,6 +135,7 @@ impl PlotDemo {
131135 ui. selectable_value ( & mut self . open_panel , Panel :: Interaction , "Interaction" ) ;
132136 ui. selectable_value ( & mut self . open_panel , Panel :: CustomAxes , "Custom Axes" ) ;
133137 ui. selectable_value ( & mut self . open_panel , Panel :: LinkedAxes , "Linked Axes" ) ;
138+ ui. selectable_value ( & mut self . open_panel , Panel :: Userdata , "Userdata" ) ;
134139 } ) ;
135140 } ) ;
136141 ui. separator ( ) ;
@@ -160,6 +165,9 @@ impl PlotDemo {
160165 Panel :: LinkedAxes => {
161166 self . linked_axes_demo . ui ( ui) ;
162167 }
168+ Panel :: Userdata => {
169+ self . userdata_demo . ui ( ui) ;
170+ }
163171 }
164172 }
165173}
@@ -636,7 +644,7 @@ impl CustomAxesDemo {
636644 }
637645 } ;
638646
639- let label_fmt = |_s : & str , val : & PlotPoint | {
647+ let label_fmt = |_s : & str , val : & PlotPoint , _ | {
640648 format ! (
641649 "Day {d}, {h}:{m:02}\n {p:.2}%" ,
642650 d = day( val. x) ,
@@ -1191,6 +1199,62 @@ impl ChartsDemo {
11911199 }
11921200}
11931201
1202+ #[ derive( PartialEq , serde:: Deserialize , serde:: Serialize , Default ) ]
1203+ struct UserdataDemo { }
1204+
1205+ struct DemoPoint {
1206+ x : f64 ,
1207+ y : f64 ,
1208+ custom_info : bool ,
1209+ }
1210+
1211+ impl UserdataDemo {
1212+ #[ allow( clippy:: unused_self, clippy:: significant_drop_tightening) ]
1213+ fn ui ( & self , ui : & mut egui:: Ui ) -> Response {
1214+ let points = ( 1 ..=1000 )
1215+ . map ( |i| DemoPoint {
1216+ x : i as f64 / 1000.0 ,
1217+ y : ( ( i as f64 ) / 1000.0 * std:: f64:: consts:: PI * 2.0 ) . sin ( ) ,
1218+ custom_info : i % 2 == 0 ,
1219+ } )
1220+ . collect :: < Vec < _ > > ( ) ;
1221+
1222+ let custom_things = Arc :: new ( Mutex :: new ( HashMap :: < Id , Vec < bool > > :: new ( ) ) ) ;
1223+
1224+ let custom_things_ = custom_things. clone ( ) ;
1225+ Plot :: new ( "Userdata Plot Demo" )
1226+ . legend ( Legend :: default ( ) )
1227+ . label_formatter ( |_, _, item| {
1228+ format ! (
1229+ "item: {:?}\n custom_thing: {:?}" ,
1230+ item,
1231+ item. and_then( |( id, index) | custom_things_
1232+ . lock( )
1233+ . get( & id)
1234+ . and_then( |vec| vec. get( index) . copied( ) ) )
1235+ )
1236+ } )
1237+ . show ( ui, |plot_ui| {
1238+ let id = Id :: new ( 1234 ) ;
1239+ let mut lock = custom_things. lock ( ) ;
1240+ let entry = lock. entry ( id) . or_default ( ) ;
1241+
1242+ for p in & points {
1243+ entry. push ( p. custom_info ) ;
1244+ }
1245+
1246+ plot_ui. line (
1247+ Line :: new (
1248+ "test" ,
1249+ points. iter ( ) . map ( |p| [ p. x , p. y ] ) . collect :: < Vec < _ > > ( ) ,
1250+ )
1251+ . id ( id) ,
1252+ ) ;
1253+ } )
1254+ . response
1255+ }
1256+ }
1257+
11941258fn is_approx_zero ( val : f64 ) -> bool {
11951259 val. abs ( ) < 1e-6
11961260}
0 commit comments