Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

CumulativeGrowth.php 4.6KB

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