Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

CumulativeEmployeeData.php 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. namespace App\Filament\Pages\Widgets;
  3. use App\Models\Company;
  4. use App\Models\Employeeship;
  5. use App\Models\User;
  6. use Leandrocfe\FilamentApexCharts\Widgets\ApexChartWidget;
  7. class CumulativeEmployeeData extends ApexChartWidget
  8. {
  9. protected int|string|array $columnSpan = [
  10. 'md' => 2,
  11. 'xl' => 3,
  12. ];
  13. /**
  14. * Chart Id
  15. *
  16. * @var string
  17. */
  18. protected static string $chartId = 'cumulative-employee-data';
  19. /**
  20. * Widget Title
  21. *
  22. * @var string|null
  23. */
  24. protected static ?string $heading = 'Cumulative Employee Data';
  25. protected function getOptions(): array
  26. {
  27. $startOfYear = today()->startOfYear();
  28. $today = today();
  29. // Company data
  30. $employeeData = Employeeship::selectRaw("COUNT(*) as aggregate, role, YEARWEEK(created_at, 3) as week")
  31. ->whereBetween('created_at', [$startOfYear, $today])
  32. ->groupByRaw('week, role')
  33. ->get();
  34. $weeks = [];
  35. for ($week = $startOfYear->copy(); $week->lte($today); $week->addWeek()) {
  36. $weeks[$week->format('oW')] = 0;
  37. }
  38. $weeklyRoleData = collect($weeks)->mapWithKeys(static function ($value, $week) use ($employeeData) {
  39. $editors = $employeeData->where('role', 'editor')->where('week', $week)->first();
  40. $admins = $employeeData->where('role', 'admin')->where('week', $week)->first();
  41. return [
  42. $week => [
  43. 'editors' => $editors ? $editors->aggregate : 0,
  44. 'admins' => $admins ? $admins->aggregate : 0,
  45. ]
  46. ];
  47. });
  48. $cumulativeEditors = $weeklyRoleData->reduce(function ($carry, $value) {
  49. $carry[] = ($carry ? end($carry) : 0) + $value['editors'];
  50. return $carry;
  51. }, []);
  52. $cumulativeAdmins = $weeklyRoleData->reduce(function ($carry, $value) {
  53. $carry[] = ($carry ? end($carry) : 0) + $value['admins'];
  54. return $carry;
  55. }, []);
  56. $totalEmployees = [];
  57. for ($i = 0; $i < count($cumulativeEditors); $i++) {
  58. $totalEmployees[] = $cumulativeEditors[$i] + $cumulativeAdmins[$i];
  59. }
  60. $weeklyGrowthRate = [0];
  61. for ($i = 1; $i < count($totalEmployees); $i++) {
  62. $growth = (($totalEmployees[$i] - $totalEmployees[$i - 1]) / $totalEmployees[$i - 1]) * 100;
  63. $weeklyGrowthRate[] = round($growth, 2);
  64. }
  65. $labels = collect($weeks)->keys()->map(static function ($week) {
  66. $year = substr($week, 0, 4);
  67. $weekNumber = substr($week, 4);
  68. return today()->setISODate($year, $weekNumber)->format('M d');
  69. });
  70. return [
  71. 'chart' => [
  72. 'type' => 'line',
  73. 'height' => 350,
  74. 'stacked' => true,
  75. 'toolbar' => [
  76. 'show' => false,
  77. ],
  78. ],
  79. 'series' => [
  80. [
  81. 'name' => 'Editors',
  82. 'type' => 'bar',
  83. 'data' => $cumulativeEditors,
  84. ],
  85. [
  86. 'name' => 'Admins',
  87. 'type' => 'bar',
  88. 'data' => $cumulativeAdmins,
  89. ],
  90. [
  91. 'name' => 'Weekly Growth Rate',
  92. 'type' => 'area',
  93. 'data' => $weeklyGrowthRate,
  94. ],
  95. ],
  96. 'stroke' => [
  97. 'width' => [0, 0, 2],
  98. 'curve' => 'smooth',
  99. ],
  100. 'xaxis' => [
  101. 'categories' => $labels,
  102. 'position' => 'bottom',
  103. 'labels' => [
  104. 'style' => [
  105. 'colors' => '#9ca3af',
  106. 'fontWeight' => 600,
  107. ],
  108. ],
  109. ],
  110. 'yaxis' => [
  111. 'labels' => [
  112. 'style' => [
  113. 'colors' => '#9ca3af',
  114. 'fontWeight' => 600,
  115. ],
  116. ],
  117. ],
  118. 'legend' => [
  119. 'labels' => [
  120. 'colors' => '#9ca3af',
  121. 'fontWeight' => 600,
  122. ],
  123. ],
  124. 'colors' => ['#d946ef', '#6d28d9', '#3b82f6'],
  125. 'fill' => [
  126. 'type' => 'gradient',
  127. 'gradient' => [
  128. 'shade' => 'dark',
  129. 'type' => 'vertical',
  130. 'shadeIntensity' => 0.2,
  131. 'gradientToColors' => ['#ec4899', '#8b5cf6', '#0ea5e9'],
  132. 'inverseColors' => true,
  133. 'opacityFrom' => [0.85, 0.85, 0.85],
  134. 'opacityTo' => [0.85, 0.85, 0.4],
  135. 'stops' => [0, 100, 100],
  136. ],
  137. ],
  138. 'plotOptions' => [
  139. 'bar' => [
  140. 'horizontal' => false,
  141. 'borderRadius' => 5,
  142. 'borderRadiusApplication' => 'end',
  143. 'columnWidth' => '60%',
  144. 'dataLabels' => [
  145. 'total' => [
  146. 'enabled' => true,
  147. 'style' => [
  148. 'color' => '#9ca3af',
  149. 'fontSize' => '14px',
  150. 'fontWeight' => 600,
  151. ],
  152. ]
  153. ],
  154. ],
  155. ],
  156. 'dataLabels' => [
  157. 'enabled' => false,
  158. ],
  159. ];
  160. }
  161. }