From 58b9abbd329b43231564b1e10ebca314b7760ae5 Mon Sep 17 00:00:00 2001 From: salam Date: Thu, 30 Apr 2026 13:57:38 +0200 Subject: [PATCH] wip statistic events quasi --- app/Actions/Statistics/LoadEvents.php | 41 ++++++++++++++--- app/Actions/Statistics/MoveingAverage.php | 13 ------ app/Filament/Pages/Statistic.php | 10 ++++- app/Filament/Widgets/MedianAverageWidget.php | 46 ++++++++++++++++++++ app/Filament/Widgets/MovingAverageChart.php | 8 ++++ app/Filament/Widgets/PunctualChart.php | 13 +++--- 6 files changed, 106 insertions(+), 25 deletions(-) create mode 100644 app/Filament/Widgets/MedianAverageWidget.php diff --git a/app/Actions/Statistics/LoadEvents.php b/app/Actions/Statistics/LoadEvents.php index 8a6ead76..41fa3de3 100644 --- a/app/Actions/Statistics/LoadEvents.php +++ b/app/Actions/Statistics/LoadEvents.php @@ -35,11 +35,11 @@ class LoadEvents */ switch ($filter['team_position']) { case 'home': - $res = StatisticFixtures::where('league_id', $league_id)->where('home_id', '=', $team_id)->first(); + $res = StatisticFixtures::where('league_id', $league_id)->where('home_id', $team_id)->first(); break; case 'away': - $res = StatisticFixtures::where('league_id', $league_id)->where('away_id', '=', $team_id)->first(); + $res = StatisticFixtures::where('league_id', $league_id)->where('away_id', $team_id)->first(); break; default: @@ -59,16 +59,40 @@ class LoadEvents $exp_away_from = $exp_away - $accuracy; $exp_away_to = $exp_away + $accuracy; + // dd($team_id, $exp_home, $exp_away); + + // "endDate" + // "startDate" + + $start = $filter['startDate'] ?? 0; + $end = $filter['endDate'] ?? 0; + /** * STATTISTIC DATA */ switch ($filter['team_position']) { case 'home': - $res = StatistikoEvent::where('league_id', $league_id)->where('home_id', '=', $team_id)->whereBetween('expgoalhome_value', [$exp_home_from, $exp_home_to])->get(); + $res = StatistikoEvent::where('league_id', $league_id) + ->where('home_id', '=', $team_id) + ->whereBetween('exp_home', [$exp_home_from, $exp_home_to]) + ->when($start, function ($query) use ($start) { + return $query->where('starttime', '>=', $start); + })->when($end, function ($query) use ($end) { + return $query->where('starttime', '<=', $end); + }) + ->get(); break; case 'away': - $res = StatistikoEvent::where('league_id', $league_id)->where('away_id', '=', $team_id)->whereBetween('expgoalsaway_value', [$exp_away_from, $exp_away_to])->get(); + $res = StatistikoEvent::where('league_id', $league_id) + ->where('away_id', '=', $team_id) + ->whereBetween('exp_away', [$exp_away_from, $exp_away_to]) + ->when($start, function ($query) use ($start) { + return $query->where('starttime', '>=', $start); + })->when($end, function ($query) use ($end) { + return $query->where('starttime', '<=', $end); + }) + ->get(); break; default: @@ -77,7 +101,12 @@ class LoadEvents $query->where('home_id', '=', $team_id)->orWhere('away_id', '=', $team_id); }) ->where(function ($query) use ($exp_home_from, $exp_home_to, $exp_away_from, $exp_away_to) { - $query->whereBetween('expgoalhome_value', [$exp_home_from, $exp_home_to])->orWhereBetween('expgoalsaway_value', [$exp_away_from, $exp_away_to]); + $query->whereBetween('exp_home', [$exp_home_from, $exp_home_to])->orWhereBetween('exp_away', [$exp_away_from, $exp_away_to]); + }) + ->when($start, function ($query) use ($start) { + return $query->where('starttime', '>=', $start); + })->when($end, function ($query) use ($end) { + return $query->where('starttime', '<=', $end); })->get(); break; } @@ -213,6 +242,8 @@ class LoadEvents [ 'label' => ucfirst($team_position), 'data' => $datasets, + 'fill' => true, + 'tension' => 0.5, 'borderColor' => '#36A2EB', ], ], diff --git a/app/Actions/Statistics/MoveingAverage.php b/app/Actions/Statistics/MoveingAverage.php index 65ce9c72..030030f8 100644 --- a/app/Actions/Statistics/MoveingAverage.php +++ b/app/Actions/Statistics/MoveingAverage.php @@ -7,24 +7,15 @@ class MoveingAverage public static function get(&$data) { - // dd($data); - $arr = $data['datasets'][0]['data']; $new = []; $last = count($arr); - $second_last = $last--; foreach ($arr as $key => $value) { switch ($key) { case 0: $new[] = $value; break; - case 1: - $new[] = ($arr[0] + $value) / 2; - break; - case $second_last: - $new[] = ($arr[$second_last] + $arr[$last]) / 2; - break; case $last: $new[] = $arr[$last]; break; @@ -35,11 +26,7 @@ class MoveingAverage break; } } - // $data['datasets'][0]['backgroundColor'] = '#00a63e'; $data['datasets'][0]['borderColor'] = '#00a63e'; - // $data['datasets'][0]['fill'] = true; - $data['datasets'][0]['tension'] = 0.5; - // $data['datasets'][0]['borderRadius'] = 30; $data['datasets'][0]['data'] = $new; } } diff --git a/app/Filament/Pages/Statistic.php b/app/Filament/Pages/Statistic.php index 1c3f243a..2bfeab5f 100644 --- a/app/Filament/Pages/Statistic.php +++ b/app/Filament/Pages/Statistic.php @@ -3,6 +3,7 @@ namespace App\Filament\Pages; use App\Actions\LoadStatisticFixturesLeaguesOptions; +use App\Filament\Widgets\MedianAverageWidget; use App\Filament\Widgets\MovingAverageChart; use App\Filament\Widgets\PunctualChart; use App\Models\Element; @@ -61,8 +62,12 @@ class Statistic extends BaseDashboard ->preload() ->searchable() ->label('Team'), - DatePicker::make('startDate'), - DatePicker::make('endDate'), + DatePicker::make('startDate') + ->native(false) + ->prefixIcon('phosphor-calendar'), + DatePicker::make('endDate') + ->native(false) + ->prefixIcon('phosphor-calendar'), ])->columns(4) ->columnSpanFull(), Section::make() @@ -145,6 +150,7 @@ class Statistic extends BaseDashboard protected function getFooterWidgets(): array { return [ + MedianAverageWidget::make(), PunctualChart::make(), MovingAverageChart::make(), ]; diff --git a/app/Filament/Widgets/MedianAverageWidget.php b/app/Filament/Widgets/MedianAverageWidget.php new file mode 100644 index 00000000..a903ec9b --- /dev/null +++ b/app/Filament/Widgets/MedianAverageWidget.php @@ -0,0 +1,46 @@ +filters); + + if (! isset($data['datasets'][0])) { + return []; + } + + $arr = $data['datasets'][0]['data']; + $total = count($arr); + $summe = array_sum($arr); + + if (! $total) { + return []; + } + + if ($total % 2) { + $m = $total / 2 + 1; + $median = $arr[$m]; + } else { + $m = $total / 2; + $m1 = floor($m); + $m2 = ceil($m); + $median = round(($arr[$m1] + $arr[$m2]) / 2, 2); + } + + return [ + Stat::make('Number of events', $total), + Stat::make('Average', round($summe / $total, 2)), + Stat::make('Median', $median), + ]; + } +} diff --git a/app/Filament/Widgets/MovingAverageChart.php b/app/Filament/Widgets/MovingAverageChart.php index dff928fc..b651c610 100644 --- a/app/Filament/Widgets/MovingAverageChart.php +++ b/app/Filament/Widgets/MovingAverageChart.php @@ -16,6 +16,14 @@ class MovingAverageChart extends ChartWidget public ?string $header; + protected ?array $options = [ + 'plugins' => [ + 'legend' => [ + 'display' => false, + ], + ], + ]; + public function getDescription(): ?string { return 'Moving Average'; diff --git a/app/Filament/Widgets/PunctualChart.php b/app/Filament/Widgets/PunctualChart.php index 183f608f..140b8249 100644 --- a/app/Filament/Widgets/PunctualChart.php +++ b/app/Filament/Widgets/PunctualChart.php @@ -12,15 +12,18 @@ class PunctualChart extends ChartWidget { use InteractsWithPageFilters; - /** - * 1. Ein Diagramm, das die Werte des ausgewählten Feldes GRUPPE 2 (HEIM/AUSWÄRTS/VERANSTALTUNG) darstellt, - * mit dem Punktwert auf der Ordinate und dem aufsteigenden Datum auf der Abszisse, - * und das den Mittelwert und den Medianwert für die ausgewählte Statistik an den ausgewählten Daten enthält. - */ protected ?string $pollingInterval = null; public ?string $header; + protected ?array $options = [ + 'plugins' => [ + 'legend' => [ + 'display' => false, + ], + ], + ]; + public function getDescription(): ?string { return 'Punctual';