@@ -13,12 +13,16 @@ public partial class Statistics : ComponentBase, IAsyncDisposable
1313 private Activity [ ] _activities = [ ] ;
1414 private IJSObjectReference ? _module ;
1515
16+ private int NumberOfMonthsToReview { get ; set ; } = 3 ;
17+ private ChallengeMeasurement Measurement { get ; set ; } = ChallengeMeasurement . Distance ;
18+ private Dictionary < string , float [ ] > Distances { get ; set ; } = [ ] ;
19+ private Dictionary < string , float [ ] > Elevations { get ; set ; } = [ ] ;
20+ private Dictionary < string , float [ ] > Durations { get ; set ; } = [ ] ;
21+
1622 protected override async Task OnInitializedAsync ( )
1723 {
1824 _athlete = await AuthenticationService . GetCurrentAthleteAsync ( ) ;
19-
20- ActivityFilter filter = new ( _athlete . Id , false , DateTimeOffset . Now . AddMonths ( - 2 ) . ToFirstOfTheMonth ( ) ) ;
21- _activities = await ActivityService . GetActivitiesAsync ( filter ) ;
25+ UpdateMeasurementData ( ) ;
2226 }
2327
2428 private Dictionary < string , float [ ] > GetAggregatedMeasurementData ( Func < Activity , float > selector , int monthCount = 3 )
@@ -33,70 +37,98 @@ private Dictionary<string, float[]> GetAggregatedMeasurementData(Func<Activity,
3337 . Select ( m =>
3438 {
3539 int month = now . AddMonths ( m ) . Month ;
40+ int year = now . AddMonths ( m ) . Year ;
3641 return g
37- . Where ( a => a . Details . StartDate . Month == month )
42+ . Where ( a => a . Details . StartDate . Month == month && a . Details . StartDate . Year == year )
3843 . Sum ( selector ) ;
3944 } )
4045 . ToArray ( )
4146 )
4247 . Where ( d => d . Value . Any ( v => v > 0 ) )
4348 . ToDictionary ( ) ;
4449
45- // if (aggregatedData.Count != 0)
46- // {
47- // aggregatedData["Total"] =
48- // [
49- // .. aggregatedData.Values.Aggregate((sum, next) => [.. sum.Zip(next, (a, b) => a + b)]),
50- // ];
51- // }
52-
5350 return aggregatedData ;
5451 }
5552
53+ private static string [ ] CreateMonthArray ( int monthCount )
54+ {
55+ return
56+ [
57+ .. Enumerable . Range ( 0 , monthCount )
58+ . Select ( i =>
59+ {
60+ DateTime month = DateTime . Now . AddMonths ( - i ) ;
61+ return month . ToString ( month . Month == 1 ? "MMMM-yy" : "MMMM" ) ;
62+ } )
63+ . Reverse ( )
64+ ] ;
65+ }
66+
5667 protected override async Task OnAfterRenderAsync ( bool firstRender )
5768 {
5869 if ( ! firstRender )
5970 return ;
6071
72+ await DrawChart ( ) ;
73+ }
74+
75+ private async Task UpdateMeasurementData ( )
76+ {
77+ ActivityFilter filter = new (
78+ _athlete . Id ,
79+ false ,
80+ DateTimeOffset . Now . AddMonths ( - NumberOfMonthsToReview + 1 ) . ToFirstOfTheMonth ( ) ) ;
81+ _activities = await ActivityService . GetActivitiesAsync ( filter ) ;
82+
83+ Distances = GetAggregatedMeasurementData (
84+ a => a . Details . DistanceInMeters / 1000 ,
85+ NumberOfMonthsToReview ) ;
86+ Elevations = GetAggregatedMeasurementData ( a => a . Details . TotalElevationGain , NumberOfMonthsToReview ) ;
87+ Durations = GetAggregatedMeasurementData (
88+ a => ( float ) a . Details . MovingTimeInSeconds / 3600 ,
89+ NumberOfMonthsToReview ) ;
90+ }
91+
92+ private async Task DrawChart ( )
93+ {
6194 _module = await Js . InvokeAsync < IJSObjectReference > ( "import" , "./Pages/Statistics.razor.js" ) ;
6295
63- string [ ] lastThreeMonths = new int [ ]
64- {
65- DateTimeOffset . Now . AddMonths ( - 2 ) . Month ,
66- DateTimeOffset . Now . AddMonths ( - 1 ) . Month ,
67- DateTimeOffset . Now . Month ,
68- }
69- . Select ( CultureInfo . CurrentCulture . DateTimeFormat . GetMonthName )
70- . ToArray ( ) ;
71-
72- Dictionary < string , float [ ] > distanceLastThreeMonths =
73- GetAggregatedMeasurementData ( a => ( float ) a . Details . DistanceInMeters / 1000 ) ;
74- Dictionary < string , float [ ] > elevationLastThreeMonths =
75- GetAggregatedMeasurementData ( a => a . Details . TotalElevationGain ) ;
76- Dictionary < string , float [ ] > timeLastThreeMonths =
77- GetAggregatedMeasurementData ( a => ( float ) a . Details . MovingTimeInSeconds / 3600 ) ;
78-
79- await _module . InvokeVoidAsync (
80- "drawChart" ,
81- distanceLastThreeMonths ,
82- lastThreeMonths ,
83- "totalDistanceChart" ,
84- "Total Distance [km]" ,
85- "km" ) ;
86- await _module . InvokeVoidAsync (
87- "drawChart" ,
88- elevationLastThreeMonths ,
89- lastThreeMonths ,
90- "totalElevationChart" ,
91- "Total Elevation [m]" ,
92- "m" ) ;
93- await _module . InvokeVoidAsync (
94- "drawChart" ,
95- timeLastThreeMonths ,
96- lastThreeMonths ,
97- "totalTimeChart" ,
98- "Total Time [h]" ,
99- "h" ) ;
96+ string [ ] months = CreateMonthArray ( NumberOfMonthsToReview ) ;
97+
98+ UpdateMeasurementData ( ) ;
99+
100+ switch ( Measurement )
101+ {
102+ case ChallengeMeasurement . Distance :
103+ await _module . InvokeVoidAsync (
104+ "drawChart" ,
105+ Distances ,
106+ months ,
107+ "TheChart" ,
108+ "Total Distance [km]" ,
109+ "km" ) ;
110+ break ;
111+ case ChallengeMeasurement . Elevation :
112+ await _module . InvokeVoidAsync (
113+ "drawChart" ,
114+ Elevations ,
115+ months ,
116+ "TheChart" ,
117+ "Total Elevation [m]" ,
118+ "m" ) ;
119+ break ;
120+ case ChallengeMeasurement . Time :
121+ await _module . InvokeVoidAsync (
122+ "drawChart" ,
123+ Durations ,
124+ months ,
125+ "TheChart" ,
126+ "Total Time [h]" ,
127+ "h" ) ;
128+ break ;
129+ default :
130+ throw new Exception ( "Unknown challenge measurement" ) ;
131+ }
100132 }
101133
102134 public async ValueTask DisposeAsync ( )
0 commit comments