Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

CumulativeEmployeeData.php 5.6KB

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