Files
nuhodds_fi4/app/Livewire/StatisticEventChart.php
T
2026-04-24 13:59:48 +02:00

141 lines
3.9 KiB
PHP

<?php
namespace App\Livewire;
use App\Models\Event;
use App\Models\StatistikoEvent;
use Filament\Widgets\ChartWidget;
use Illuminate\Contracts\Support\Htmlable;
use Illuminate\Support\HtmlString;
class StatisticEventChart extends ChartWidget
{
public Event $record;
public ?string $filter = 'gls';
protected bool $hasDeferredFilters = true;
protected ?string $pollingInterval = null;
public function getHeading(): string|Htmlable|null
{
$str = '<div class="flex flex-row gap-4 justify-center bg-gray-300 font-thin">';
$str .= '<div>'.$this->record->country->name.' '.$this->record->group->name.'</div>';
if ($this->record->home->betsapi_image_id) {
$str .= ' <img src="https://assets.b365api.com/images/team/m/'.$this->record->home->betsapi_image_id.'.png" class="h-6">';
}
$str .= '<div>'.$this->record->home->name.'</div>';
$str .= '<div>vs</div>';
$str .= '<div>'.$this->record->away->name.'</div>';
if ($this->record->away->betsapi_image_id) {
$str .= ' <img src="https://assets.b365api.com/images/team/m/'.$this->record->away->betsapi_image_id.'.png" class="h-6">';
}
$str .= '</div>';
return new HtmlString($str);
}
protected function getFilters(): ?array
{
return [
'gls' => 'Goals',
'crn' => 'Corner',
'fls' => 'Committed Fouls',
'ofs' => 'Offsides',
'crd' => 'Cards',
'snt' => 'Shots on target',
'tsh' => 'TotalShots',
];
}
protected function getData(): array
{
$activeFilter = $this->filter;
$res = StatistikoEvent::where('league_id', $this->record['competition_group_id'])
->where('home_id', $this->record['home_id'])
->where('away_id', $this->record['away_id'])
->get();
switch ($activeFilter) {
case 'crn':
$h = 'crn_h';
$a = 'crn_a';
break;
case 'fls':
$h = 'fls_h';
$a = 'fls_a';
break;
case 'ofs':
$h = 'ofs_h';
$a = 'ofs_a';
break;
case 'crd':
$h = 'crd_h';
$a = 'crd_a';
break;
case 'snt':
$h = 'snt_h';
$a = 'snt_a';
break;
case 'tsh':
$h = 'tsh_h';
$a = 'tsh_a';
break;
default:
$h = 'gls_h';
$a = 'gls_a';
break;
}
$starttimes = [];
$data_home = [];
$data_away = [];
foreach ($res as $event) {
$data_home[] = $event[$h];
$data_away[] = $event[$a];
$starttimes[] = $event['starttime']->format('d.m.Y');
}
return [
'datasets' => [
[
'label' => $this->record->home->name,
'data' => $data_home,
'backgroundColor' => '#155dfc',
'borderColor' => '#2b7fff',
],
[
'label' => $this->record->away->name,
'data' => $data_away,
'backgroundColor' => '#00a63e',
'borderColor' => '#00c950',
],
],
'labels' => $starttimes,
'scales' => $this->getOptions(),
];
}
protected function getOptions(): array
{
return [
'scales' => [
'y' => [
'ticks' => [
'precision' => 0, // Forces integers
'beginAtZero' => true,
],
],
],
];
}
protected function getType(): string
{
return 'line';
}
}