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.

CumulativeUserData.php 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. namespace App\Filament\Pages\Widgets\Users\Charts;
  3. use App\Models\User;
  4. use Leandrocfe\FilamentApexCharts\Widgets\ApexChartWidget;
  5. class CumulativeUserData 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-user-data';
  17. /**
  18. * Widget Title
  19. *
  20. * @var string|null
  21. */
  22. protected static ?string $heading = 'Cumulative User Data';
  23. protected function getOptions(): array
  24. {
  25. $startOfYear = today()->startOfYear();
  26. $today = today();
  27. // Company data
  28. $userData = User::selectRaw("COUNT(*) as aggregate, YEARWEEK(created_at, 3) as week")
  29. ->whereBetween('created_at', [$startOfYear, $today])
  30. ->groupByRaw('week')
  31. ->get();
  32. $weeks = [];
  33. for ($week = $startOfYear->copy(); $week->lte($today); $week->addWeek()) {
  34. $weeks[$week->format('oW')] = 0;
  35. }
  36. $weeklyData = collect($weeks)->mapWithKeys(static function ($value, $week) use ($userData) {
  37. $matchingData = $userData->firstWhere('week', $week);
  38. return [$week => $matchingData ? $matchingData->aggregate : 0];
  39. });
  40. $totalUsers = $weeklyData->reduce(static function ($carry, $value) {
  41. $carry[] = ($carry ? end($carry) : 0) + $value;
  42. return $carry;
  43. }, []);
  44. // Calculate percentage increase and increase in companies per week
  45. $newUsers = [0];
  46. $weeklyGrowthRate = [0];
  47. $cumulativeDataLength = count($totalUsers);
  48. for ($key = 1; $key < $cumulativeDataLength; $key++) {
  49. $value = $totalUsers[$key];
  50. $previousWeekValue = $totalUsers[$key - 1];
  51. $newUsers[] = $value - $previousWeekValue;
  52. $weeklyGrowthRate[] = round((($value - $previousWeekValue) / $previousWeekValue) * 100, 2);
  53. }
  54. $labels = collect($weeks)->keys()->map(static function ($week) {
  55. $year = substr($week, 0, 4);
  56. $weekNumber = substr($week, 4);
  57. return today()->setISODate($year, $weekNumber)->format('M d');
  58. });
  59. return [
  60. 'chart' => [
  61. 'type' => 'line',
  62. 'height' => 350,
  63. 'stacked' => false,
  64. 'toolbar' => [
  65. 'show' => false,
  66. ],
  67. ],
  68. 'series' => [
  69. [
  70. 'name' => 'Weekly Growth Rate',
  71. 'type' => 'area',
  72. 'data' => $weeklyGrowthRate,
  73. ],
  74. [
  75. 'name' => 'New Users',
  76. 'type' => 'line',
  77. 'data' => $newUsers,
  78. ],
  79. [
  80. 'name' => 'Total Users',
  81. 'type' => 'column',
  82. 'data' => $totalUsers,
  83. ],
  84. ],
  85. 'xaxis' => [
  86. 'categories' => $labels,
  87. 'position' => 'bottom',
  88. 'labels' => [
  89. 'style' => [
  90. 'colors' => '#9ca3af',
  91. 'fontWeight' => 600,
  92. ],
  93. ],
  94. ],
  95. 'yaxis' => [
  96. [
  97. 'seriesName' => 'Weekly Growth Rate',
  98. 'labels' => [
  99. 'style' => [
  100. 'colors' => '#9ca3af',
  101. 'fontWeight' => 600,
  102. ],
  103. ],
  104. ],
  105. [
  106. 'seriesName' => 'New Users',
  107. 'decimalsInFloat' => 0,
  108. 'opposite' => true,
  109. 'labels' => [
  110. 'style' => [
  111. 'colors' => '#9ca3af',
  112. 'fontWeight' => 600,
  113. ],
  114. ],
  115. ],
  116. [
  117. 'seriesName' => 'Total Users',
  118. 'decimalsInFloat' => 0,
  119. 'opposite' => true,
  120. 'labels' => [
  121. 'style' => [
  122. 'colors' => '#9ca3af',
  123. 'fontWeight' => 600,
  124. ],
  125. ],
  126. ],
  127. ],
  128. 'legend' => [
  129. 'position' => 'top',
  130. 'horizontalAlign' => 'center',
  131. 'labels' => [
  132. 'colors' => '#9ca3af',
  133. 'fontWeight' => 600,
  134. ],
  135. ],
  136. 'markers' => [
  137. 'size' => 0,
  138. ],
  139. 'colors' => ['#6d28d9', '#3b82f6', '#d946ef'],
  140. 'fill' => [
  141. 'type' => 'gradient',
  142. 'gradient' => [
  143. 'shade' => 'dark',
  144. 'type' => 'vertical',
  145. 'shadeIntensity' => 0.5,
  146. 'gradientToColors' => ['#6d28d9', '#0ea5e9', '#d946ef'],
  147. 'inverseColors' => false,
  148. 'opacityFrom' => [0.85, 1, 0.75],
  149. 'opacityTo' => [0.4, 0.85, 1],
  150. 'stops' => [0, 20, 80, 100],
  151. ],
  152. ],
  153. 'stroke' => [
  154. 'width' => [2, 5, 0],
  155. 'curve' => 'smooth',
  156. ],
  157. 'plotOptions' => [
  158. 'bar' => [
  159. 'borderRadius' => 5,
  160. 'borderRadiusApplication' => 'end',
  161. 'columnWidth' => '60%',
  162. ],
  163. ],
  164. ];
  165. }
  166. }