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

174 lines
3.3 KiB

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 Dcat\Admin\Admin;
  6. use Dcat\Admin\Support\JavaScript;
  7. use Dcat\Admin\Widgets\ApexCharts\Chart;
  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 OrderStatistics extends Chart
  13. {
  14. public function __construct()
  15. {
  16. parent::__construct();
  17. $this->setUpOptions();
  18. }
  19. /**
  20. * 初始化图表配置
  21. */
  22. protected function setUpOptions()
  23. {
  24. $this->options([
  25. 'chart' => [
  26. //'width' => '180%',
  27. 'type' => 'bar',
  28. 'events' => [
  29. ],
  30. 'toolbar' => ['show' => false],
  31. ],
  32. 'plotOptions' => [
  33. 'bar' => [
  34. //'columnWidth' => '45%',
  35. 'distributed' => true,
  36. ]
  37. ],
  38. 'dataLabels' => [
  39. 'enabled' => false
  40. ],
  41. 'legend' => [
  42. 'show' => false
  43. ],
  44. 'xaxis' => [
  45. //'categories' =>
  46. // [75, 125, 225, 175, 125, 75, 25]
  47. //,
  48. 'labels' => [
  49. 'show' => true,
  50. 'style' => [
  51. 'fontSize' => '12px'
  52. ]
  53. ],
  54. ],
  55. 'yaxis' => [
  56. 'show' => true
  57. ],
  58. 'tooltip' => [
  59. 'x' => ['show' => true],
  60. ],
  61. ]);
  62. }
  63. /**
  64. * 处理图表数据
  65. */
  66. protected function buildData()
  67. {
  68. $query = Order::query()
  69. ->select('*');
  70. $dateTime = request('created_at', 0);
  71. if ($dateTime) {
  72. $query->whereBetween('created_at',[$dateTime['start'] . ' 00:00:00',$dateTime['end'].' 23:59:59']);
  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. /**
  128. * 重写初始化JS
  129. * @return string
  130. */
  131. protected function buildDefaultScript()
  132. {
  133. $options = JavaScript::format($this->options);
  134. return <<<JS
  135. (function () {
  136. var options = {$options};
  137. var chart = new ApexCharts(
  138. $("{$this->containerSelector}")[0],
  139. options
  140. );
  141. chart.render();
  142. $(window).resize(function () {
  143. var height = $(window).height() - $('.Dcat_Admin_Widgets_Box').offset().top - 130;
  144. if (height < 320) {
  145. height = 320
  146. }
  147. chart.updateOptions({
  148. 'chart': {
  149. 'height': height
  150. }
  151. });
  152. }).resize();
  153. })();
  154. JS;
  155. }
  156. }