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

142 lines
3.0 KiB

4 years ago
  1. <?php
  2. namespace App\AdminAgent\Metrics\Examples;
  3. use App\Common\DataTime;
  4. use App\Common\OrderStatus;
  5. use App\Models\Order;
  6. use App\Models\User;
  7. use Dcat\Admin\Admin;
  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 Bar
  13. {
  14. /**
  15. * 初始化卡片内容
  16. */
  17. protected function init()
  18. {
  19. parent::init();
  20. $color = Admin::color();
  21. // 卡片内容宽度
  22. $this->contentWidth(0, 12);
  23. // 标题
  24. //$this->title('财务统计');
  25. $this->chartHeight = 500;
  26. // 设置下拉选项
  27. $this->dropdown([
  28. '1' => '日',
  29. '30' => '月',
  30. '365' => '年',
  31. ]);
  32. // 设置图表颜色
  33. $this->chartColors([
  34. $color->green(),
  35. ]);
  36. }
  37. /**
  38. * 处理请求
  39. *
  40. * @param Request $request
  41. *
  42. * @return mixed|void
  43. */
  44. public function handle(Request $request)
  45. {
  46. $query = User::query()
  47. ->where('agent_id',Admin::user()->id)
  48. ->select('*');
  49. switch ($request->get('option')) {
  50. case '1':
  51. $query->addSelect(DB::raw("count(id) as sum_price,CONCAT(YEAR(created_at),'-',MONTH(created_at),'-',DAY(created_at)) AS statistics_time"));
  52. break;
  53. case '30':
  54. $query->addSelect(DB::raw("count(id) as sum_price,CONCAT(YEAR(created_at),'-',MONTH(created_at)) AS statistics_time"));
  55. break;
  56. case '365':
  57. $query->addSelect(DB::raw("count(id) as sum_price,CONCAT(YEAR(created_at)) AS statistics_time"));
  58. break;
  59. default:
  60. $query->addSelect(DB::raw("count(id) as sum_price,CONCAT(YEAR(created_at),'-',MONTH(created_at),'-',DAY(created_at)) AS statistics_time"));
  61. }
  62. $users = $query->groupBy('statistics_time')
  63. ->orderBy('statistics_time')
  64. ->get();
  65. $userNum = 0;
  66. $userArr = [];
  67. foreach ($users as $user) {
  68. $userNum += $user->sum_price;
  69. array_push($userArr,$userNum);
  70. }
  71. $this->withChart(
  72. $userArr
  73. );
  74. $this->chartLabels(
  75. Arr::pluck($users,'statistics_time')
  76. );
  77. }
  78. /**
  79. * 设置图表数据.
  80. *
  81. * @param array $data
  82. *
  83. * @return $this
  84. */
  85. public function withChart(array $data)
  86. {
  87. return $this->chart([
  88. 'series' => [[
  89. 'name' => '用户数',
  90. 'data' => $data
  91. ]],
  92. 'chart' => [
  93. //'width' => '180%',
  94. 'type' => 'bar',
  95. 'events' => [
  96. ],
  97. 'toolbar' => ['show' => false],
  98. ],
  99. 'colors' => $this->colors,
  100. 'plotOptions' => [
  101. 'bar' => [
  102. //'columnWidth' => '45%',
  103. 'distributed' => true,
  104. ]
  105. ],
  106. 'dataLabels' => [
  107. 'enabled' => false
  108. ],
  109. 'legend' => [
  110. 'show' => false
  111. ],
  112. 'xaxis' => [
  113. //'categories' =>
  114. // [75, 125, 225, 175, 125, 75, 25]
  115. //,
  116. 'labels' => [
  117. 'show' => true,
  118. 'style' => [
  119. 'colors' => $this->colors,
  120. 'fontSize' => '12px'
  121. ]
  122. ],
  123. ],
  124. 'yaxis' => [
  125. 'show' => true
  126. ],
  127. 'tooltip' => [
  128. 'x' => ['show' => true],
  129. ],
  130. ]);
  131. }
  132. }