added production line count chart
This commit is contained in:
111
app/Filament/Widgets/CumulativeChart.php
Normal file
111
app/Filament/Widgets/CumulativeChart.php
Normal file
@@ -0,0 +1,111 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Widgets;
|
||||
|
||||
use App\Models\Line;
|
||||
use Filament\Widgets\ChartWidget;
|
||||
|
||||
class CumulativeChart extends ChartWidget
|
||||
{
|
||||
protected static ?string $heading = 'Production Line Count';
|
||||
|
||||
protected $listeners = ['cumulativeChart'];
|
||||
|
||||
|
||||
protected int|string|array $columnSpan = 12;
|
||||
protected function getData(): array
|
||||
{
|
||||
$selectedPlant = session('selected_plant') ?? session('select_plant');
|
||||
$activeFilter = $this->filter;
|
||||
|
||||
// Define date range based on filter
|
||||
switch ($activeFilter) {
|
||||
case 'yesterday':
|
||||
$startDate = now()->subDay()->startOfDay();
|
||||
$endDate = now()->subDay()->endOfDay();
|
||||
break;
|
||||
case 'this_week':
|
||||
$startDate = now()->startOfWeek();
|
||||
$endDate = now()->endOfWeek();
|
||||
break;
|
||||
case 'this_month':
|
||||
$startDate = now()->startOfMonth();
|
||||
$endDate = now()->endOfMonth();
|
||||
break;
|
||||
default: // today
|
||||
$startDate = now()->startOfDay();
|
||||
$endDate = now()->endOfDay();
|
||||
break;
|
||||
}
|
||||
|
||||
// Get all lines for selected plant
|
||||
$lines = Line::where('plant_id', $selectedPlant)
|
||||
->pluck('name', 'id')
|
||||
->toArray();
|
||||
|
||||
// Get total production per line in the date range
|
||||
$production = \DB::table('production_quantities')
|
||||
->select('line_id', \DB::raw('COUNT(*) as total_quantity'))
|
||||
->whereBetween('created_at', [$startDate, $endDate])
|
||||
->whereIn('line_id', array_keys($lines))
|
||||
->groupBy('line_id')
|
||||
->pluck('total_quantity', 'line_id')
|
||||
->toArray();
|
||||
|
||||
// Match quantities with lines (fill 0 if missing)
|
||||
$labels = [];
|
||||
$data = [];
|
||||
|
||||
foreach ($lines as $lineId => $lineName) {
|
||||
$labels[] = $lineName;
|
||||
$data[] = $production[$lineId] ?? 0;
|
||||
}
|
||||
|
||||
return [
|
||||
'labels' => $labels,
|
||||
'datasets' => [
|
||||
[
|
||||
'label' => match ($activeFilter) {
|
||||
'yesterday' => "Production Quantity (Yesterday)",
|
||||
'this_week' => "Weekly Production This Month",
|
||||
'this_month' => "Yesterday's Hourly Production",
|
||||
default => "Today's Production",
|
||||
},
|
||||
'data' => $data,
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
protected function getType(): string
|
||||
{
|
||||
return 'bar';
|
||||
}
|
||||
|
||||
protected function getFilters(): ?array
|
||||
{
|
||||
return [
|
||||
'today' => 'Today',
|
||||
'yesterday' => 'Yesterday',
|
||||
'this_week'=> 'This Week',
|
||||
'this_month'=> 'This Month',
|
||||
];
|
||||
}
|
||||
|
||||
protected function getOptions(): array
|
||||
{
|
||||
return [
|
||||
'scales' => [
|
||||
'y' => [
|
||||
'beginAtZero' => true, //Start Y-axis from 0
|
||||
'ticks' => [
|
||||
'stepSize' => 0.5,
|
||||
],
|
||||
],
|
||||
],
|
||||
|
||||
];
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user