'$refresh']; // Listen for filter updates protected function getData(): array { $activeFilter = $this->filter; // Get filter values from session $selectedPlant = session('selected_plant'); $selectedLine = session('selected_line'); $query = \DB::table('production_quantities') ->join('plants', 'production_quantities.plant_id', '=', 'plants.id') // Join plants table ->join('lines', 'production_quantities.line_id', '=', 'lines.id') // Join lines table ->selectRaw('EXTRACT(HOUR FROM production_quantities.created_at) AS hour, count(*) AS total_quantity') ->whereBetween('production_quantities.created_at', [now()->startOfDay(), now()->endOfDay()]) ->when($selectedPlant, function ($q) use ($selectedPlant) { return $q->where('plants.id', $selectedPlant); }) ->when($selectedLine, function ($q) use ($selectedLine) { return $q->where('lines.id', $selectedLine); }) ->groupByRaw('EXTRACT(HOUR FROM production_quantities.created_at)') ->orderByRaw('EXTRACT(HOUR FROM production_quantities.created_at)') ->pluck('total_quantity', 'hour') ->toArray(); // // Ensure all 24 hours are covered, filling missing ones with zero // $data = array_replace(array_fill(8, 24, 0), $query); // return [ // 'datasets' => [ // [ // 'label' => 'Hourly Production', // 'data' => array_values($data), // 'borderColor' => 'rgba(75, 192, 192, 1)', // 'backgroundColor' => 'rgba(75, 192, 192, 0.2)', // 'fill' => false, // 'tension' => 0.3, // ], // ], // // 'labels' => array_map(fn ($hour) => ($hour == 0 ? '12 AM' : ($hour == 12 ? '12 PM' : ($hour < 12 ? "{$hour} AM" : ($hour - 12) . " PM"))), array_keys($data)), // // Labels in 24-hour format // 'labels' => array_map(fn ($hour) => date("g A", strtotime("$hour:00")), array_keys($data)), // Improved hour formatting // ]; $allHours = array_fill(0, 24, 0); $data = array_replace($allHours, $query); $shiftedKeys = range(8, 23); // 8 AM to 11 PM $shiftedKeys = array_merge($shiftedKeys, range(0, 8)); $orderedData = array_map(fn($hour) => $data[$hour], $shiftedKeys); return [ 'datasets' => [ [ 'label' => 'Hourly Production', 'data' => array_values($orderedData), 'borderColor' => 'rgba(75, 192, 192, 1)', 'backgroundColor' => 'rgba(75, 192, 192, 0.2)', 'fill' => false, 'tension' => 0.3, ], ], // Correct label sequence from 8 AM to 7 AM 'labels' => array_map(fn ($hour) => date("g A", strtotime("$hour:00")), $shiftedKeys), ]; } protected function getType(): string { return 'line'; } protected function getOptions(): array { return [ 'scales' => [ 'y' => [ 'beginAtZero' => true, //Start Y-axis from 0 'ticks' => [ 'stepSize' => 0.5, ], ], ], ]; } protected function getFilters(): ?array { return [ 'today' => 'Today', ]; } }