海南旅游SAAS
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.

178 lines
3.3 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. <?php
  2. namespace App\Admin\Metrics\Examples;
  3. use App\Models\Agent;
  4. use Dcat\Admin\Support\JavaScript;
  5. use Dcat\Admin\Widgets\ApexCharts\Chart;
  6. use Illuminate\Support\Arr;
  7. use Illuminate\Support\Facades\DB;
  8. class AgentStatistics extends Chart
  9. {
  10. public function __construct()
  11. {
  12. parent::__construct();
  13. $this->setUpOptions();
  14. }
  15. /**
  16. * 初始化图表配置
  17. */
  18. protected function setUpOptions()
  19. {
  20. $this->options([
  21. 'chart' => [
  22. //'width' => '180%',
  23. 'type' => 'bar',
  24. 'events' => [
  25. ],
  26. 'toolbar' => ['show' => false],
  27. ],
  28. 'plotOptions' => [
  29. 'bar' => [
  30. //'columnWidth' => '45%',
  31. 'distributed' => true,
  32. ]
  33. ],
  34. 'dataLabels' => [
  35. 'enabled' => false
  36. ],
  37. 'legend' => [
  38. 'show' => false
  39. ],
  40. 'xaxis' => [
  41. //'categories' =>
  42. // [75, 125, 225, 175, 125, 75, 25]
  43. //,
  44. 'labels' => [
  45. 'show' => true,
  46. 'style' => [
  47. 'fontSize' => '12px'
  48. ]
  49. ],
  50. ],
  51. 'yaxis' => [
  52. 'show' => true
  53. ],
  54. 'tooltip' => [
  55. 'x' => ['show' => true],
  56. ],
  57. ]);
  58. }
  59. /**
  60. * 处理图表数据
  61. */
  62. protected function buildData()
  63. {
  64. $query = Agent::query()
  65. ->select('*');
  66. $dateTime = request('created_at', 0);
  67. if ($dateTime) {
  68. $query->whereBetween('created_at',[$dateTime['start'] . ' 00:00:00',$dateTime['end'].' 23:59:59']);
  69. }
  70. switch (request('time_key', 0)) {
  71. case '1':
  72. $query->addSelect(DB::raw("count(id) as sum_price,CONCAT(YEAR(created_at),'-',MONTH(created_at),'-',DAY(created_at)) AS statistics_time"));
  73. break;
  74. case '30':
  75. $query->addSelect(DB::raw("count(id) as sum_price,CONCAT(YEAR(created_at),'-',MONTH(created_at)) AS statistics_time"));
  76. break;
  77. case '365':
  78. $query->addSelect(DB::raw("count(id) as sum_price,CONCAT(YEAR(created_at)) AS statistics_time"));
  79. break;
  80. default:
  81. $query->addSelect(DB::raw("count(id) as sum_price,CONCAT(YEAR(created_at),'-',MONTH(created_at),'-',DAY(created_at)) AS statistics_time"));
  82. }
  83. $users = $query->groupBy('statistics_time')
  84. ->orderBy('statistics_time')
  85. ->get();
  86. $userNum = 0;
  87. $userArr = [];
  88. foreach ($users as $user) {
  89. $userNum += $user->sum_price;
  90. array_push($userArr,$userNum);
  91. }
  92. $this->withData([
  93. [
  94. 'name' => '代理商数量',
  95. 'data' => $userArr
  96. ],
  97. ]
  98. );
  99. $this->withCategories(
  100. Arr::pluck($users,'statistics_time')
  101. );
  102. }
  103. public function withData(array $data)
  104. {
  105. return $this->option('series', $data);
  106. }
  107. /**
  108. * 设置图表类别.
  109. *
  110. * @param array $data
  111. *
  112. * @return $this
  113. */
  114. public function withCategories(array $data)
  115. {
  116. return $this->option('xaxis.categories', $data);
  117. }
  118. /**
  119. * 渲染图表
  120. *
  121. * @return string
  122. */
  123. public function render()
  124. {
  125. $this->buildData();
  126. return parent::render();
  127. }
  128. /**
  129. * 重写初始化JS
  130. * @return string
  131. */
  132. protected function buildDefaultScript()
  133. {
  134. $options = JavaScript::format($this->options);
  135. return <<<JS
  136. (function () {
  137. var options = {$options};
  138. var chart = new ApexCharts(
  139. $("{$this->containerSelector}")[0],
  140. options
  141. );
  142. chart.render();
  143. $(window).resize(function () {
  144. var height = $(window).height() - $('.Dcat_Admin_Widgets_Box').offset().top - 130;
  145. if (height < 320) {
  146. height = 320
  147. }
  148. chart.updateOptions({
  149. 'chart': {
  150. 'height': height
  151. }
  152. });
  153. }).resize();
  154. })();
  155. JS;
  156. }
  157. }