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

141 lines
2.7 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
  1. <?php
  2. namespace App\AdminAgent\Metrics\Examples;
  3. use App\Common\OrderStatus;
  4. use App\Models\Order;
  5. use Dcat\Admin\Admin;
  6. use Dcat\Admin\Widgets\ApexCharts\Chart;
  7. use Dcat\Admin\Widgets\Metrics\Bar;
  8. use Illuminate\Http\Request;
  9. use Illuminate\Support\Arr;
  10. use Illuminate\Support\Facades\DB;
  11. class OrderStatistics extends Chart
  12. {
  13. public function __construct()
  14. {
  15. parent::__construct();
  16. $this->setUpOptions();
  17. }
  18. /**
  19. * 初始化图表配置
  20. */
  21. protected function setUpOptions()
  22. {
  23. $this->options([
  24. 'chart' => [
  25. //'width' => '180%',
  26. 'type' => 'bar',
  27. 'events' => [
  28. ],
  29. 'toolbar' => ['show' => false],
  30. ],
  31. 'plotOptions' => [
  32. 'bar' => [
  33. //'columnWidth' => '45%',
  34. 'distributed' => true,
  35. ]
  36. ],
  37. 'dataLabels' => [
  38. 'enabled' => false
  39. ],
  40. 'legend' => [
  41. 'show' => false
  42. ],
  43. 'xaxis' => [
  44. //'categories' =>
  45. // [75, 125, 225, 175, 125, 75, 25]
  46. //,
  47. 'labels' => [
  48. 'show' => true,
  49. 'style' => [
  50. 'fontSize' => '12px'
  51. ]
  52. ],
  53. ],
  54. 'yaxis' => [
  55. 'show' => true
  56. ],
  57. 'tooltip' => [
  58. 'x' => ['show' => true],
  59. ],
  60. ]);
  61. }
  62. /**
  63. * 处理图表数据
  64. */
  65. protected function buildData()
  66. {
  67. $query = Order::query()
  68. ->where('agent_id',Admin::user()->id)
  69. ->select('*');
  70. $dateTime = request('created_at', 0);
  71. if ($dateTime) {
  72. $query->whereBetween('created_at',$dateTime);
  73. }
  74. switch (request('time_key', 0)) {
  75. case '1':
  76. $query->addSelect(DB::raw("count(id) as sum_price,CONCAT(YEAR(created_at),'-',MONTH(created_at),'-',DAY(created_at)) AS statistics_time"));
  77. break;
  78. case '30':
  79. $query->addSelect(DB::raw("count(id) as sum_price,CONCAT(YEAR(created_at),'-',MONTH(created_at)) AS statistics_time"));
  80. break;
  81. case '365':
  82. $query->addSelect(DB::raw("count(id) as sum_price,CONCAT(YEAR(created_at)) AS statistics_time"));
  83. break;
  84. default:
  85. $query->addSelect(DB::raw("count(id) as sum_price,CONCAT(YEAR(created_at),'-',MONTH(created_at),'-',DAY(created_at)) AS statistics_time"));
  86. }
  87. $order = $query->groupBy('statistics_time')
  88. ->orderBy('created_at')
  89. ->get()
  90. ->toArray();
  91. $this->withData([
  92. [
  93. 'name' => '订单数',
  94. 'data' => Arr::pluck($order,'sum_price')
  95. ],
  96. ]
  97. );
  98. $this->withCategories(
  99. Arr::pluck($order,'statistics_time')
  100. );
  101. }
  102. public function withData(array $data)
  103. {
  104. return $this->option('series', $data);
  105. }
  106. /**
  107. * 设置图表类别.
  108. *
  109. * @param array $data
  110. *
  111. * @return $this
  112. */
  113. public function withCategories(array $data)
  114. {
  115. return $this->option('xaxis.categories', $data);
  116. }
  117. /**
  118. * 渲染图表
  119. *
  120. * @return string
  121. */
  122. public function render()
  123. {
  124. $this->buildData();
  125. return parent::render();
  126. }
  127. }