106 lines
3.6 KiB
PHP
106 lines
3.6 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Widgets;
|
|
|
|
use Filament\Widgets\ChartWidget;
|
|
|
|
class ItemOverview extends ChartWidget
|
|
{
|
|
protected static ?string $heading = 'Chart';
|
|
|
|
|
|
protected int|string|array $columnSpan = 'full';
|
|
|
|
protected function getData(): array
|
|
{
|
|
$activeFilter = $this->filter;
|
|
$query = \DB::table('production_quantities')
|
|
->selectRaw('EXTRACT(HOUR FROM created_at) AS hour, SUM(hourly_quantity) AS total_quantity')
|
|
->whereBetween('created_at', [now()->startOfDay(), now()->endOfDay()])
|
|
->groupByRaw('EXTRACT(HOUR FROM created_at)')
|
|
->orderByRaw('EXTRACT(HOUR FROM created_at)')
|
|
->pluck('total_quantity', 'hour')
|
|
->toArray();
|
|
// Initialize data for each hour interval (8 AM - 7 PM)
|
|
$data = array_fill(8, 12, 0); // 8 AM to 7 PM (indexes 8 to 19)
|
|
|
|
// Populate actual values
|
|
// foreach ($query as $record) {
|
|
// $hour = (int) $record->hour;
|
|
// if ($hour >= 8 && $hour <= 19) {
|
|
// $data[$hour] = $record->total_quantity; // Assign only the hourly production
|
|
// }
|
|
// }
|
|
|
|
// Convert data to chart format
|
|
return [
|
|
'datasets' => [
|
|
[
|
|
'label' => 'Hourly Production',
|
|
'data' => array_values($data), // Values only
|
|
'borderColor' => 'rgba(75, 192, 192, 1)',
|
|
'backgroundColor' => 'rgba(75, 192, 192, 0.2)',
|
|
'fill' => false, // No area fill, just the line
|
|
'tension' => 0.3, // Smooth curve
|
|
],
|
|
],
|
|
'labels' => array_map(fn($h) => ($h <= 11 ? "$h AM" : ($h == 12 ? "12 PM" : ($h - 12) . " PM")), array_keys($data)),
|
|
];
|
|
|
|
// // Initialize data for each hour interval (8 AM - 7 PM)
|
|
// $data = [
|
|
// '8 AM - 9 AM' => 0, '9 AM - 10 AM' => 0, '10 AM - 11 AM' => 0, '11 AM - 12 PM' => 0,
|
|
// '12 PM - 1 PM' => 0, '1 PM - 2 PM' => 0, '2 PM - 3 PM' => 0, '3 PM - 4 PM' => 0,
|
|
// '4 PM - 5 PM' => 0, '5 PM - 6 PM' => 0, '6 PM - 7 PM' => 0, '7 PM - 8 PM' => 0,
|
|
// ];
|
|
|
|
|
|
// // Populate actual values
|
|
// foreach ($query as $record) {
|
|
// $hour = (int) $record->hour;
|
|
// if ($hour >= 8 && $hour <= 19) {
|
|
// $index = ($hour - 8); // Match hour to array index
|
|
// $labels = array_keys($data); // Get labels
|
|
|
|
// // Store only the production for that specific hour
|
|
// $data[$labels[$index]] = $record->total_quantity;
|
|
// }
|
|
// }
|
|
|
|
|
|
// // Debugging: Check if data exists
|
|
// if (empty($query) || array_sum($data) === 0) {
|
|
// \Log::info('No production data found for today.');
|
|
// }
|
|
|
|
// return [
|
|
// 'datasets' => [
|
|
// [
|
|
// 'label' => 'Production Quantities',
|
|
// 'data' => array_values($data),
|
|
// 'borderColor' => 'rgba(75, 192, 192, 1)',
|
|
// 'backgroundColor' => 'rgba(75, 192, 192, 0.2)',
|
|
// 'fill' => true,
|
|
// ],
|
|
// ],
|
|
// 'labels' => array_keys($data),
|
|
// ];
|
|
}
|
|
|
|
protected function getType(): string
|
|
{
|
|
return 'line';
|
|
}
|
|
|
|
protected function getFilters(): ?array
|
|
{
|
|
return [
|
|
'today' => 'Today',
|
|
'week' => 'Last week',
|
|
'month' => 'Last month',
|
|
'year' => 'This year',
|
|
];
|
|
}
|
|
|
|
}
|