Files
nuhodds_fi4/app/Filament/Widgets/StatsMedianAverage.php
T
2026-06-14 17:18:58 +02:00

137 lines
5.4 KiB
PHP

<?php
namespace App\Filament\Widgets;
use App\Filament\Widgets\Data\HelperStatisticType;
use App\Filament\Widgets\Data\LoadEvents;
use App\Utilities\Round;
use Filament\Widgets\StatsOverviewWidget;
use Filament\Widgets\StatsOverviewWidget\Stat;
use Illuminate\Support\HtmlString;
use Livewire\Attributes\Reactive;
class StatsMedianAverage extends StatsOverviewWidget
{
protected ?string $pollingInterval = null;
protected int|array|null $columns = null;
#[Reactive]
public ?array $filters = [];
public ?int $count;
public function mount(array $filters)
{
$this->filters = $filters;
}
protected function getStats(): array
{
$data = LoadEvents::load($this->filters, false);
// dd($data);
$this->columns = [
'default' => 1,
'sm' => 2,
'md' => 3,
'lg' => 6,
];
$statistic_type = HelperStatisticType::get($this->filters);
if (! isset($data['datasets'][0])) {
$data['datasets'][0]['data'] = [];
$data['datasets'][1]['data'] = [];
$total_home = 0;
$total_away = 0;
$summe_home = 0;
$summe_away = 0;
$home_team = '';
$away_team = '';
} else {
$arr_home = $data['datasets'][0]['data'];
$arr_away = $data['datasets'][1]['data'];
$total_home = count($arr_home);
$total_away = count($arr_away);
$summe_home = array_sum($arr_home);
$summe_away = array_sum($arr_away);
$home_team = $data['home_team'];
$away_team = $data['away_team'];
}
// if (! $total_home) {
// return [
// Stat::make('Number of events', 0),
// Stat::make('Average ' . $statistic_type, self::template(0, 0, $home_team, $away_team)),
// Stat::make('Median ' . $statistic_type, self::template(0, 0, $home_team, $away_team)),
// Stat::make('Win', 0),
// Stat::make('Draw', 0),
// Stat::make('Lost', 0),
// ];
// }
if ($arr_home)
$median_home = self::getMedian($arr_home);
else
$median_home = 0;
if ($arr_away)
$median_away = self::getMedian($arr_away);
else
$median_away = 0;
$win = $data['results']['w'] ? number_format(Round::odd($total_home / $data['results']['w']), 2) : 0;
$draw = $data['results']['d'] ? number_format(Round::odd($total_home / $data['results']['d']), 2) : 0;
$lost = $data['results']['l'] ? number_format(Round::odd($total_home / $data['results']['l']), 2) : 0;
if ($total_home)
$value_home = number_format($summe_home / $total_home, 1);
else
$value_home = 0;
if ($total_away)
$value_away = number_format($summe_away / $total_away, 1);
else
$value_away = 0;
return [
Stat::make('Number of events', new HtmlString('<div id="number-of-events">' . $total_home . '</div>'))->extraAttributes(['x-init' => "animate('number-of-events', $total_home)"]),
Stat::make('Average ' . $statistic_type, self::template('avhome', 'avaway', $home_team, $away_team))->extraAttributes(['x-init' => "animate('avhome', $value_home); animate('avaway', $value_away)"]),
Stat::make('Median ' . $statistic_type, self::template('mehome', 'meaway', $home_team, $away_team))->extraAttributes(['x-init' => "animate('mehome', $median_home); animate('meaway', $median_away)"]),
Stat::make($data['similar_past_matches'] != 'none' ? 'Win' : 'Win: ' . $data['results']['w'], new HtmlString('<div id="number-of-win">' . $win . '</div>'))->extraAttributes(['x-init' => "animate('number-of-win', $win)"]),
Stat::make($data['similar_past_matches'] != 'none' ? 'Draw' : 'Draw: ' . $data['results']['d'], new HtmlString('<div id="number-of-draw">' . $draw . '</div>'))->extraAttributes(['x-init' => "animate('number-of-draw', $draw)"]),
Stat::make($data['similar_past_matches'] != 'none' ? 'Lost' : 'Lost: ' . $data['results']['l'], new HtmlString('<div id="number-of-lost">' . $lost . '</div>'))->extraAttributes(['x-init' => "animate('number-of-lost', $lost)"]),
];
}
protected function template(string $home_id, string $away_id, string $home_name, string $away_name): HtmlString
{
return new HtmlString('<div class="grid grid-cols-2 justify-center font-bold text-4xl">
<span class="text-center" id="' . $home_id . '">0</span>
<span class="text-center" id="' . $away_id . '">0</span>
<span class="text-center text-xs text-gray-500 dark:text-gray-400">' . $home_name . '</span>
<span class="text-center text-xs text-gray-500 dark:text-gray-400">' . $away_name . '</span>
</div>');
}
protected function getMedian(array $arr)
{
sort($arr);
$size = count($arr);
if ($size % 2 == 0) {
// even
$m = ($size) / 2;
$m1 = $m - 1;
$m2 = $m;
// dd('GERADE', $arr, $size, $m, $m1, $m2);
return round(($arr[$m1] + $arr[$m2]) / 2, 2);
} else {
// odd (add 1 because array starts with 0)
$m = ($size - 1) / 2;
// dd('UNGERADE', $m, $arr, $arr[$m]);
return $arr[$m];
}
}
}