diff --git a/app/Filament/Pages/Statistic.php b/app/Filament/Pages/Statistic.php index 2bd633d9..766db7e5 100644 --- a/app/Filament/Pages/Statistic.php +++ b/app/Filament/Pages/Statistic.php @@ -6,8 +6,10 @@ use App\Actions\LoadStatisticFixturesLeaguesOptions; use App\Actions\LoadStatisticFixturesMatchOptions; use App\Filament\Widgets\ChartEvent; use App\Filament\Widgets\ChartEventMoAv; +use App\Filament\Widgets\ChartEventPerf; use App\Filament\Widgets\ChartPlayer; use App\Filament\Widgets\ChartPlayerMoAv; +use App\Filament\Widgets\ChartPlayerPerf; use App\Filament\Widgets\StatsMedianAverage; use App\Models\Element; use App\Models\StatisticFixtures; @@ -229,7 +231,7 @@ class Statistic extends BaseDashboard return [ 'default' => 1, 'md' => 2, - // 'lg' => 6, + 'lg' => 3, ]; } @@ -253,9 +255,11 @@ class Statistic extends BaseDashboard // Events ChartEvent::make(), ChartEventMoAv::make(), + ChartEventPerf::make(), // Players ChartPlayer::make(), ChartPlayerMoAv::make(), + ChartPlayerPerf::make(), ]; } diff --git a/app/Filament/Widgets/ChartEvent.php b/app/Filament/Widgets/ChartEvent.php index 20f9be5e..8ea1dd86 100644 --- a/app/Filament/Widgets/ChartEvent.php +++ b/app/Filament/Widgets/ChartEvent.php @@ -12,6 +12,8 @@ class ChartEvent extends ChartWidget { use InteractsWithPageFilters; + protected ?string $heading = 'Events'; + protected ?string $pollingInterval = null; protected bool $isCollapsible = true; @@ -20,8 +22,6 @@ class ChartEvent extends ChartWidget protected function getData(): array { - $this->heading = 'Events'; - $data = LoadEvents::load($this->filters); $this->description = ucfirst(HelperStatisticType::get($this->filters)); diff --git a/app/Filament/Widgets/ChartEventMoAv.php b/app/Filament/Widgets/ChartEventMoAv.php index 7d28f844..8d529237 100644 --- a/app/Filament/Widgets/ChartEventMoAv.php +++ b/app/Filament/Widgets/ChartEventMoAv.php @@ -13,14 +13,14 @@ class ChartEventMoAv extends ChartWidget { use InteractsWithPageFilters; + protected ?string $heading = 'Events'; + protected ?string $pollingInterval = null; protected bool $isCollapsible = true; protected function getData(): array { - $this->heading = 'Events'; - $data = LoadEvents::load($this->filters); $this->description = ucfirst(HelperStatisticType::get($this->filters)).' Moving Average'; diff --git a/app/Filament/Widgets/ChartEventPerf.php b/app/Filament/Widgets/ChartEventPerf.php new file mode 100644 index 00000000..39e6569c --- /dev/null +++ b/app/Filament/Widgets/ChartEventPerf.php @@ -0,0 +1,67 @@ +filters); + } + + protected function getType(): string + { + return 'line'; + } + + protected function getOptions(): RawJs + { + return RawJs::make(<<<'JS' + { + interaction: { + intersect: false, + }, + // plugins: { + // tooltip: { + // callbacks: { + // label: (context) => { + // return [' '+context.parsed.y, ' '+context.dataset.playing_date[context.dataIndex], ' '+context.dataset.events[context.dataIndex], ' '+context.dataset.results_per_event[context.dataIndex]]; + // }, + // } + // } + // }, + scales: { + x: { + title: { + display: true, + text: 'Days since today' + } + }, + y: { + beginAtZero:true, + ticks: { + callback: function(value, index, ticks) { + return value % 1 === 0 ? value : ''; + } + } + }, + }, + } + JS); + } +} diff --git a/app/Filament/Widgets/ChartPlayer.php b/app/Filament/Widgets/ChartPlayer.php index e7885ae4..c5cade9a 100644 --- a/app/Filament/Widgets/ChartPlayer.php +++ b/app/Filament/Widgets/ChartPlayer.php @@ -15,6 +15,8 @@ class ChartPlayer extends ChartWidget protected bool $isCollapsible = true; + protected ?string $heading = 'Player'; + protected ?array $options = [ 'plugins' => [ 'legend' => [ @@ -37,8 +39,6 @@ class ChartPlayer extends ChartWidget return []; } - $this->heading = 'Player'; - $this->description = HelperStatisticType::get($this->filters); if (isset($data['description'])) { diff --git a/app/Filament/Widgets/ChartPlayerMoAv.php b/app/Filament/Widgets/ChartPlayerMoAv.php index 7c1db668..5b01b598 100644 --- a/app/Filament/Widgets/ChartPlayerMoAv.php +++ b/app/Filament/Widgets/ChartPlayerMoAv.php @@ -16,7 +16,7 @@ class ChartPlayerMoAv extends ChartWidget protected bool $isCollapsible = true; - public ?string $header; + protected ?string $heading = 'Player'; protected ?array $options = [ 'plugins' => [ @@ -40,8 +40,6 @@ class ChartPlayerMoAv extends ChartWidget return []; } - $this->heading = 'Player'; - $this->description = ucfirst(HelperStatisticType::get($this->filters)).' Moving Average'; $data = LoadPlayer::load($this->filters); diff --git a/app/Filament/Widgets/ChartPlayerPerf.php b/app/Filament/Widgets/ChartPlayerPerf.php new file mode 100644 index 00000000..6831173e --- /dev/null +++ b/app/Filament/Widgets/ChartPlayerPerf.php @@ -0,0 +1,31 @@ +diffInDays(Carbon::now())).''; + $labels[] = round($value['starttime']->diffInDays(Carbon::now())); $playing_date[] = $value['starttime']->format('d. F Y H:i'); diff --git a/app/Filament/Widgets/Data/LoadPerformance.php b/app/Filament/Widgets/Data/LoadPerformance.php new file mode 100644 index 00000000..f7af2397 --- /dev/null +++ b/app/Filament/Widgets/Data/LoadPerformance.php @@ -0,0 +1,146 @@ +with('away')->where('id', $match_id)->first(); + $home_id = $match->home_id; + $away_id = $match->away_id; + + $res = StatistikoEvent::where('league_id', $league_id)->get(); + + $home_matches = []; + $away_matches = []; + $labels = []; + foreach ($res as $val) { + if ($val->home_id == $home_id or $val->away_id == $home_id or $val->home_id == $away_id or $val->away_id == $away_id) { + $labels[] = round($val->starttime->diffInDays(Carbon::now())); + $label_formatted[$val->match_id] = $val->starttime->format('d. F Y H:i'); + + if ($val->home_id == $home_id or $val->away_id == $home_id) { + $home_matches[] = $val->match_id; + } else { + $home_matches[] = null; + } + if ($val->home_id == $away_id or $val->away_id == $away_id) { + $away_matches[] = $val->match_id; + } else { + $away_matches[] = null; + } + } + } + + $res = StatistikoPlayer::whereIn('match_id', $home_matches)->select('match_id', 'minutes_played', 'playerrating_value')->get(); + $homes = self::filterMatches($res, $home_matches); + + $res = StatistikoPlayer::whereIn('match_id', $away_matches)->select('match_id', 'minutes_played', 'playerrating_value')->get(); + $aways = self::filterMatches($res, $away_matches); + + $data = [ + 'datasets' => [ + [ + 'label' => $match->home->name, + 'data' => $homes, + 'spanGaps' => true, + 'tension' => 0.5, + 'borderColor' => '#fb2c36', + 'fill' => [ + 'target' => 1, + 'above' => '#fb2c3680', + 'below' => '#fdc70080', + ], + ], + [ + 'label' => $match->away->name, + 'data' => $aways, + 'spanGaps' => true, + 'tension' => 0.5, + 'borderColor' => '#fdc700', + 'fill' => false, + ], + ], + 'labels' => $labels, + ]; + + return $data; + + /** + * IF WITH PLAYER + */ + // $matches_played = 0; + // $description = ''; + // if ($tm_player_id) { + // $matches_played = StatistikoPlayer::where('tm_player_id', (int) $tm_player_id) + // ->when($player_on_field, function ($query) use ($player_on_field) { + // switch ($player_on_field) { + // case 'lineup': + // return $query->where('lineup', 'T'); + // case 'substituted': + // return $query->where('lineup', 'P'); + // } + // }) + // ->pluck('match_id')->toArray(); + + // if (! $matches_played) { + // return []; + // } + + // $description = ' (with '.StatistikoDetail::where('tm_player_id', (int) $tm_player_id)->value('name').')'; + // } + + } + + public static function filterMatches($res, $matches) + { + $result = []; + foreach ($res as $value) { + $match_id = $value->match_id; + if (isset($return[$match_id])) { + $result[$match_id]['sum_min'] += $value->minutes_played; + $result[$match_id]['rating'] += $value->playerrating_value * $value->minutes_played; + } else { + $result[$match_id]['sum_min'] = $value->minutes_played; + $result[$match_id]['rating'] = $value->playerrating_value * $value->minutes_played; + } + } + + $return = []; + foreach ($matches as $matchid) { + if (isset($result[$matchid])) { + $return[] = $result[$matchid]['rating'] / $result[$matchid]['sum_min']; + } else { + $return[] = null; + } + } + + return $return; + } +} diff --git a/app/Filament/Widgets/StatsMedianAverage.php b/app/Filament/Widgets/StatsMedianAverage.php index 1f7ef7a6..74643e1d 100644 --- a/app/Filament/Widgets/StatsMedianAverage.php +++ b/app/Filament/Widgets/StatsMedianAverage.php @@ -24,9 +24,7 @@ class StatsMedianAverage extends StatsOverviewWidget protected function getStats(): array { - $filters = $this->filters; - - $data = LoadEvents::load($filters); + $data = LoadEvents::load($this->filters, 'Events'); if (! isset($data['datasets'][0])) { return [];