@@ -48,22 +48,45 @@ protected function getData(): array
4848
4949 $ query = $ this ->item ->votes ();
5050
51- if ($ this ->filter && $ this ->filter !== 'all ' ) {
52- $ days = (int ) $ this ->filter ;
51+ $ days = $ this ->filter && $ this ->filter !== 'all ' ? (int ) $ this ->filter : null ;
52+
53+ if ($ days ) {
5354 $ query ->where ('created_at ' , '>= ' , Carbon::now ()->subDays ($ days ));
5455 }
5556
57+ // Determine grouping based on time range
58+ $ groupBy = $ this ->getGroupingStrategy ($ days );
59+
60+ if ($ groupBy === 'month ' ) {
61+ return $ this ->getMonthlyData ($ query );
62+ } elseif ($ groupBy === 'week ' ) {
63+ return $ this ->getWeeklyData ($ query );
64+ }
65+
66+ return $ this ->getDailyData ($ query );
67+ }
68+
69+ protected function getGroupingStrategy (?int $ days ): string
70+ {
71+ if ($ days === null || $ days > 365 ) {
72+ return 'month ' ;
73+ } elseif ($ days > 90 ) {
74+ return 'week ' ;
75+ }
76+
77+ return 'day ' ;
78+ }
79+
80+ protected function getDailyData ($ query ): array
81+ {
5682 $ votes = $ query
5783 ->selectRaw ('DATE(created_at) as date, COUNT(*) as count ' )
5884 ->groupByRaw ('DATE(created_at) ' )
5985 ->orderBy ('date ' )
6086 ->get ();
6187
6288 if ($ votes ->isEmpty ()) {
63- return [
64- 'datasets ' => [],
65- 'labels ' => [],
66- ];
89+ return ['datasets ' => [], 'labels ' => []];
6790 }
6891
6992 $ startDate = Carbon::parse ($ votes ->first ()->date );
@@ -76,11 +99,58 @@ protected function getData(): array
7699 $ currentDate = $ startDate ->copy ();
77100 while ($ currentDate ->lte ($ endDate )) {
78101 $ dateKey = $ currentDate ->format ('Y-m-d ' );
79- $ labels [] = $ currentDate ->format ('M d, Y ' );
102+ $ labels [] = $ currentDate ->format ('M d ' );
80103 $ data [] = $ votesMap [$ dateKey ] ?? 0 ;
81104 $ currentDate ->addDay ();
82105 }
83106
107+ return $ this ->formatChartData ($ labels , $ data );
108+ }
109+
110+ protected function getWeeklyData ($ query ): array
111+ {
112+ $ votes = $ query ->orderBy ('created_at ' )->get ();
113+
114+ if ($ votes ->isEmpty ()) {
115+ return ['datasets ' => [], 'labels ' => []];
116+ }
117+
118+ $ grouped = $ votes ->groupBy (fn ($ vote ) => Carbon::parse ($ vote ->created_at )->startOfWeek ()->format ('Y-m-d ' ));
119+
120+ $ labels = [];
121+ $ data = [];
122+
123+ foreach ($ grouped as $ weekStart => $ weekVotes ) {
124+ $ labels [] = Carbon::parse ($ weekStart )->format ('M d ' );
125+ $ data [] = $ weekVotes ->count ();
126+ }
127+
128+ return $ this ->formatChartData ($ labels , $ data );
129+ }
130+
131+ protected function getMonthlyData ($ query ): array
132+ {
133+ $ votes = $ query ->orderBy ('created_at ' )->get ();
134+
135+ if ($ votes ->isEmpty ()) {
136+ return ['datasets ' => [], 'labels ' => []];
137+ }
138+
139+ $ grouped = $ votes ->groupBy (fn ($ vote ) => Carbon::parse ($ vote ->created_at )->format ('Y-m ' ));
140+
141+ $ labels = [];
142+ $ data = [];
143+
144+ foreach ($ grouped as $ month => $ monthVotes ) {
145+ $ labels [] = Carbon::createFromFormat ('Y-m ' , $ month )->format ('M Y ' );
146+ $ data [] = $ monthVotes ->count ();
147+ }
148+
149+ return $ this ->formatChartData ($ labels , $ data );
150+ }
151+
152+ protected function formatChartData (array $ labels , array $ data ): array
153+ {
84154 return [
85155 'datasets ' => [
86156 [
0 commit comments