@@ -12,6 +12,8 @@ class VoteHistoryChart extends ChartWidget
1212
1313 public ?Item $ item = null ;
1414
15+ public ?string $ filter = 'all ' ;
16+
1517 public function getHeading (): ?string
1618 {
1719 return trans ('items.vote-history ' );
@@ -22,16 +24,36 @@ public function getDescription(): ?string
2224 return trans ('items.vote-history-description ' );
2325 }
2426
27+ protected function getFilters (): ?array
28+ {
29+ return [
30+ 'all ' => trans ('items.filter-all-time ' ),
31+ '7 ' => trans ('items.filter-last-7-days ' ),
32+ '30 ' => trans ('items.filter-last-30-days ' ),
33+ '90 ' => trans ('items.filter-last-90-days ' ),
34+ '365 ' => trans ('items.filter-last-year ' ),
35+ '730 ' => trans ('items.filter-last-2-years ' ),
36+ '1095 ' => trans ('items.filter-last-3-years ' ),
37+ ];
38+ }
39+
2540 protected function getData (): array
2641 {
27- if (!$ this ->item ) {
42+ if (! $ this ->item ) {
2843 return [
2944 'datasets ' => [],
3045 'labels ' => [],
3146 ];
3247 }
3348
34- $ votes = $ this ->item ->votes ()
49+ $ query = $ this ->item ->votes ();
50+
51+ if ($ this ->filter && $ this ->filter !== 'all ' ) {
52+ $ days = (int ) $ this ->filter ;
53+ $ query ->where ('created_at ' , '>= ' , Carbon::now ()->subDays ($ days ));
54+ }
55+
56+ $ votes = $ query
3557 ->selectRaw ('DATE(created_at) as date, COUNT(*) as count ' )
3658 ->groupByRaw ('DATE(created_at) ' )
3759 ->orderBy ('date ' )
@@ -54,7 +76,7 @@ protected function getData(): array
5476 $ currentDate = $ startDate ->copy ();
5577 while ($ currentDate ->lte ($ endDate )) {
5678 $ dateKey = $ currentDate ->format ('Y-m-d ' );
57- $ labels [] = $ currentDate ->format ('M d ' );
79+ $ labels [] = $ currentDate ->format ('M d, Y ' );
5880 $ data [] = $ votesMap [$ dateKey ] ?? 0 ;
5981 $ currentDate ->addDay ();
6082 }
0 commit comments