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

140 lines
2.6 KiB

4 years ago
  1. <?php
  2. namespace App\Admin\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. ->select('*');
  69. $dateTime = request('created_at', 0);
  70. if ($dateTime) {
  71. $query->whereBetween('created_at',$dateTime);
  72. }
  73. switch (request('time_key', 0)) {
  74. case '1':
  75. $query->addSelect(DB::raw("count(id) as sum_price,CONCAT(YEAR(created_at),'-',MONTH(created_at),'-',DAY(created_at)) AS statistics_time"));
  76. break;
  77. case '30':
  78. $query->addSelect(DB::raw("count(id) as sum_price,CONCAT(YEAR(created_at),'-',MONTH(created_at)) AS statistics_time"));
  79. break;
  80. case '365':
  81. $query->addSelect(DB::raw("count(id) as sum_price,CONCAT(YEAR(created_at)) AS statistics_time"));
  82. break;
  83. default:
  84. $query->addSelect(DB::raw("count(id) as sum_price,CONCAT(YEAR(created_at),'-',MONTH(created_at),'-',DAY(created_at)) AS statistics_time"));
  85. }
  86. $order = $query->groupBy('statistics_time')
  87. ->orderBy('created_at')
  88. ->get()
  89. ->toArray();
  90. $this->withData([
  91. [
  92. 'name' => '订单数',
  93. 'data' => Arr::pluck($order,'sum_price')
  94. ],
  95. ]
  96. );
  97. $this->withCategories(
  98. Arr::pluck($order,'statistics_time')
  99. );
  100. }
  101. public function withData(array $data)
  102. {
  103. return $this->option('series', $data);
  104. }
  105. /**
  106. * 设置图表类别.
  107. *
  108. * @param array $data
  109. *
  110. * @return $this
  111. */
  112. public function withCategories(array $data)
  113. {
  114. return $this->option('xaxis.categories', $data);
  115. }
  116. /**
  117. * 渲染图表
  118. *
  119. * @return string
  120. */
  121. public function render()
  122. {
  123. $this->buildData();
  124. return parent::render();
  125. }
  126. }