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