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.

CumulativeTotal.php 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 CumulativeTotal extends ApexChartWidget
  7. {
  8. protected static ?int $sort = 2;
  9. /**
  10. * Chart Id
  11. *
  12. * @var string
  13. */
  14. protected static string $chartId = 'cumulative-total';
  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 exponential smoothing for total companies
  38. $alpha = 0.3; // Smoothing factor, between 0 and 1
  39. $smoothedTotalCompanies = [];
  40. $smoothedTotalCompanies[0] = $totalCompanies[0]; // Initialize the first smoothed value
  41. for ($i = 1, $iMax = count($totalCompanies); $i < $iMax; $i++) {
  42. $smoothedTotalCompanies[$i] = $alpha * $totalCompanies[$i] + (1 - $alpha) * $smoothedTotalCompanies[$i - 1];
  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' => 'line',
  52. 'height' => 350,
  53. 'fontFamily' => 'inherit',
  54. 'toolbar' => [
  55. 'show' => false,
  56. ],
  57. ],
  58. 'title' => [
  59. 'text' => 'Cumulative Total',
  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' => 'Total Companies',
  77. 'data' => $totalCompanies,
  78. ],
  79. [
  80. 'name' => 'Smoothed Total Companies',
  81. 'data' => $smoothedTotalCompanies,
  82. ],
  83. ],
  84. 'xaxis' => [
  85. 'type' => 'category',
  86. 'categories' => $labels,
  87. 'position' => 'bottom',
  88. 'labels' => [
  89. 'show' => true,
  90. ],
  91. ],
  92. 'yaxis' => [
  93. 'decimalsInFloat' => 0,
  94. 'labels' => [
  95. 'show' => true,
  96. ],
  97. ],
  98. 'dataLabels' => [
  99. 'enabled' => false,
  100. ],
  101. 'legend' => [
  102. 'show' => true,
  103. 'position' => 'bottom', // Placing the legend at the right side of the chart.
  104. 'horizontalAlign' => 'center', // Centering the legend items horizontally.
  105. 'floating' => false,
  106. 'labels' => [
  107. 'useSeriesColors' => true,
  108. ],
  109. 'markers' => [
  110. 'width' => 30,
  111. 'height' => 4,
  112. 'radius' => 4,
  113. ],
  114. ],
  115. 'colors' => ['#454DC8', '#22d3ee'],
  116. 'markers' => [
  117. 'size' => 4,
  118. 'hover' => [
  119. 'size' => 7,
  120. ],
  121. ],
  122. 'stroke' => [
  123. 'curve' => 'smooth',
  124. ],
  125. ];
  126. }
  127. }