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

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