Added chart js plugin package and sets static label
This commit is contained in:
@@ -2,7 +2,11 @@
|
||||
|
||||
namespace App\Filament\Widgets;
|
||||
|
||||
use App\Models\Line;
|
||||
use Filament\Widgets\ChartWidget;
|
||||
use Illuminate\Support\Js;
|
||||
use Filament\Support\RawJs;
|
||||
use Illuminate\Support\Stringable;
|
||||
|
||||
class ProductionOrderChart extends ChartWidget
|
||||
{
|
||||
@@ -10,70 +14,93 @@ class ProductionOrderChart extends ChartWidget
|
||||
|
||||
protected int|string|array $columnSpan = 12;
|
||||
|
||||
protected static ?string $maxHeight = '330px';
|
||||
|
||||
protected $listeners = ['productionOrderChart'];
|
||||
|
||||
|
||||
protected function getData(): array
|
||||
{
|
||||
$activeFilter = $this->filter;
|
||||
|
||||
$selectedPlant = session('selected_plant');
|
||||
$selectedLine = session('selected_line');
|
||||
$productionOrder = session('production_order');
|
||||
|
||||
if (!$selectedPlant || !$selectedLine || !$productionOrder)
|
||||
{
|
||||
return [
|
||||
'datasets' => [],
|
||||
'labels' => [],
|
||||
];
|
||||
}
|
||||
$activeFilter = $this->filter;
|
||||
|
||||
if ($activeFilter === 'yesterday') {
|
||||
$startDate = now()->subDay()->setTime(8, 0, 0); // yesterday 8:00 AM
|
||||
$endDate = now()->setTime(8, 0, 0); // today 8:00 AM
|
||||
$groupBy = 'EXTRACT(HOUR FROM production_quantities.created_at)';
|
||||
}
|
||||
else if ($activeFilter === 'this_week') {
|
||||
// Monday 8:00 AM of the current week
|
||||
$startDate = now()->startOfWeek()->setTime(8, 0, 0);
|
||||
$selectedPlant = session('selected_plant');
|
||||
$selectedLine = session('selected_line');
|
||||
$productionOrder = session('production_order');
|
||||
|
||||
// Next Monday 8:00 AM (end of Sunday + 8 hrs)
|
||||
$endDate = now()->endOfWeek()->addDay()->setTime(8, 0, 0);
|
||||
if (!$selectedPlant || !$selectedLine || !$productionOrder) {
|
||||
return [
|
||||
'datasets' => [],
|
||||
'labels' => [],
|
||||
];
|
||||
}
|
||||
|
||||
$groupBy = 'EXTRACT(DOW FROM production_quantities.created_at)'; // Group by day of week
|
||||
}
|
||||
// 1. Check line type
|
||||
$lineType = Line::where('id', $selectedLine)->value('type');
|
||||
|
||||
else if ($activeFilter === 'this_month') {
|
||||
$startDate = now()->startOfMonth();
|
||||
$endDate = now()->endOfMonth();
|
||||
$groupBy = "FLOOR((EXTRACT(DAY FROM production_quantities.created_at) - 1) / 7) + 1";
|
||||
}
|
||||
else
|
||||
{
|
||||
$startDate = now()->setTime(8, 0, 0); // today at 8:00 AM
|
||||
$endDate = now()->copy()->addDay()->setTime(8, 0, 0); // tomorrow at 8:00 AM
|
||||
$groupBy = 'EXTRACT(HOUR FROM production_quantities.created_at)';
|
||||
}
|
||||
// 2. Set base table and columns depending on line type
|
||||
if ($lineType == 'FG Line')
|
||||
{
|
||||
$table = 'quality_validations';
|
||||
$dateColumn = 'created_at'; // adjust if your date column is named differently
|
||||
$plantColumn = 'plant_id';
|
||||
$lineColumn = 'line_id';
|
||||
$orderColumn = 'production_order';
|
||||
}
|
||||
else
|
||||
{
|
||||
$table = 'production_quantities';
|
||||
$dateColumn = 'created_at';
|
||||
$plantColumn = 'plant_id';
|
||||
$lineColumn = 'line_id';
|
||||
$orderColumn = 'production_order';
|
||||
}
|
||||
|
||||
// 3. Set filter logic as before
|
||||
if ($activeFilter == 'yesterday')
|
||||
{
|
||||
$startDate = now()->subDay()->setTime(8, 0, 0);
|
||||
$endDate = now()->setTime(8, 0, 0);
|
||||
$groupBy = "EXTRACT(HOUR FROM $table.$dateColumn)";
|
||||
}
|
||||
else if ($activeFilter == 'this_week')
|
||||
{
|
||||
$startDate = now()->startOfWeek()->setTime(8, 0, 0);
|
||||
$endDate = now()->endOfWeek()->addDay()->setTime(8, 0, 0);
|
||||
$groupBy = "EXTRACT(DOW FROM $table.$dateColumn)";
|
||||
}
|
||||
else if ($activeFilter == 'this_month')
|
||||
{
|
||||
$startDate = now()->startOfMonth();
|
||||
$endDate = now()->endOfMonth();
|
||||
$groupBy = "FLOOR((EXTRACT(DAY FROM $table.$dateColumn) - 1) / 7) + 1";
|
||||
}
|
||||
else
|
||||
{
|
||||
$startDate = now()->setTime(8, 0, 0);
|
||||
$endDate = now()->copy()->addDay()->setTime(8, 0, 0);
|
||||
$groupBy = "EXTRACT(HOUR FROM $table.$dateColumn)";
|
||||
}
|
||||
|
||||
$query = \DB::table('production_quantities')
|
||||
->join('plants', 'production_quantities.plant_id', '=', 'plants.id')
|
||||
->join('lines', 'production_quantities.line_id', '=', 'lines.id')
|
||||
->selectRaw("$groupBy AS time_unit, count(*) AS total_quantity")
|
||||
->whereBetween('production_quantities.created_at', [$startDate, $endDate])
|
||||
->where('plants.id', $selectedPlant)
|
||||
->where('lines.id', $selectedLine)
|
||||
->when($productionOrder, fn($q) => $q->where('production_quantities.production_order', $productionOrder))
|
||||
->groupByRaw($groupBy)
|
||||
->orderByRaw($groupBy)
|
||||
->pluck('total_quantity', 'time_unit')
|
||||
->toArray();
|
||||
if ($activeFilter === 'this_month') {
|
||||
$weeksCount = ceil($endDate->day / 7); // Calculate total weeks dynamically
|
||||
$allWeeks = array_fill(1, $weeksCount, 0); // Initialize all weeks with 0
|
||||
$data = array_replace($allWeeks, $query); // Fill missing weeks with 0
|
||||
// 4. Build the query dynamically
|
||||
$query = \DB::table($table)
|
||||
->join('plants', "$table.$plantColumn", '=', 'plants.id')
|
||||
->join('lines', "$table.$lineColumn", '=', 'lines.id')
|
||||
->selectRaw("$groupBy AS time_unit, count(*) AS total_quantity")
|
||||
->whereBetween("$table.$dateColumn", [$startDate, $endDate])
|
||||
->where('plants.id', $selectedPlant)
|
||||
->where('lines.id', $selectedLine)
|
||||
->when($productionOrder, fn($q) => $q->where("$table.$orderColumn", $productionOrder))
|
||||
->groupByRaw($groupBy)
|
||||
->orderByRaw($groupBy)
|
||||
->pluck('total_quantity', 'time_unit')
|
||||
->toArray();
|
||||
|
||||
if ($activeFilter == 'this_month')
|
||||
{
|
||||
$weeksCount = ceil($endDate->day / 7);
|
||||
$allWeeks = array_fill(1, $weeksCount, 0);
|
||||
$data = array_replace($allWeeks, $query);
|
||||
|
||||
// Generate dynamic week labels
|
||||
$labels = [];
|
||||
for ($i = 1; $i <= $weeksCount; $i++) {
|
||||
$weekStart = $startDate->copy()->addDays(($i - 1) * 7)->format('d M');
|
||||
@@ -83,19 +110,13 @@ class ProductionOrderChart extends ChartWidget
|
||||
|
||||
$orderedData = array_values($data);
|
||||
}
|
||||
else if ($activeFilter === 'this_week') {
|
||||
// Correct week labels: ['Mon', 'Tue', ..., 'Sun']
|
||||
else if ($activeFilter == 'this_week')
|
||||
{
|
||||
$labels = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'];
|
||||
|
||||
// Initialize default data for all 7 days
|
||||
$data = array_fill(0, 7, 0);
|
||||
|
||||
// Fill in data from query results
|
||||
foreach ($query as $dow => $count) {
|
||||
$data[$dow] = $count;
|
||||
}
|
||||
|
||||
// Ensure days are ordered from Monday to Sunday
|
||||
$orderedData = [
|
||||
$data[1] ?? 0, // Monday
|
||||
$data[2] ?? 0, // Tuesday
|
||||
@@ -103,42 +124,55 @@ class ProductionOrderChart extends ChartWidget
|
||||
$data[4] ?? 0, // Thursday
|
||||
$data[5] ?? 0, // Friday
|
||||
$data[6] ?? 0, // Saturday
|
||||
$data[0] ?? 0, // Sunday (move to last)
|
||||
$data[0] ?? 0, // Sunday
|
||||
];
|
||||
}
|
||||
else
|
||||
{
|
||||
// Hourly data (same as before)
|
||||
$allHours = array_fill(0, 24, 0);
|
||||
$data = array_replace($allHours, $query);
|
||||
$allHours = array_fill(0, 24, 0);
|
||||
$data = array_replace($allHours, $query);
|
||||
|
||||
// Shift hours for proper display (8 AM to 7 AM)
|
||||
$shiftedKeys = array_merge(range(8, 23), range(0, 8));
|
||||
$orderedData = array_map(fn($hour) => $data[$hour], $shiftedKeys);
|
||||
$shiftedKeys = array_merge(range(8, 23), range(0, 8));
|
||||
$orderedData = array_map(fn($hour) => $data[$hour], $shiftedKeys);
|
||||
|
||||
// Labels: ["8 AM", "9 AM", ..., "7 AM"]
|
||||
$labels = array_map(fn ($hour) => date("g A", strtotime("$hour:00")), $shiftedKeys);
|
||||
$labels = array_map(fn ($hour) => date("g A", strtotime("$hour:00")), $shiftedKeys);
|
||||
}
|
||||
|
||||
$orderedData = array_map(function ($value) {
|
||||
return ($value == 0 || is_null($value)) ? null : $value;
|
||||
}, $orderedData);
|
||||
|
||||
|
||||
return [
|
||||
'datasets' => [
|
||||
[
|
||||
'label' => match ($activeFilter)
|
||||
{
|
||||
'this_week' => $lineType == 'FG Line'
|
||||
? "Daily Fg Production Order Count This Week"
|
||||
: "Daily Production Order Count This Week",
|
||||
'this_month' => $lineType == 'FG Line'
|
||||
? "Weekly Fg Production Order Count This Month"
|
||||
: "Weekly Production Order Count This Month",
|
||||
'yesterday' => $lineType == 'FG Line'
|
||||
? "Yesterday's Hourly Fg Order Count Production"
|
||||
: "Yesterday's Hourly Order Count Production",
|
||||
default => $lineType == 'FG Line'
|
||||
? "Today's Hourly Fg Production Order Count"
|
||||
: "Today's Hourly Production Order Count",
|
||||
},
|
||||
|
||||
'data' => $orderedData,
|
||||
'borderColor' => 'rgba(75, 192, 192, 1)',
|
||||
'backgroundColor' => 'rgba(75, 192, 192, 0.2)',
|
||||
'fill' => false,
|
||||
'tension' => 0.3,
|
||||
],
|
||||
],
|
||||
'labels' => $labels,
|
||||
];
|
||||
}
|
||||
|
||||
return [
|
||||
'datasets' => [
|
||||
[
|
||||
'label' => match ($activeFilter) {
|
||||
'this_week' => "Daily Production Order Count This Week",
|
||||
'this_month' => "Weekly Production Order Count This Month", // Updated Label
|
||||
'yesterday' => "Yesterday's Hourly Order Count Production",
|
||||
default => "Today's Hourly Production Order Count",
|
||||
},
|
||||
'data' => $orderedData,
|
||||
'borderColor' => 'rgba(75, 192, 192, 1)',
|
||||
'backgroundColor' => 'rgba(75, 192, 192, 0.2)',
|
||||
'fill' => false,
|
||||
'tension' => 0.3,
|
||||
],
|
||||
],
|
||||
'labels' => $labels,
|
||||
];
|
||||
}
|
||||
protected function getType(): string
|
||||
{
|
||||
return 'bar';
|
||||
@@ -147,7 +181,21 @@ class ProductionOrderChart extends ChartWidget
|
||||
protected function getOptions(): array
|
||||
{
|
||||
return [
|
||||
|
||||
'plugins' => [
|
||||
'datalabels' => [
|
||||
// 'anchor' => 'start',
|
||||
// 'align' => 'start',
|
||||
// 'offset' => 1,
|
||||
'anchor' => 'start',
|
||||
'align' => 'start',
|
||||
'offset' => 8,
|
||||
'color' => '#000',
|
||||
'font' => [
|
||||
'weight' => 'bold',
|
||||
],
|
||||
'formatter' => Js::from("function(value) { return Number(value); }"),
|
||||
],
|
||||
],
|
||||
'scales' => [
|
||||
'y' => [
|
||||
'beginAtZero' => true,
|
||||
|
||||
Reference in New Issue
Block a user