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

155 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
  1. <?php
  2. namespace App\Admin\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. ->from($table)
  87. ->groupBy('tart_dat');
  88. },'l_tab');
  89. $users = DB::table(function ($query) use ($table,$time) {
  90. $query->selectRaw($time ." AS start_date")
  91. ->from($table);
  92. },'r_tab')
  93. ->joinSub($right, 'l_tab', function ($join) {
  94. $join->on('l_tab.tart_dat', '>=', 'r_tab.start_date');
  95. })
  96. ->addSelect(DB::raw('tart_dat as statistics_time,count(tart_dat) as count'))
  97. ->orderBy('tart_dat')
  98. ->groupBy('tart_dat');
  99. $dateTime = request('created_at', 0);
  100. if ($dateTime) {
  101. $users->whereBetween('tart_dat',$dateTime);
  102. }
  103. $users = $users->get()
  104. ->toArray();
  105. $this->withData([
  106. [
  107. 'name' => '用户数',
  108. 'data' => Arr::pluck($users,'count')
  109. ],
  110. ]
  111. );
  112. $this->withCategories(
  113. Arr::pluck($users,'statistics_time')
  114. );
  115. }
  116. public function withData(array $data)
  117. {
  118. return $this->option('series', $data);
  119. }
  120. /**
  121. * 设置图表类别.
  122. *
  123. * @param array $data
  124. *
  125. * @return $this
  126. */
  127. public function withCategories(array $data)
  128. {
  129. return $this->option('xaxis.categories', $data);
  130. }
  131. /**
  132. * 渲染图表
  133. *
  134. * @return string
  135. */
  136. public function render()
  137. {
  138. $this->buildData();
  139. return parent::render();
  140. }
  141. }