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

158 lines
2.9 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
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
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\Database\Eloquent\Model;
  10. use Illuminate\Http\Request;
  11. use Illuminate\Support\Arr;
  12. use Illuminate\Support\Facades\DB;
  13. class UserStatistics extends Chart
  14. {
  15. public function __construct()
  16. {
  17. parent::__construct();
  18. $this->setUpOptions();
  19. }
  20. /**
  21. * 初始化图表配置
  22. */
  23. protected function setUpOptions()
  24. {
  25. $this->options([
  26. 'chart' => [
  27. //'width' => '180%',
  28. 'type' => 'bar',
  29. 'events' => [
  30. ],
  31. 'toolbar' => ['show' => false],
  32. ],
  33. 'plotOptions' => [
  34. 'bar' => [
  35. //'columnWidth' => '45%',
  36. 'distributed' => true,
  37. ]
  38. ],
  39. 'dataLabels' => [
  40. 'enabled' => false
  41. ],
  42. 'legend' => [
  43. 'show' => false
  44. ],
  45. 'xaxis' => [
  46. //'categories' =>
  47. // [75, 125, 225, 175, 125, 75, 25]
  48. //,
  49. 'labels' => [
  50. 'show' => true,
  51. 'style' => [
  52. 'fontSize' => '12px'
  53. ]
  54. ],
  55. ],
  56. 'yaxis' => [
  57. 'show' => true
  58. ],
  59. 'tooltip' => [
  60. 'x' => ['show' => true],
  61. ],
  62. ]);
  63. }
  64. /**
  65. * 处理图表数据
  66. */
  67. protected function buildData()
  68. {
  69. switch (request('time_key', 0)) {
  70. case '1':
  71. $time = "DATE_FORMAT(created_at,'%Y-%m-%d')";
  72. break;
  73. case '30':
  74. $time = "DATE_FORMAT(created_at,'%Y-%m')";
  75. break;
  76. case '365':
  77. $time = "DATE_FORMAT(created_at,'%Y')";
  78. break;
  79. default:
  80. $time = "DATE_FORMAT(created_at,'%Y-%m-%d')";
  81. }
  82. $model = new User;
  83. $table = $model->getTable();
  84. $right = DB::table(function ($query) use ($table,$time){
  85. $query->selectRaw($time." AS tart_dat")
  86. ->where('agent_id',Admin::user()->id)
  87. ->from($table)
  88. ->groupBy('tart_dat');
  89. },'l_tab');
  90. $users = DB::table(function ($query) use ($table,$time) {
  91. $query->selectRaw($time ." AS start_date")
  92. ->where('agent_id',Admin::user()->id)
  93. ->from($table);
  94. },'r_tab')
  95. ->joinSub($right, 'l_tab', function ($join) {
  96. $join->on('l_tab.tart_dat', '>=', 'r_tab.start_date');
  97. })
  98. ->addSelect(DB::raw('tart_dat as statistics_time,count(tart_dat) as count'))
  99. ->orderBy('tart_dat')
  100. ->groupBy('tart_dat');
  101. $dateTime = request('created_at', 0);
  102. if ($dateTime) {
  103. $users->whereBetween('tart_dat',$dateTime);
  104. }
  105. $users = $users->get()
  106. ->toArray();
  107. $this->withData([
  108. [
  109. 'name' => '用户数',
  110. 'data' => Arr::pluck($users,'count')
  111. ],
  112. ]
  113. );
  114. $this->withCategories(
  115. Arr::pluck($users,'statistics_time')
  116. );
  117. }
  118. public function withData(array $data)
  119. {
  120. return $this->option('series', $data);
  121. }
  122. /**
  123. * 设置图表类别.
  124. *
  125. * @param array $data
  126. *
  127. * @return $this
  128. */
  129. public function withCategories(array $data)
  130. {
  131. return $this->option('xaxis.categories', $data);
  132. }
  133. /**
  134. * 渲染图表
  135. *
  136. * @return string
  137. */
  138. public function render()
  139. {
  140. $this->buildData();
  141. return parent::render();
  142. }
  143. }