diff --git a/app/Filament/Widgets/StatsMedianAverage.php b/app/Filament/Widgets/StatsMedianAverage.php index d1361fc7..95f14b07 100644 --- a/app/Filament/Widgets/StatsMedianAverage.php +++ b/app/Filament/Widgets/StatsMedianAverage.php @@ -64,37 +64,16 @@ class StatsMedianAverage extends StatsOverviewWidget 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('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), ]; } - sort($arr_home); - sort($arr_away); - - if ($total_home % 2 == 0) { - // even - $m = $total_home / 2; - $m1 = floor($m); - $m2 = ceil($m); - $median_home = round(($arr_home[$m1] + $arr_home[$m2]) / 2, 2); - - $m = $total_away / 2; - $m1 = floor($m); - $m2 = ceil($m); - $median_away = round(($arr_away[$m1] + $arr_away[$m2]) / 2, 2); - - } else { - // odd - $m = $total_home / 2; - $median_home = $arr_home[$m]; - - $m = floor($total_away / 2); - $median_away = $arr_away[$m]; - } + $median_home = self::getMedian($arr_home); + $median_away = self::getMedian($arr_away); $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; @@ -104,22 +83,42 @@ class StatsMedianAverage extends StatsOverviewWidget $value_away = number_format($summe_away / $total_away, 1); return [ - Stat::make('Number of events', new HtmlString('
'.$total_home.'
'))->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('
'.$win.'
'))->extraAttributes(['x-init' => "animate('number-of-win', $win)"]), - Stat::make($data['similar_past_matches'] != 'none' ? 'Draw' : 'Draw: '.$data['results']['d'], new HtmlString('
'.$draw.'
'))->extraAttributes(['x-init' => "animate('number-of-draw', $draw)"]), - Stat::make($data['similar_past_matches'] != 'none' ? 'Lost' : 'Lost: '.$data['results']['l'], new HtmlString('
'.$lost.'
'))->extraAttributes(['x-init' => "animate('number-of-lost', $lost)"]), + Stat::make('Number of events', new HtmlString('
' . $total_home . '
'))->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('
' . $win . '
'))->extraAttributes(['x-init' => "animate('number-of-win', $win)"]), + Stat::make($data['similar_past_matches'] != 'none' ? 'Draw' : 'Draw: ' . $data['results']['d'], new HtmlString('
' . $draw . '
'))->extraAttributes(['x-init' => "animate('number-of-draw', $draw)"]), + Stat::make($data['similar_past_matches'] != 'none' ? 'Lost' : 'Lost: ' . $data['results']['l'], new HtmlString('
' . $lost . '
'))->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('
- 0 - 0 - '.$home_name.' - '.$away_name.' + 0 + 0 + ' . $home_name . ' + ' . $away_name . '
'); } + + 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]; + } + } }