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

175 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
  1. <?php
  2. namespace App\Admin\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. ->select('*');
  67. $dateTime = request('created_at', 0);
  68. if ($dateTime) {
  69. $query->whereBetween('created_at',[$dateTime['start'] . ' 00:00:00',$dateTime['end'].' 23:59:59']);
  70. }
  71. switch (request('time_key', 0)) {
  72. case '1':
  73. $query->addSelect(DB::raw("count(id) as sum_price,CONCAT(YEAR(created_at),'-',MONTH(created_at),'-',DAY(created_at)) AS statistics_time"));
  74. break;
  75. case '30':
  76. $query->addSelect(DB::raw("count(id) as sum_price,CONCAT(YEAR(created_at),'-',MONTH(created_at)) AS statistics_time"));
  77. break;
  78. case '365':
  79. $query->addSelect(DB::raw("count(id) as sum_price,CONCAT(YEAR(created_at)) AS statistics_time"));
  80. break;
  81. default:
  82. $query->addSelect(DB::raw("count(id) as sum_price,CONCAT(YEAR(created_at),'-',MONTH(created_at),'-',DAY(created_at)) AS statistics_time"));
  83. }
  84. $order = $query->groupBy('statistics_time')
  85. ->orderBy('statistics_time')
  86. ->get()
  87. ->toArray();
  88. $this->withData([
  89. [
  90. 'name' => '销量',
  91. 'data' => Arr::pluck($order,'sum_price')
  92. ],
  93. ]
  94. );
  95. $this->withCategories(
  96. Arr::pluck($order,'statistics_time')
  97. );
  98. }
  99. /**
  100. * 设置图表数据
  101. *
  102. * @param array $data
  103. *
  104. * @return \App\Admin\Metrics\Examples\ProductStatistics
  105. */
  106. public function withData(array $data)
  107. {
  108. return $this->option('series', $data);
  109. }
  110. /**
  111. * 设置图表类别.
  112. *
  113. * @param array $data
  114. *
  115. * @return $this
  116. */
  117. public function withCategories(array $data)
  118. {
  119. return $this->option('xaxis.categories', $data);
  120. }
  121. /**
  122. * 渲染图表
  123. *
  124. * @return string
  125. */
  126. public function render()
  127. {
  128. $this->buildData();
  129. return parent::render();
  130. }
  131. /**
  132. * 重写初始化JS
  133. * @return string
  134. */
  135. protected function buildDefaultScript()
  136. {
  137. $options = JavaScript::format($this->options);
  138. return <<<JS
  139. (function () {
  140. var options = {$options};
  141. var chart = new ApexCharts(
  142. $("{$this->containerSelector}")[0],
  143. options
  144. );
  145. chart.render();
  146. $(window).resize(function () {
  147. var height = $(window).height() - $('.Dcat_Admin_Widgets_Box').offset().top - 130;
  148. if (height < 320) {
  149. height = 320
  150. }
  151. chart.updateOptions({
  152. 'chart': {
  153. 'height': height
  154. }
  155. });
  156. }).resize();
  157. })();
  158. JS;
  159. }
  160. }