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

176 lines
3.4 KiB

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