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.

CumulativeGrowth.php 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. namespace App\Filament\Pages\Widgets\Employees\Charts;
  3. use App\Models\Employeeship;
  4. use Leandrocfe\FilamentApexCharts\Widgets\ApexChartWidget;
  5. class CumulativeGrowth extends ApexChartWidget
  6. {
  7. protected static ?int $sort = 1;
  8. /**
  9. * Chart Id
  10. *
  11. * @var string
  12. */
  13. protected static string $chartId = 'cumulative-growth';
  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, DATE_FORMAT(created_at, '%Y%m') as month")
  21. ->whereBetween('created_at', [$startOfYear, $today])
  22. ->groupByRaw('month')
  23. ->get();
  24. $months = [];
  25. for ($month = $startOfYear->copy(); $month->lte($today); $month->addMonth()) {
  26. $months[$month->format('Ym')] = 0;
  27. }
  28. $monthlyData = collect($months)->mapWithKeys(static function ($value, $month) use ($employeeData) {
  29. $matchingData = $employeeData->firstWhere('month', $month);
  30. return [$month => $matchingData->aggregate ?? 0];
  31. });
  32. $totalEmployees = $monthlyData->reduce(static function ($carry, $value) {
  33. $carry[] = ($carry ? end($carry) : 0) + $value;
  34. return $carry;
  35. }, []);
  36. // Calculate percentage increase and increase in companies per month
  37. $newEmployees = [0];
  38. $monthlyPercentageChange = [0];
  39. for ($i = 1, $iMax = count($totalEmployees); $i < $iMax; $i++) {
  40. $newEmployees[] = $totalEmployees[$i] - $totalEmployees[$i - 1];
  41. $monthlyPercentageChange[] = $totalEmployees[$i - 1] !== 0 ? ($newEmployees[$i] / $totalEmployees[$i - 1]) * 100 : 0;
  42. }
  43. $labels = collect($months)->keys()->map(static function ($month) {
  44. $year = substr($month, 0, 4);
  45. $monthNumber = substr($month, 4);
  46. return today()->startOfYear()->setDate($year, $monthNumber, 1)->format('M');
  47. });
  48. return [
  49. 'chart' => [
  50. 'type' => 'area',
  51. 'height' => 350,
  52. 'fontFamily' => 'inherit',
  53. 'toolbar' => [
  54. 'show' => false,
  55. ],
  56. ],
  57. 'title' => [
  58. 'text' => 'Cumulative Growth',
  59. 'align' => 'left',
  60. 'margin' => 20,
  61. 'style' => [
  62. 'fontSize' => '20px',
  63. ],
  64. ],
  65. 'subtitle' => [
  66. 'text' => 'Monthly',
  67. 'align' => 'left',
  68. 'margin' => 20,
  69. 'style' => [
  70. 'fontSize' => '14px',
  71. ],
  72. ],
  73. 'series' => [
  74. [
  75. 'name' => 'Growth Rate',
  76. 'data' => $monthlyPercentageChange,
  77. ],
  78. [
  79. 'name' => 'New Employees',
  80. 'data' => $newEmployees,
  81. ],
  82. ],
  83. 'xaxis' => [
  84. 'categories' => $labels,
  85. 'position' => 'bottom',
  86. 'labels' => [
  87. 'show' => true,
  88. 'style' => [
  89. 'colors' => '#9ca3af',
  90. ],
  91. ],
  92. ],
  93. 'yaxis' => [
  94. 'decimalsInFloat' => 2,
  95. 'labels' => [
  96. 'style' => [
  97. 'colors' => '#9ca3af',
  98. ],
  99. ],
  100. ],
  101. 'dataLabels' => [
  102. 'enabled' => false,
  103. ],
  104. 'legend' => [
  105. 'show' => true,
  106. 'position' => 'bottom',
  107. 'horizontalAlign' => 'center',
  108. 'floating' => false,
  109. 'labels' => [
  110. 'useSeriesColors' => true,
  111. ],
  112. 'markers' => [
  113. 'width' => 30,
  114. 'height' => 8,
  115. 'radius' => 0,
  116. ],
  117. ],
  118. 'colors' => ['#454DC8', '#22d3ee'],
  119. 'fill' => [
  120. 'type' => 'gradient',
  121. 'gradient' => [
  122. 'opacityFrom' => 0.6,
  123. 'opacityTo' => 0.8,
  124. ],
  125. ],
  126. 'markers' => [
  127. 'size' => 4,
  128. 'hover' => [
  129. 'size' => 7,
  130. ],
  131. ],
  132. 'stroke' => [
  133. 'curve' => 'smooth',
  134. ],
  135. ];
  136. }
  137. }