You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CumulativeRoles.php 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. namespace App\Filament\Pages\Widgets\Employees\Charts;
  3. use App\Models\Employeeship;
  4. use Leandrocfe\FilamentApexCharts\Widgets\ApexChartWidget;
  5. class CumulativeRoles extends ApexChartWidget
  6. {
  7. protected static ?int $sort = 0;
  8. /**
  9. * Chart Id
  10. *
  11. * @var string
  12. */
  13. protected static string $chartId = 'cumulative-roles';
  14. protected static ?string $pollingInterval = null;
  15. protected function getOptions(): array
  16. {
  17. $startOfYear = today()->startOfYear();
  18. $today = today();
  19. // Company data
  20. $employeeData = Employeeship::selectRaw("COUNT(*) as aggregate, role, DATE_FORMAT(created_at, '%Y%m') as month")
  21. ->whereBetween('created_at', [$startOfYear, $today])
  22. ->groupByRaw('month, role')
  23. ->get();
  24. $months = [];
  25. for ($month = $startOfYear->copy(); $month->lte($today); $month->addMonth()) {
  26. $months[$month->format('Ym')] = 0;
  27. }
  28. $monthlyRoleData = collect($months)->mapWithKeys(static function ($value, $month) use ($employeeData) {
  29. $editors = $employeeData->where('role', 'editor')->where('month', $month)->first();
  30. $admins = $employeeData->where('role', 'admin')->where('month', $month)->first();
  31. return [
  32. $month => [
  33. 'editors' => $editors->aggregate ?? 0,
  34. 'admins' => $admins->aggregate ?? 0,
  35. ]
  36. ];
  37. });
  38. $cumulativeEditors = $monthlyRoleData->reduce(static function ($carry, $value) {
  39. $carry[] = ($carry ? end($carry) : 0) + $value['editors'];
  40. return $carry;
  41. }, []);
  42. $cumulativeAdmins = $monthlyRoleData->reduce(static function ($carry, $value) {
  43. $carry[] = ($carry ? end($carry) : 0) + $value['admins'];
  44. return $carry;
  45. }, []);
  46. $labels = collect($months)->keys()->map(static function ($month) {
  47. $year = substr($month, 0, 4);
  48. $monthNumber = substr($month, 4);
  49. return today()->startOfYear()->setDate($year, $monthNumber, 1)->format('M');
  50. });
  51. return [
  52. 'chart' => [
  53. 'type' => 'bar',
  54. 'height' => 350,
  55. 'fontFamily' => 'inherit',
  56. 'toolbar' => [
  57. 'show' => false,
  58. ],
  59. ],
  60. 'title' => [
  61. 'text' => 'Cumulative Roles',
  62. 'align' => 'left',
  63. 'margin' => 20,
  64. 'style' => [
  65. 'fontSize' => '20px',
  66. ],
  67. ],
  68. 'subtitle' => [
  69. 'text' => 'Monthly',
  70. 'align' => 'left',
  71. 'margin' => 20,
  72. 'style' => [
  73. 'fontSize' => '14px',
  74. ],
  75. ],
  76. 'series' => [
  77. [
  78. 'name' => 'Editors',
  79. 'data' => $cumulativeEditors,
  80. ],
  81. [
  82. 'name' => 'Admins',
  83. 'data' => $cumulativeAdmins,
  84. ],
  85. ],
  86. 'xaxis' => [
  87. 'categories' => $labels,
  88. 'position' => 'bottom',
  89. 'labels' => [
  90. 'show' => true,
  91. 'style' => [
  92. 'colors' => '#9ca3af',
  93. ],
  94. ],
  95. ],
  96. 'yaxis' => [
  97. 'decimalsInFloat' => 2,
  98. 'labels' => [
  99. 'style' => [
  100. 'colors' => '#9ca3af',
  101. ],
  102. ],
  103. ],
  104. 'dataLabels' => [
  105. 'enabled' => false,
  106. ],
  107. 'legend' => [
  108. 'show' => true,
  109. 'position' => 'bottom',
  110. 'horizontalAlign' => 'center',
  111. 'floating' => false,
  112. 'labels' => [
  113. 'useSeriesColors' => true,
  114. ],
  115. 'markers' => [
  116. 'width' => 12,
  117. 'height' => 12,
  118. 'radius' => 0,
  119. ],
  120. ],
  121. 'tooltip' => [
  122. 'enabled' => true,
  123. 'shared' => true,
  124. 'intersect' => false,
  125. 'x' => [
  126. 'show' => true,
  127. ],
  128. ],
  129. 'colors' => ['#454DC8', '#22d3ee'],
  130. 'plotOptions' => [
  131. 'bar' => [
  132. 'horizontal' => false,
  133. 'endingShape' => 'rounded',
  134. 'columnWidth' => '55%',
  135. ],
  136. ],
  137. 'markers' => [
  138. 'size' => 4,
  139. 'hover' => [
  140. 'size' => 7,
  141. ],
  142. ],
  143. 'stroke' => [
  144. 'curve' => 'smooth',
  145. ],
  146. ];
  147. }
  148. }