Added chart for games per category

This commit is contained in:
Anna-Sara Sélea 2025-09-21 20:56:00 +02:00
parent b5844f0f48
commit a3f3773834
4 changed files with 78 additions and 7 deletions

View file

@ -8,6 +8,8 @@ use Filament\Widgets\StatsOverviewWidget\Card;
use App\Models\User;
use App\Models\Item;
use App\Models\Reserveditem;
use Filament\Support\Enums\IconPosition;
class AdminWidget extends BaseWidget
{
@ -17,10 +19,10 @@ class AdminWidget extends BaseWidget
{
return [
//Card::make(__('Total number of users'), User::count() ),
Card::make(__('Total number of games'), Item::where('type', 'game')->count() ),
Card::make(__('Total number of items'), Item::where('type', 'item')->count() ),
Card::make(__('Reservations at the moment'), Reserveditem::where('returned_date', null)->count() ),
Card::make(__('Reservations over time'), Reserveditem::withTrashed()->withTrashed()->count() ),
Stat::make(__('Total amount of games'), Item::where('type', 'game')->count() ),
Stat::make(__('Total amount of items'), Item::where('type', 'item')->count() ),
Stat::make(__('Reservations at the moment'), Reserveditem::where('returned_date', null)->count() ),
Stat::make(__('Reservations over time'), Reserveditem::withTrashed()->withTrashed()->count() ),
];
}

View file

@ -0,0 +1,66 @@
<?php
namespace App\Filament\Widgets;
use Filament\Widgets\ChartWidget;
use App\Models\Reserveditem;
use App\Models\Category;
class PopularCategoryChart extends ChartWidget
{
protected static ?string $heading = 'Game reservations by category';
protected static ?string $maxHeight = '275px';
protected static ?array $options = [
'scales' => [
'x' => [
'display' => false,
],
'y' => [
'display' => false,
],
],
];
protected function getData(): array
{
$categories = Category::where('type', 'game')->get();
$categoryNames = [];
foreach ($categories as $obj){
$categoryNames[] = $obj->name;
}
$items = Reserveditem::withTrashed()->with('item')->whereHas('item', function($query){
return $query->where('type', 'game');
})->get()->groupBy('item.category_id');
$itemCategoriesCount = [];
foreach ($items as $item){
$itemCategoriesCount[] = $item->count();
}
return [
'datasets' => [
[
'label' => __('Reservation Category'),
'data' => $itemCategoriesCount,
'backgroundColor' => ["#03045e","#0077b6","#00b4d8","#90e0ef","#caf0f8"],
"hoverOffset" => 4,
],
],
'labels' => $categoryNames,
];
}
protected function getType(): string
{
return 'pie';
}
}

View file

@ -10,6 +10,7 @@ use App\Models\Reserveditem;
class ReservationsChart extends ChartWidget
{
protected static ?string $heading = "Reservations by month";
protected static ?string $maxHeight = '300px';
protected function getData(): array

View file

@ -33,8 +33,8 @@
"Is admin": "Admin",
"Password": "Lösenord",
"Email verified at": "Email verifierad den",
"Total number of games": "Totalt antal spel i lager",
"Total number of items": "Totalt antal saker i lager",
"Total amount of games": "Totalt antal spel i lager",
"Total amount of items": "Totalt antal saker i lager",
"Reservations at the moment": "Nuvarande reservationer",
"Reservations over time": "Alla reservationer",
"Phone": "Telefonnummer",
@ -42,5 +42,7 @@
"Category literature": "Kategori litteratur",
"Category games": "Kategori spel",
"Category items": "Kategori saker",
"Reservations by month": "Reservationer per månad"
"Reservations by month": "Reservationer per månad",
"Amount of games in inventory": "Antal spel i lager",
"Amount of items in inventory": "Antal saker i lager"
}