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

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