1-
2- using System ;
1+ using System ;
32using System . Linq ;
3+ using System . Threading . Tasks ;
44using CommunityToolkit . Mvvm . ComponentModel ;
55using LiveChartsCore ;
66using LiveChartsCore . Defaults ;
@@ -26,6 +26,13 @@ private static readonly (string, double)[] s_initialData =
2626 ( "Hamilton" , 1000 )
2727 } ;
2828
29+ public ViewModel ( )
30+ {
31+ _ = StartRace ( ) ;
32+ }
33+
34+ public bool IsReading { get ; set ; } = true ;
35+
2936 [ ObservableProperty ]
3037 private ISeries [ ] _series =
3138 s_initialData
@@ -49,16 +56,28 @@ private static readonly (string, double)[] s_initialData =
4956 [ ObservableProperty ]
5057 private Axis [ ] _yAxes = { new Axis { IsVisible = false } } ;
5158
52- public void RandomIncrement ( )
59+ public async Task StartRace ( )
5360 {
54- foreach ( var item in Series )
61+ await Task . Delay ( 1000 ) ;
62+
63+ // to keep this sample simple, we run the next infinite loop
64+ // in a real application you should stop the loop/task when the view is disposed
65+
66+ while ( IsReading )
5567 {
56- if ( item . Values is null ) continue ;
68+ foreach ( var item in Series )
69+ {
70+ if ( item . Values is null ) continue ;
5771
58- var i = ( ( ObservableValue [ ] ) item . Values ) [ 0 ] ;
59- i . Value += _r . Next ( 0 , 100 ) ;
60- }
72+ var i = ( ( ObservableValue [ ] ) item . Values ) [ 0 ] ;
73+ i . Value += _r . Next ( 0 , 100 ) ;
74+ }
6175
62- Series = Series . OrderByDescending ( x => ( ( ObservableValue [ ] ) x . Values ! ) [ 0 ] . Value ) . ToArray ( ) ;
76+ Series = Series
77+ . OrderByDescending ( x => ( ( ObservableValue [ ] ) x . Values ! ) [ 0 ] . Value )
78+ . ToArray ( ) ;
79+
80+ await Task . Delay ( 100 ) ;
81+ }
6382 }
6483}
0 commit comments