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

150 lines
2.8 KiB

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